glide 长方形图片显示圆角问题

目前业务是RecyclerView嵌套RecyclerView,子RecyclerView里面显示图片,图片显示方式又分为 多图和单图显示方式(这个是已经调试好的效果)

 测试显示结果只有单张图片不显示圆角,直觉告诉我可能是图片太大了(260*100)那就改小点 比如200*160 测试显示还是一样的效果,没有任何反应。

索性直接把单个图片的大小改成和多图一样的,运行测试我去奇迹出现了 居然有圆角了 有圆角了 这是什么情况 难道是图片只能是正文形?想着glide不能这么变态吧(哈哈)

又在想网找了几个圆角的公共类 发现没啥不一样,在找到 https://github.com/wildma/GlideRoundImage/blob/master/GlideRoundImage.java 看到写前4.0以上代码问题

@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
Bitmap bitmap = TransformationUtils.centerCrop(pool, toTransform, outWidth, outHeight);
return roundCrop(pool, bitmap);
}
完整代码
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;

import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
import com.bumptech.glide.load.resource.bitmap.TransformationUtils;

import java.security.MessageDigest;

public class GlideRoundTransform extends BitmapTransformation  {

    private static float radius = 0f;

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

    public GlideRoundTransform(Context context, int dp) {
        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;
    }

    @Override
    public void updateDiskCacheKey(MessageDigest messageDigest) {

    }
}
View Code

 

APK安装包下载地址

posted on 2019-04-28 17:20  freexiaoyu  阅读(1439)  评论(0编辑  收藏  举报