加載圖片框架Glide

如何使用Glide,首先需要將這個庫引入到我們的項目當中。然後在項目的build.gradle文件當中添加如下依賴:

dependencies {
    compile 'com.github.bumptech.glide:glide:3.7.0'
}
Glide中需要用到網絡功能,因此你還得在AndroidManifest.xml中聲明一下網絡權限纔行:

<uses-permission android:name="android.permission.INTERNET" />
那麼首先打開項目的佈局文件,在佈局當中加入一個Button和一個ImageView,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Load Image"
        android:onClick="loadImage"
        />

    <ImageView
        android:id="@+id/image_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>
public class MainActivity extends AppCompatActivity {

    ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = (ImageView) findViewById(R.id.image_view);
    }

    public void loadImage(View view) {
        String url = "http://cn.bing.com/az/hprichbg/rb/Dongdaemun_ZH-CN10736487148_1920x1080.jpg";
        Glide.with(this).load(url).into(imageView);
    }

}
實際上核心的代碼就只有這一行而已:
Glide.with(this).load(url).into(imageView);
Glide加載GIF圖並不需要編寫什麼額外的代碼,Glide內部會自動判斷圖片格式。




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章