原文地址https://futurestud.io/blog/glide-getting-started
Glide,和Picasso很相似,可以從各種圖片來源加載和顯示圖片,并且很好的支持緩存。同時,它在對圖片操作時,只占用很少的內存。Glide已經被谷歌官方的應用程序所使用(如2015年的 Google I / O的應用程序),同時,它和Picasso一樣受到Android應用開發者的歡迎。
Gradle:
compile'com.github.bumptech.glide:glide:3.6.1'
Maven:
com.github.bumptech.glideglide3.6.1aar
Eclipse:
在這里https://github.com/bumptech/glide/releases下載jar包,放到libs文件夾。
和Picasso一樣,Glide也使用流式的接口。Glide 至少需要三個參數構造一個完整的圖片加載請求:
with(Context context) - 上下文環境
load(String imageUrl) - 需要加載圖片的URL.
into(ImageView targetImageView) - 圖片顯示的ImageView.
下面是最簡單加載網絡圖片的用法:
ImageView targetImageView = (ImageView) findViewById(R.id.imageView);
String internetUrl ="http://i.imgur.com/DvpvklR.png";
Glide
.with(context)
.load(internetUrl)
.into(targetImageView);
從資源文件中加載:
int resourceId = R.mipmap.ic_launcher;
Glide
.with(context)
.load(resourceId)
.into(imageViewResource);
從文件中加載圖片:
Filefile=newFile(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])
.placeholder(R.mipmap.ic_launcher) //設置占位圖
.error(R.mipmap.future_studio_launcher)
//設置錯誤圖片
.crossFade() //設置淡入淡出效果,默認300ms,可以傳參
//.dontAnimate() //不顯示動畫效果
.into(imageViewFade);
當不知道ImageView的大小的時候,這個選項是非常有用的,我們可以設置需要加載的圖片尺寸。
Glide支持兩種圖片縮放形式,CenterCrop 和 FitCenter
CenterCrop:等比例縮放圖片,直到圖片的狂高都大于等于ImageView的寬度,然后截取中間的顯示。
Glide
.with(context)
.load(UsageExampleListViewAdapter.eatFoodyImages[0])
.override(600,200) // resizes
the image to these dimensions (inpixel)
.centerCrop() // this
cropping technique scales the image so that it fills the requested boundsandthen cropsthe extra.
.into(imageViewResizeCenterCrop);
FitCenter:等比例縮放圖片,寬或者是高等于ImageView的寬或者是高。
Glide
.with(context)
.load(UsageExampleListViewAdapter.eatFoodyImages[0])
.override(600,200)
.fitCenter()
.into(imageViewResizeFitCenter);
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可以加載視頻的縮略圖:
StringfilePath ="/storage/emulated/0/Pictures/example_video.mp4";
Glide
.with(context)
.load( Uri.fromFile(newFile( filePath )) )
.into(imageViewGifAsBitmap );
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通過Target的回調獲取Bitmap,最常用的是SimpleTarget:
privateSimpleTarget
target =newSimpleTarget() {
@Override
publicvoidonResourceReady(Bitmapbitmap, GlideAnimation glideAnimation) {
// do something with the bitmap
// for demonstration purposes, let's
just set it to an ImageView
imageView1.setImageBitmap( bitmap );
}
};
privatevoidloadImageSimpleTarget() {
Glide
.with( context )// could be an issue!
.load(eatFoodyImages[0] )
.asBitmap()
.into( target );
}
-
設置Bitmap的大小:
privateSimpleTarget
target2 =newSimpleTarget(250,250) {
@Override
publicvoidonResourceReady(Bitmapbitmap, GlideAnimation glideAnimation) {
imageView2.setImageBitmap( bitmap );
}
};
privatevoidloadImageSimpleTargetApplicationContext() {
Glide
.with(context.getApplicationContext() )// safer!
.load(eatFoodyImages[1] )
.asBitmap()
.into( target2 );
}