Android 开发:glide圆角正确实现方法

鉴于昨天很晚测试了一堆码后

我得出结论Glide4 我玩不转啊 /大哭

不过 测试的好几个百度到的示例 有的方法明显过时 有的性能 啧啧 不敢恭维...

其中比较好用的 有如下两个

直接抄砖吧....

/**
  * 圆形
 **/
public class GlideCircleTransform extends BitmapTransformation {

    private static final String ID = "com.examle.nnp.ah.glide.GlideCircleTransform";

    private static final byte[] ID_BYTES = ID.getBytes(CHARSET);

    @Override

    protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {

        int size = Math.min(toTransform.getWidth(), toTransform.getHeight());

        int x = (toTransform.getWidth() - size) / 2;

        int y = (toTransform.getHeight() - size) / 2;

        Bitmap square = Bitmap.createBitmap(toTransform, x, y, size, size);

        Bitmap circle = pool.get(size, size, Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(circle);

        Paint paint = new Paint();

        paint.setShader(new BitmapShader(square, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));

        paint.setAntiAlias(true);

        float r = size / 2f;

        canvas.drawCircle(r, r, r, paint);

        return circle;

    }

    @Override

    public boolean equals(Object obj) {

        return obj instanceof GlideCircleTransform;

    }

    @Override

    public int hashCode() {

        return ID.hashCode();

    }

    @Override

    public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {

        messageDigest.update(ID_BYTES);

    }

}

 

/**
  * 圆角
 **/
public class GlideRoundTransform  extends BitmapTransformation {

    private static final String ID = "com.examle.nnp.ah.glide.GlideRoundTransform";

    private static final byte[] ID_BYTES = ID.getBytes(CHARSET);

    private int radius;

    public GlideRoundTransform(Context context, int radius) {

        this.radius = UUi.dp2px(radius);

    }

    @Override

    protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {

        int width = toTransform.getWidth();

        int height = toTransform.getHeight();

        Bitmap bitmap = pool.get(width, height, Bitmap.Config.ARGB_8888);

        bitmap.setHasAlpha(true);

        Canvas canvas = new Canvas(bitmap);

        Paint paint = new Paint();

        paint.setAntiAlias(true);

        paint.setShader(new BitmapShader(toTransform, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));

        canvas.drawRoundRect(new RectF(0, 0, width, height), radius, radius, paint);

        return bitmap;

    }

    @Override

    public boolean equals(Object obj) {

        if (obj instanceof GlideRoundTransform) {

            GlideRoundTransform other = (GlideRoundTransform) obj;

            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).putInt(radius).array();

        messageDigest.update(radiusData);

    }

}

--------------------------------------------------------分 ---- 割 -----线----------------------------------------------------------------

到最后 我查阅官方文档发现....

新版的Glide里面 已经封装了相关的方法

RequestOptions options = new RequestOptions(); //圆角
options.bitmapTransform(new RoundedCorners(1));
RequestOptions options = new RequestOptions(); //圆形
options.bitmapTransform(new CircleCrop());

哭死.... 不说了 前面又有个坑 我先去踩了....

最后... 用完这个还是没解决您的问题 不妨试试 这个自定义的类

/**
 * @作者: yzq
 * @创建日期: 2019/8/7 20:25
 * @文件作用: 绘制圆角图
 **/
public class GlideRoundTransform extends BitmapTransformation {
    private static float radius = 0f;
    private static int type = 0;

    public GlideRoundTransform() {
        this(4);
    }

    public GlideRoundTransform(int dp) {
        super();
        this.type = 0;
        this.radius = Resources.getSystem().getDisplayMetrics().density * dp;
    }

    /**
     * @param dp
     * @param type 为1 则只绘制 图片上面两个圆角
     */
    public GlideRoundTransform(int dp, int type) {
        super();
        this.type = type;
        this.radius = Resources.getSystem().getDisplayMetrics().density * dp;
    }

    @Override
    protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {
        //变换的时候裁切
        Bitmap bitmap = TransformationUtils.centerCrop(pool, toTransform, outWidth, outHeight);
        return roundCrop(pool, bitmap);
    }

    @Override
    public void updateDiskCacheKey(MessageDigest messageDigest) {

    }


    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);
        if (type == 0) {
            RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight());
            canvas.drawRoundRect(rectF, radius, radius, paint);
        }
        if (type == 1) {
            RectF rectRound = new RectF(0f, 100f, source.getWidth(), source.getHeight());
            canvas.drawRect(rectRound, paint);
        }
        return result;
    }
}

调用

RequestOptions options = new RequestOptions();
            options.centerCrop()
                    .placeholder(R.drawable.c_error) //预加载
                    .error(R.drawable.c_error)      //失败
                    .priority(Priority.IMMEDIATE) //优先级
                    .diskCacheStrategy(DiskCacheStrategy.RESOURCE)//设置缓存策略
                    .transform(new GlideRoundTransform(cornerRadius,type));//设置四角圆角
Glide.with(context).load(url).apply(options).into(imageView);

当然 你要想了解更多Glide的使用 可参阅:https://www.cnblogs.com/94xiyang/p/9662042.html

https://blog.csdn.net/huangxiaoguo1/article/details/78543224

发布了35 篇原创文章 · 获赞 9 · 访问量 1万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 岁月 设计师: pinMode

分享到微信朋友圈

×

扫一扫,手机浏览