Glide二次封装

Glide图片加载框架学习及常用方法封装
github地址:https://github.com/bumptech/glide
因为项目中引用了OkHttp框架,都是一家公司的项目,我这里的依赖如下:
[html] view plain copy
  1. compile 'com.github.bumptech.glide:glide:3.7.0'  
  2.     compile 'com.github.bumptech.glide:okhttp3-integration:[email protected]'  
  3.     compile 'com.squareup.okhttp3:okhttp:3.1.2'  
实现效果图如下:
   
Glide二次封装
Glide二次封装
GlideTestActivity.java:
[java] view plain copy
  1. package com.czhappy.kuaizhi.activity;  
  2.   
  3. import android.os.Bundle;  
  4. import android.view.View;  
  5. import android.widget.Button;  
  6. import android.widget.ImageView;  
  7. import android.widget.ProgressBar;  
  8.   
  9. import com.bumptech.glide.Glide;  
  10. import com.bumptech.glide.load.resource.drawable.GlideDrawable;  
  11. import com.bumptech.glide.request.animation.GlideAnimation;  
  12. import com.bumptech.glide.request.target.GlideDrawableImageViewTarget;  
  13. import com.czhappy.kuaizhi.R;  
  14. import com.czhappy.kuaizhi.glide.GlideImgManager;  
  15.   
  16. import butterknife.BindView;  
  17. import butterknife.ButterKnife;  
  18. import butterknife.OnClick;  
  19.   
  20. /** 
  21.  * Description: 
  22.  * User: chenzheng 
  23.  * Date: 2016/9/8 0008 
  24.  * Time: 09:01 
  25.  */  
  26. public class GlideTestActivity extends BaseActivity {  
  27.   
  28.     @BindView(R.id.imageview1)  
  29.     ImageView imageview1;  
  30.     @BindView(R.id.imageview2)  
  31.     ImageView imageview2;  
  32.     @BindView(R.id.imageview3)  
  33.     ImageView imageview3;  
  34.     @BindView(R.id.imageview4)  
  35.     ImageView imageview4;  
  36.     @BindView(R.id.imageview5)  
  37.     ImageView imageview5;  
  38.     @BindView(R.id.textview1)  
  39.     Button textview1;  
  40.     @BindView(R.id.imageview6)  
  41.     ImageView imageview6;  
  42.     @BindView(R.id.progressBar)  
  43.     ProgressBar progressBar;  
  44.   
  45.     private String imageArr[] = {"http://pic14.nipic.com/20110607/6776092_111031284000_2.jpg",  
  46.             "http://cdn.duitang.com/uploads/item/201507/13/20150713235634_UKdLB.jpeg",  
  47.             "http://att2.citysbs.com/taizhou/2011/08/27/101937_kuumaaio_40ee6a85b8df443965c4ca5e6f5d80fa.jpg"};  
  48.   
  49.     @Override  
  50.     protected void onCreate(Bundle savedInstanceState) {  
  51.         super.onCreate(savedInstanceState);  
  52.         this.setContentView(R.layout.activity_glide_test);  
  53.         ButterKnife.bind(this);  
  54.         initView();  
  55.     }  
  56.   
  57.     private void initView() {  
  58.         //加载网络图片(普通)  
  59.         GlideImgManager.loadImage(this, imageArr[0], imageview1);  
  60.   
  61.         //加载网络图片(圆角)  
  62.         GlideImgManager.loadRoundCornerImage(this, imageArr[1], imageview2);  
  63.   
  64.         //加载网络图片(圆形)  
  65.         GlideImgManager.loadCircleImage(this, imageArr[2], imageview3);  
  66.   
  67.         //加载项目中的图片  
  68.         GlideImgManager.loadImage(this, R.mipmap.ic_launcher, imageview4);  
  69.   
  70.         //加载网络图片(GIF)  
  71.         String gifUrl = "http://ww4.sinaimg.cn/mw690/bcc93f49gw1f6r1nce77jg207x07sx6q.gif";  
  72.         GlideImgManager.loadGifImage(this, gifUrl, imageview5);  
  73.     }  
  74.   
  75.     //加载进度监听  
  76.     private void loadListener() {  
  77.         Glide.with(this)  
  78.                 .load(imageArr[0])  
  79.                 .into(new GlideDrawableImageViewTarget(imageview6) {  
  80.                     @Override  
  81.                     public void onResourceReady(GlideDrawable drawable, GlideAnimation anim) {  
  82.                         super.onResourceReady(drawable, anim);  
  83.                         progressBar.setVisibility(View.GONE);  
  84.                     }  
  85.   
  86.                     @Override  
  87.                     public void onStart() {  
  88.                         super.onStart();  
  89.                         progressBar.setVisibility(View.VISIBLE);  
  90.                     }  
  91.                 });  
  92.     }  
  93.   
  94.     @OnClick(R.id.textview1)  
  95.     public void onClick(View view) {  
  96.         switch (view.getId()) {  
  97.             case R.id.textview1:  
  98.                 loadListener();  
  99.                 break;  
  100.         }  
  101.     }  
  102. }  

activity_glide_test.xml:
[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:background="@color/white">  
  7.   
  8.     <LinearLayout  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="match_parent"  
  11.         android:orientation="vertical">  
  12.   
  13.         <ImageView  
  14.             android:id="@+id/imageview1"  
  15.             android:layout_width="200dp"  
  16.             android:layout_height="160dp"  
  17.             android:layout_gravity="center_horizontal"  
  18.             android:layout_margin="10dp"  
  19.             android:scaleType="fitXY" />  
  20.   
  21.         <ImageView  
  22.             android:id="@+id/imageview2"  
  23.             android:layout_width="200dp"  
  24.             android:layout_height="160dp"  
  25.             android:layout_gravity="center_horizontal"  
  26.             android:layout_margin="10dp"  
  27.             android:scaleType="fitXY" />  
  28.   
  29.         <ImageView  
  30.             android:id="@+id/imageview3"  
  31.             android:layout_width="200dp"  
  32.             android:layout_height="200dp"  
  33.             android:layout_gravity="center_horizontal"  
  34.             android:layout_margin="10dp"  
  35.             android:scaleType="fitXY" />  
  36.   
  37.         <ImageView  
  38.             android:id="@+id/imageview4"  
  39.             android:layout_width="200dp"  
  40.             android:layout_height="200dp"  
  41.             android:layout_gravity="center_horizontal"  
  42.             android:layout_margin="10dp"  
  43.             android:scaleType="fitXY" />  
  44.   
  45.         <ImageView  
  46.             android:id="@+id/imageview5"  
  47.             android:layout_width="200dp"  
  48.             android:layout_height="200dp"  
  49.             android:layout_gravity="center_horizontal"  
  50.             android:layout_margin="10dp"  
  51.             android:scaleType="fitXY" />  
  52.   
  53.         <Button  
  54.             android:id="@+id/textview1"  
  55.             android:layout_width="match_parent"  
  56.             android:layout_height="wrap_content"  
  57.             android:layout_margin="10dp"  
  58.             android:padding="10dp"  
  59.             android:text="进度条加载" />  
  60.   
  61.         <FrameLayout  
  62.             android:layout_width="wrap_content"  
  63.             android:layout_height="wrap_content"  
  64.             android:layout_gravity="center_horizontal">  
  65.   
  66.             <ImageView  
  67.                 android:id="@+id/imageview6"  
  68.                 android:layout_width="200dp"  
  69.                 android:layout_height="160dp"  
  70.                 android:layout_gravity="center_horizontal"  
  71.                 android:layout_margin="10dp"  
  72.                 android:scaleType="fitXY" />  
  73.   
  74.             <ProgressBar  
  75.                 android:id="@+id/progressBar"  
  76.                 android:layout_width="32dp"  
  77.                 android:layout_height="32dp"  
  78.                 android:layout_gravity="center"  
  79.                 android:indeterminate="false"  
  80.                 android:visibility="gone" />  
  81.         </FrameLayout>  
  82.     </LinearLayout>  
  83. </ScrollView>  

下面就是代码中所用到的Glide加载图片时所用到的工具类:
GlideImgManager.java:
[java] view plain copy
  1. package com.czhappy.kuaizhi.glide;  
  2.   
  3. import android.content.Context;  
  4. import android.widget.ImageView;  
  5.   
  6. import com.bumptech.glide.Glide;  
  7. import com.bumptech.glide.load.engine.DiskCacheStrategy;  
  8. import com.czhappy.kuaizhi.R;  
  9.   
  10. import java.io.File;  
  11.   
  12. /** 
  13.  * Description: 
  14.  * User: chenzheng 
  15.  * Date: 2016/8/31 0031 
  16.  * Time: 15:43 
  17.  */  
  18. public class GlideImgManager {  
  19.   
  20.     public static void loadImage(Context context, String url, int erroImg, int emptyImg, ImageView iv) {  
  21.         //原生 API  
  22.         Glide.with(context).load(url).placeholder(emptyImg).error(erroImg).into(iv);  
  23.     }  
  24.   
  25.     public static void loadImage(Context context, String url, ImageView iv) {  
  26.         //原生 API  
  27.         Glide.with(context).load(url).crossFade().placeholder(R.drawable.empty_photo).error(R.drawable.empty_photo).into(iv);  
  28.     }  
  29.   
  30.     public static void loadGifImage(Context context, String url, ImageView iv) {  
  31.         Glide.with(context).load(url).asGif().diskCacheStrategy(DiskCacheStrategy.SOURCE).placeholder(R.drawable.empty_photo).error(R.drawable.empty_photo).into(iv);  
  32.     }  
  33.   
  34.   
  35.     public static void loadCircleImage(Context context, String url, ImageView iv) {  
  36.         Glide.with(context).load(url).placeholder(R.drawable.empty_photo).error(R.drawable.empty_photo).transform(new GlideCircleTransform(context)).into(iv);  
  37.     }  
  38.   
  39.     public static void loadRoundCornerImage(Context context, String url, ImageView iv) {  
  40.         Glide.with(context).load(url).placeholder(R.drawable.empty_photo).error(R.drawable.empty_photo).transform(new GlideRoundTransform(context,10)).into(iv);  
  41.     }  
  42.   
  43.   
  44.     public static void loadImage(Context context, final File file, final ImageView imageView) {  
  45.         Glide.with(context)  
  46.                 .load(file)  
  47.                 .into(imageView);  
  48.   
  49.   
  50.     }  
  51.   
  52.     public static void loadImage(Context context, final int resourceId, final ImageView imageView) {  
  53.         Glide.with(context)  
  54.                 .load(resourceId)  
  55.                 .into(imageView);  
  56.     }  
  57. }  

GlideCircleTransform.java:
[java] view plain copy
  1. package com.czhappy.kuaizhi.glide;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.Bitmap;  
  5. import android.graphics.BitmapShader;  
  6. import android.graphics.Canvas;  
  7. import android.graphics.Paint;  
  8.   
  9. import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;  
  10. import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;  
  11.   
  12. /** 
  13.  * Description: 
  14.  * User: chenzheng 
  15.  * Date: 2016/8/31 0031 
  16.  * Time: 15:42 
  17.  */  
  18. public class GlideCircleTransform extends BitmapTransformation {  
  19.     public GlideCircleTransform(Context context) {  
  20.         super(context);  
  21.     }  
  22.   
  23.     @Override protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {  
  24.         return circleCrop(pool, toTransform);  
  25.     }  
  26.   
  27.     private static Bitmap circleCrop(BitmapPool pool, Bitmap source) {  
  28.         if (source == null) return null;  
  29.   
  30.         int size = Math.min(source.getWidth(), source.getHeight());  
  31.         int x = (source.getWidth() - size) / 2;  
  32.         int y = (source.getHeight() - size) / 2;  
  33.   
  34.         // TODO this could be acquired from the pool too  
  35.         Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);  
  36.   
  37.         Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);  
  38.         if (result == null) {  
  39.             result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);  
  40.         }  
  41.   
  42.         Canvas canvas = new Canvas(result);  
  43.         Paint paint = new Paint();  
  44.         paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));  
  45.         paint.setAntiAlias(true);  
  46.         float r = size / 2f;  
  47.         canvas.drawCircle(r, r, r, paint);  
  48.         return result;  
  49.     }  
  50.   
  51.     @Override public String getId() {  
  52.         return getClass().getName();  
  53.     }  
  54. }  

GlideRoundTransform.java:
[java] view plain copy
  1. package com.czhappy.kuaizhi.glide;  
  2.   
  3. import android.content.Context;  
  4. import android.content.res.Resources;  
  5. import android.graphics.Bitmap;  
  6. import android.graphics.BitmapShader;  
  7. import android.graphics.Canvas;  
  8. import android.graphics.Paint;  
  9. import android.graphics.RectF;  
  10.   
  11. import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;  
  12. import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;  
  13.   
  14. /** 
  15.  * Description: 
  16.  * User: chenzheng 
  17.  * Date: 2016/8/31 0031 
  18.  * Time: 15:42 
  19.  */  
  20. public class GlideRoundTransform extends BitmapTransformation {  
  21.   
  22.     private static float radius = 0f;  
  23.   
  24.     public GlideRoundTransform(Context context) {  
  25.         this(context, 4);  
  26.     }  
  27.   
  28.     public GlideRoundTransform(Context context, int dp) {  
  29.         super(context);  
  30.         this.radius = Resources.getSystem().getDisplayMetrics().density * dp;  
  31.     }  
  32.   
  33.     @Override protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {  
  34.         return roundCrop(pool, toTransform);  
  35.     }  
  36.   
  37.     private static Bitmap roundCrop(BitmapPool pool, Bitmap source) {  
  38.         if (source == null) return null;  
  39.   
  40.         Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);  
  41.         if (result == null) {  
  42.             result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);  
  43.         }  
  44.   
  45.         Canvas canvas = new Canvas(result);  
  46.         Paint paint = new Paint();  
  47.         paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));  
  48.         paint.setAntiAlias(true);  
  49.         RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight());  
  50.         canvas.drawRoundRect(rectF, radius, radius, paint);  
  51.         return result;  
  52.     }  
  53.   
  54.     @Override public String getId() {  
  55.         return getClass().getName() + Math.round(radius);  
  56.     }  

当前工具类支持网络图片加载、圆角图片、圆形图片、手机本地图片、项目图片、gif图片等等,简单实用