Glide加载图片问题

关于Android加载图片的问题

 目前安卓加载图片三大框架:
        1 Picasso
         2)   Glide
         3)   Fresco
简单谈一下使用Glide加载图片,Glide解决了快速加载图片的问题,但还有一个问题悬而未决:就是关于圆形图片和圆角图片问题,关于使用Glide动态加载圆角图片的问题,大家首先找到的很多blog如下所示:

compile 'com.github.bumptech.glide:glide:3.6.1'
GlideRoundTransform类
public class GlideRoundTransform extends BitmapTransformation {

    private static float radius = 0f;

    public GlideRoundTransform(Context context) {
        this(context, 4);
    }

    public GlideRoundTransform(Context context, int dp) {
        super(context);
        this.radius = Resources.getSystem().getDisplayMetrics().density * dp;
    }

    @Override protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
        return roundCrop(pool, toTransform);
    }

    private static Bitmap roundCrop(BitmapPool pool, Bitmap source) {
        if (source == null) return null;

        Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
        if (result == null) {
            result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
        }

        Canvas canvas = new Canvas(result);
        Paint paint = new Paint();
        paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
        paint.setAntiAlias(true);
        RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight());
        canvas.drawRoundRect(rectF, radius, radius, paint);
        return result;
    }

    @Override public String getId() {
        return getClass().getName() + Math.round(radius);
    }
}

然后动态加载:

glideRequest.load("https://www.baidu.com/img/bdlogo.png").transform(new GlideRoundTransform(context)).into(imageView);

当时一想的太简单,无非设置centerCrop的属性,继承BitmapTransformation重画。但是事实并不是这样的,在glide4.0上面 centerCrop和圆角图片有冲突只能显示一个,大部分都是上边的代码代码,发现这个在glide4.0上面直接报错无法识别。原因如下

查看new Centercrop()源码:

  1. /** 
  2.  * Scale the image so that either the width of the image matches the given width and the height of 
  3.  * the image is greater than the given height or vice versa, and then crop the larger dimension to 
  4.  * match the given dimension. 
  5.  * 
  6.  * Does not maintain the image's aspect ratio 
  7.  */  
  8. public class CenterCrop extends BitmapTransformation {  
  9.   private static final String ID = "com.bumptech.glide.load.resource.bitmap.CenterCrop";  
  10.   private static final byte[] ID_BYTES = ID.getBytes(CHARSET);  
  11.   public CenterCrop() {  
  12.     // Intentionally empty.  
  13.   }  
  14.   @Deprecated  
  15.   public CenterCrop(@SuppressWarnings("unused") Context context) {  
  16.     this();  
  17.   }  
  18.   @Deprecated  
  19.   public CenterCrop(@SuppressWarnings("unused") BitmapPool bitmapPool) {  
  20.     this();  
  21.   }  
  22.   // Bitmap doesn't implement equals, so == and .equals are equivalent here.  
  23.   @SuppressWarnings("PMD.CompareObjectsWithEquals")  
  24.   @Override  
  25.   protected Bitmap transform(  
  26.       @NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {  
  27.     return TransformationUtils.centerCrop(pool, toTransform, outWidth, outHeight);  
  28.   }  
  29.   @Override  
  30.   public boolean equals(Object o) {  
  31.     return o instanceof CenterCrop;  
  32.   }  
  33.   @Override  
  34.   public int hashCode() {  
  35.     return ID.hashCode();  
  36.   }  
  37.   @Override  
  38.   public void updateDiskCacheKey(MessageDigest messageDigest) {  
  39.     messageDigest.update(ID_BYTES);  
  40.   }  
  41. }  

里面也是继承了BitmapTransformation这个类然后重画,我们调用transform()这个方法等于把系统的Centercrop这个方法给覆盖了,这下只能在自己自定义的BitmapTransformation将两个效果一起画出来了。

SO,要这样写

扫描二维码关注公众号,回复: 11070002 查看本文章

  1. protected void onCreate(Bundle savedInstanceState) {  
  2.         super.onCreate(savedInstanceState);  
  3.         setContentView(R.layout.activity_main);  
  4.         RequestOptions myOptions = new RequestOptions().centerCrop();  
  5.         Glide.with(this).load(R.drawable.picture1).apply(myOptions).into(imageView);  
  6.     }  
设置transform圆角属性的要这么写
RequestOptions myOptions = new RequestOptions().centerCrop().transform(new GlideRoundTransform(this,30));  
Glide.with(this).load(R.drawable.item1).apply(myOptions).into(icon1);  

重构GlideRoundTransform 类:

public class GlideRoundTransform extends BitmapTransformation {
    private static float radius = 0f;
    public GlideRoundTransform(Context context) {
        this(context, 4);
    }
    public GlideRoundTransform(Context context, int dp) {
        super(context);
        this.radius = Resources.getSystem().getDisplayMetrics().density * dp;
    }
    @Override
    protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
        Bitmap bitmap = TransformationUtils.centerCrop(pool, toTransform, outWidth, outHeight);
        return roundCrop(pool, bitmap);
    }
    private static Bitmap roundCrop(BitmapPool pool, Bitmap source) {
        if (source == null) return null;
        Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
        if (result == null) {
            result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
        }
        Canvas canvas = new Canvas(result);
        Paint paint = new Paint();
        paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
        paint.setAntiAlias(true);
        RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight());
        canvas.drawRoundRect(rectF, radius, radius, paint);
        return result;
    }
    public String getId() {
        return getClass().getName() + Math.round(radius);
    }
    @Override
    public void updateDiskCacheKey(MessageDigest messageDigest) {
    }
}

实现效果如下:



最后附上两个关于Glide资源(下载链接):

Glide4.0以下和以上,欢迎下载配套资源:

https://download.csdn.net/download/sailor_luo/10371032

点击打开链接

发布了149 篇原创文章 · 获赞 132 · 访问量 11万+

猜你喜欢

转载自blog.csdn.net/Sailor_luo/article/details/80066430