Glide4.0與centerCrop衝突問題(同時解決刷新閃爍)方案

Glide4.0與centerCrop衝突問題(同時解決刷新閃爍)方案

也許大家會遇到使用加載圖片,需要圓角與centerCrop裁剪圖片,但是經過常規設置centerCrop,然後再設置圓角,發現實際運行是不生效的,沒有圓角,具體的原因可以谷歌下。
下面的解決也是網上千律一篇的解決方案,不過大家在拿來就用的時候,可能沒有注意到或者遇到刷新閃爍問題,就需要注意了,因爲網上方法不全導致的!
刷新加載Glide 的時候圖片會閃動一下,網上提供的各種解決Glide 加載圖片出現閃動可能有效,可能沒效果,終在下面方法

在此主要解決閃爍問題

大家可以仔細看下,下面方法比網上多了三個方法

  1. equals()
  2. hashCode()
  3. updateDiskCacheKey
/**
 * 實現圓角與centerCrop加載實現
 * 
 */
public class GlideRoundTransform extends BitmapTransformation {
    private static final String ID = "com.bumptech.glide.transformations.GlideRoundTransform";
    private static final byte[] ID_BYTES = ID.getBytes(Charset.forName("UTF-8"));
    private float radius = 0f;

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

    public GlideRoundTransform(Context context, int dp) {
        super();
        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 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 boolean equals(Object o) {
        if (o instanceof GlideRoundTransform) {
            GlideRoundTransform other = (GlideRoundTransform) o;
            return radius == other.radius;
        }
        return false;
    }

    @Override
    public int hashCode() {
        return Util.hashCode(ID.hashCode(), Util.hashCode(radius));
    }

    @Override
    public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
        messageDigest.update(ID_BYTES);

        byte[] radiusData = ByteBuffer.allocate(4).putFloat(radius).array();
        messageDigest.update(radiusData);
    }
}

這三個方法就是解決閃爍
實際官網給的很清楚,大家可以進行查閱
Transformations:
官網原文解決方案:http://bumptech.github.io/glide/doc/transformations.html

使用:
例如:

GlideApp.with(context)
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(placeErrorId)
                .error(placeErrorId)
                .transform(new GlideRoundTransform(context, roundingRadius))
                .into(imageView);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章