Android Glide 加载圆形图片和圆角图片的两种方法

两种使用 BitmapTransformation 来实现 Glide 加载圆形图片和圆角图片的方法。

背景

Glide 并不能直接支持 Round Pictures ,需要使用 BitmapTransformation 来进行处理。

Round Pictures: CircleImageView / CircularImageView / RoundedImageView are known to have issues with TransitionDrawable (.crossFade() with .thumbnail() or .placeholder()) and animated GIFs, use a BitmapTransformation (.circleCrop() will be available in v4) or .dontAnimate() to fix the issue.

这里介绍下网上常见的方式和使用 RoundedBitmapDrawable 两种方法,本质上是差不多的:

  • 使用 Canvas 和 Paint 来绘制
  • 使用 Android.support.v4.graphics.drawable.RoundedBitmapDrawable

PS: RoundedBitmapDrawable 是 support.v4 下的一个类,想了解更多,可以阅读我之前的文章: Android 必知必会-使用 supportV4 的 RoundedBitmapDrawable 实现圆角

代码实现

方法一

实现圆形图片:

/**
 * Glide 圆形图片 Transform
 */

public class GlideCircleTransform extends BitmapTransformation {
    public GlideCircleTransform(Context context) {
        super(context);
    }

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

    private static Bitmap circleCrop(BitmapPool pool, Bitmap source) {
        if (source == null) return null;
        int size = Math.min(source.getWidth(), source.getHeight());
        int x = (source.getWidth() - size) / 2;
        int y = (source.getHeight() - size) / 2;
        Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);
        Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);
        if (result == null) {
            result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
        }
        Canvas canvas = new Canvas(result);
        Paint paint = new Paint();
        paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
        paint.setAntiAlias(true);
        float r = size / 2f;
        canvas.drawCircle(r, r, r, paint);
        return result;
    }

    @Override
    public String getId() {
        return getClass().getName();
    }
}

实现圆角图片:

/**
 * Glide 圆角 Transform
 */

public class GlideRoundTransform extends BitmapTransformation {

    private static float radius = 0f;

    /**
 * 构造函数 默认圆角半径 4dp
 *
 * @param context Context
 */
    public GlideRoundTransform(Context context) {
        this(context, 4);
    }

    /**
 * 构造函数
 *
 * @param context Context
 * @param dp 圆角半径
 */
    public GlideRoundTransform(Context context, int dp) {
        super(context);
        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);
    }
}

方法二

public static Bitmap drawableToBitmap(Drawable drawable) {
        // 取 drawable 的长宽
        int w = drawable.getIntrinsicWidth();
        int h = drawable.getIntrinsicHeight();

        // 取 drawable 的颜色格式
        Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                : Bitmap.Config.RGB_565;
        // 建立对应 bitmap
        Bitmap bitmap = Bitmap.createBitmap(w, h, config);
        // 建立对应 bitmap 的画布
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, w, h);
        // 把 drawable 内容画到画布中
        drawable.draw(canvas);
        return bitmap;
    }
/**
 * RoundedBitmapDrawable 是 V4 下的一个类,不能简单的通过:强制转换成 BitmapDrawable
 * Bitmap bitmap = ((BitmapDrawable)xxx).getBitmap();
 */

实现圆形:

RoundedBitmapDrawable drawableA = RoundedBitmapDrawableFactory.create(getResources(), bitmap);
        drawableA.setCircular(true);
        Bitmap a = drawableToBitmap(drawableA);

实现圆角:

RoundedBitmapDrawable drawableB = RoundedBitmapDrawableFactory.create(getResources(), bitmap);
       drawableB.setCornerRadius(30L);
       Bitmap b = drawableToBitmap(drawableB);

 

来自:http://likfe.com/2016/08/31/android-glide-roundedBitmapTransformation/

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值