Glide,和Picasso很相似,可以從各種圖片來源加載和顯示圖片,並且很好的支持緩存。同時,它在對圖片操作時,只占用很少的內存。Glide已經被谷歌官方的應用程序所使用(如2015年的 Google I / O的應用程序),同時,它和Picasso一樣受到Android應用開發者的歡迎。
在項目中引入Glide
Gradle:
compile 'com.github.bumptech.glide:glide:3.6.1'
Maven:
<dependency> <groupId>com.github.bumptech.glide</groupId> <artifactId>glide</artifactId> <version>3.6.1</version> <type>aar</type> </dependency>
Eclipse:
在這里 https://github.com/bumptech/glide/releases下載jar包,放到libs文件夾。
簡單使用,從URL中加載圖片
和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);
從文件中加載圖片:
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 ); }
