在項目中引入Glide
Gradle:
compile ‘com.github.bumptech.glide:glide:3.7.0’
Maven:
com.github.bumptech.glide
glide
3.7.0
Eclipse:
在這裏 https://github.com/bumptech/glide/releases下載jar包,放到libs文件夾。
glide全局配置
有時我們需要配置Glide的緩存路徑,緩存大小等配置,我們可以通過實現 GlideModule接口:
private static final int DISK_CACHE_SIZE = 100 * 1024 * 1024;
public static final int MAX_MEMORY_CACHE_SIZE = 10 * 1024 * 1024;
@Override
public void applyOptions(final Context context, GlideBuilder builder) {
//設置磁盤緩存的路徑 path
final File cacheDir = new File(path);
builder.setDiskCache(new DiskCache.Factory() {
@Override
public DiskCache build() {
return DiskLruCacheWrapper.get(cacheDir, DISK_CACHE_SIZE);
}
});
//設置內存緩存大小,一般默認使用glide內部的默認值
builder.setMemoryCache(new LruResourceCache(MAX_MEMORY_CACHE_SIZE));
}
@Override
public void registerComponents(Context context, Glide glide) {
}
然後在AndroidManifest.xml中添加配置:
int resourceId = R.mipmap.ic_launcher;
Glide
.with(context)
.load(resourceId)
.into(imageViewResource);
從文件中加載圖片
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Running.jpg");
Glide
.with(context)
.load(file)
.into(imageViewFile);
從URI中加載圖片
Uri uri = resourceIdToUri(context, R.mipmap.future_studio_launcher);
Glide
.with(context)
.load(uri)
.into(imageViewUri);
佔位圖,錯誤圖,和淡入淡出效果
Glide
.with(context)
.load(UsageExampleListViewAdapter.eatFoodyImages[0])
.placeholder(R.mipmap.ic_launcher) //設置佔位圖
.error(R.mipmap.future_studio_launcher) //設置錯誤圖片
.crossFade() //設置淡入淡出效果,默認300ms,可以傳參
//.dontAnimate() //不顯示動畫效果
.into(imageViewFade);
設置圖片大小和縮放形式
Glide 會根據ImageView的大小,自動限制圖片緩存和內存中的大小,當然也可以通過調用override(horizontalSize, verticalSize)限制圖片的大小:
Glide
.with(context)
.load(UsageExampleListViewAdapter.eatFoodyImages[0])
.override(600, 200) // resizes the image to these dimensions (in pixel). does not respect aspect ratio
.into(imageViewResize);
當不知道ImageView的大小的時候,這個選項是非常有用的,我們可以設置需要加載的圖片尺寸。
Glide支持兩種圖片縮放形式,CenterCrop 和 FitCenter
CenterCrop:等比例縮放圖片,直到圖片的狂高都大於等於ImageView的寬度,然後截取中間的顯示。
Glide
.with(context)
.load(UsageExampleListViewAdapter.eatFoodyImages[0])
.override(600, 200) // resizes the image to these dimensions (in pixel)
.centerCrop() // this cropping technique scales the image so that it fills the requested bounds and then crops the extra.
.into(imageViewResizeCenterCrop);
FitCenter:等比例縮放圖片,寬或者是高等於ImageView的寬或者是高。
Glide
.with(context)
.load(UsageExampleListViewAdapter.eatFoodyImages[0])
.override(600, 200)
.fitCenter()
.into(imageViewResizeFitCenter);
加載GIF和視頻文件
Fresco支持加載GIF,並且使用的方式和加載圖片一樣:
String gifUrl = "http://i.kinja-img.com/gawker-media/image/upload/s--B7tUiM5l--/gf2r69yorbdesguga10i.gif";
Glide
.with( context )
.load( gifUrl )
.asGif()
.error( R.drawable.full_cake )
.into( imageViewGif );
Glide可以加載視頻的縮略圖:
String filePath = "/storage/emulated/0/Pictures/example_video.mp4";
Glide
.with( context )
.load( Uri.fromFile( new File( filePath ) ) )
.into( imageViewGifAsBitmap );
Glide緩存策略
Glide默認開啓磁盤緩存和內存緩存,當然也可以對單張圖片進行設置特定的緩存策略。
設置圖片不加入到內存緩存
Glide
.with( context )
.load( eatFoodyImages[0] )
.skipMemoryCache( true )
.into( imageViewInternet );
設置圖片不加入到磁盤緩存
Glide
.with( context )
.load( eatFoodyImages[0] )
.diskCacheStrategy( DiskCacheStrategy.NONE )
.into( imageViewInternet );
Glide支持多種磁盤緩存策略:
DiskCacheStrategy.NONE :不緩存圖片
DiskCacheStrategy.SOURCE :緩存圖片源文件
DiskCacheStrategy.RESULT:緩存修改過的圖片
DiskCacheStrategy.ALL:緩存所有的圖片,默認
圖片加載優先級
Glide支持爲圖片加載設置優先級,優先級高的先加載,優先級低的後加載:
private void loadImageWithHighPriority() {
Glide
.with( context )
.load( UsageExampleListViewAdapter.eatFoodyImages[0] )
.priority( Priority.HIGH )
.into( imageViewHero );
}
private void loadImagesWithLowPriority() {
Glide
.with( context )
.load( UsageExampleListViewAdapter.eatFoodyImages[1] )
.priority( Priority.LOW )
.into( imageViewLowPrioLeft );
Glide
.with( context )
.load( UsageExampleListViewAdapter.eatFoodyImages[2] )
.priority( Priority.LOW )
.into( imageViewLowPrioRight );
}
Glide獲取Bitmap
Glide通過Target的回調獲取Bitmap,最常用的是SimpleTarget:
private SimpleTarget target = new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap bitmap, GlideAnimation glideAnimation) {
// do something with the bitmap
// for demonstration purposes, let's just set it to an ImageView
imageView1.setImageBitmap( bitmap );
}
};
private void loadImageSimpleTarget() {
Glide
.with( context ) // could be an issue!
.load( eatFoodyImages[0] )
.asBitmap()
.into( target );
}
設置Bitmap的大小:
private SimpleTarget target2 = new SimpleTarget<Bitmap>( 250, 250 ) {
@Override
public void onResourceReady(Bitmap bitmap, GlideAnimation glideAnimation) {
imageView2.setImageBitmap( bitmap );
}
};
private void loadImageSimpleTargetApplicationContext() {
Glide
.with( context.getApplicationContext() ) // safer!
.load( eatFoodyImages[1] )
.asBitmap()
.into( target2 );
}
調試和混淆
有時需要查看加載圖片的一些信息,包括加載的時間,圖片URL,圖片的尺寸,這時,可以把調試的開關打開
adb shell setprop log.tag.DecodeJob VERBOSE
把這個開關打開就能看到加載每張圖片的一些信息:
如果項目開啓了混淆,需要在app proguard文件中加入:
glide
-keep public class * implements com.bumptech.glide.module.GlideModule
-keep public enum com.bumptech.glide.load.resource.bitmap.ImageHeaderParser$** {
**[] $VALUES;
public *;
}