注意:在Android Studio的項目的build.gradle中添加:
compile 'com.github.bumptech.glide:glide:3.6.1'
然后同步一下
目錄:
- 使用Glide結合列表的樣式進行圖片加載
- 如果使用的是RecyclerView,可以在Adapter的onBindViewHolder方法中使用
- 當加載網絡圖片時,由於加載過程中圖片未能及時顯示,此時可能需要設置等待時的圖片,通過placeHolder()方法
- 當加載圖片失敗時,通過error(Drawable drawable)方法設置加載失敗后的圖片顯示
- 圖片的縮放,centerCrop()和fitCenter()
- 顯示gif動畫
- 顯示本地視頻
- 緩存策略
- 優先級,設置圖片加載的順序
- 當不需要將加載的資源直接放入到ImageView中而是想獲取資源的Bitmap對象
- 集成網絡棧(okHttp,Volley)
1.使用Glide結合列表的樣式進行圖片加載:
1) 如果使用的是ListView,可以直接在Adapter的getView方法中使用:
@Overridepublic View getView(int position, View convertView, ViewGroup parent) {if (null == convertView) {//.....}Glide.with(context).load(imageUrls[position]).into(holder.imageView);return convertView;}
2) 如果使用的是RecyclerView,可以在Adapter的onBindViewHolder方法中使用:
@Overridepublic void onBindViewHolder(RVViewHolder holder, int position) {Glide.with(MainActivity.this).load(args[position]).into(holder.imageView);}
3) 當加載網絡圖片時,由於加載過程中圖片未能及時顯示,此時可能需要設置等待時的圖片,通過placeHolder()方法:
Glide.with(context).load(UsageExampleListViewAdapter.eatFoodyImages[0]).placeholder(R.mipmap.ic_launcher) // can also be a drawable.into(imageViewPlaceholder);
4) 當加載圖片失敗時,通過error(Drawable drawable)方法設置加載失敗后的圖片顯示:
Glide.with(context).load("http://futurestud.io/non_existing_image.png").error(R.mipmap.future_studio_launcher) // will be displayed if the image cannot be loaded.into(imageViewError);
5) 圖片的縮放,centerCrop()和fitCenter():
//使用centerCrop是利用圖片圖填充ImageView設置的大小,如果ImageView的//Height是match_parent則圖片就會被拉伸填充Glide.with(MainActivity.this).load(args[position]).centerCrop().into(holder.imageView);
//使用fitCenter即縮放圖像讓圖像都測量出來等於或小於 ImageView 的邊界范圍//該圖像將會完全顯示,但可能不會填滿整個 ImageView。Glide.with(MainActivity.this).load(args[position]).fitCenter().into(holder.imageView);
6) 顯示gif動畫:
Glide.with( context ).load( gifUrl ).asGif() //判斷加載的url資源是否為gif格式的資源.error( R.drawable.full_cake ).into( imageViewGif );
7) 顯示本地視頻
String filePath = "/storage/emulated/0/Pictures/example_video.mp4";Glide.with( context ).load( Uri.fromFile( new File( filePath ) ) ).into( imageViewGifAsBitmap );
8) 緩存策略:
Glide.with( context ).load( Images[0] ).skipMemoryCache( true ) //跳過內存緩存.into( imageViewInternet );
Glide.with( context ).load( images[0] ).diskCacheStrategy( DiskCacheStrategy.NONE ) //跳過硬盤緩存.into( imageViewInternet );
DiskCacheStrategy.NONE什么都不緩存DiskCacheStrategy.SOURCE僅僅只緩存原來的全分辨率的圖像DiskCacheStrategy.RESULT僅僅緩存最終的圖像,即降低分辨率后的(或者是轉換后的)DiskCacheStrategy.ALL緩存所有版本的圖像(默認行為)
9) 優先級,設置圖片加載的順序:
Priority.LOWPriority.NORMALPriority.HIGHPriority.IMMEDIATE
private void loadImageWithHighPriority() {Glide.with( context ).load( mages[0] ).priority( Priority.HIGH ).into( imageViewHero );}private void loadImagesWithLowPriority() {Glide.with( context ).load( images[1] ).priority( Priority.LOW ).into( imageViewLowPrioLeft );Glide.with( context ).load( images[2] ).priority( Priority.LOW ).into( imageViewLowPrioRight );}
10) 當不需要將加載的資源直接放入到ImageView中而是想獲取資源的Bitmap對象:
//括號中的300,600代表寬和高但是未有作用SimpleTarget target = new SimpleTarget<Bitmap>(300,600) {@Overridepublic void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {holder.imageView.setImageBitmap(resource);}};Glide.with(MainActivity.this).load(args[position]).asBitmap().into(target);
11) 集成網絡棧(okHttp,Volley):
dependencies {// your other dependencies// ...// Glidecompile 'com.github.bumptech.glide:glide:3.6.1'// Glide's OkHttp Integrationcompile 'com.github.bumptech.glide:okhttp-integration:1.3.1@aar'compile 'com.squareup.okhttp:okhttp:2.5.0'}
dependencies {// your other dependencies// ...// Glidecompile 'com.github.bumptech.glide:glide:3.6.1'// Glide's Volley Integrationcompile 'com.github.bumptech.glide:volley-integration:1.3.1@aar'compile 'com.mcxiaoke.volley:library:1.0.8'}
