Glide加载圆形图片

Glide加载圆形图片

glide已经在项目中用了一段时间了,算是一个很强大的框架了,最近有个需求,需要加载一张圆形图片,而且图片需要一个边框,网上一大推加载圆形图片的资料,基本都是一样的,但是没有加载边框的!我在原先代码的基础上加了几行,实现了能够加载边框的需求,代码如下:

/**
 * glide加载圆形图片的实现
 */
public class GlideCircleTransform extends BitmapTransformation {

    private int mBorderColor;
    private int mBorderWidth;

    public GlideCircleTransform(Context context) {
        super(context);
    }

    public GlideCircleTransform(Context context, int bordColor, int bordWidth) {
        super(context);
        this.mBorderColor = bordColor;
        this.mBorderWidth = bordWidth;
    }

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

    private  Bitmap circleCrop(BitmapPool pool, Bitmap source) {

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

        int width = (source.getWidth() - size) / 2;
        int height = (source.getHeight() - size) / 2;

        Bitmap bitmap = pool.get(size, size, Bitmap.Config.ARGB_8888);
        if (bitmap == null) {
            bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
        }

        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint();
        BitmapShader shader = new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
        if (width != 0 || height != 0) {
            Matrix matrix = new Matrix();
            matrix.setTranslate(-width, -height);
            shader.setLocalMatrix(matrix);
        }
        paint.setShader(shader);
        paint.setAntiAlias(true);
        float r = size / 2f;
        canvas.drawCircle(r, r, r, paint);

        if (mBorderWidth > 0){
            Paint mBorderPaint = new Paint();
            mBorderPaint.setStyle(Paint.Style.STROKE);
            mBorderPaint.setAntiAlias(true);
            mBorderPaint.setColor(mBorderColor);
            mBorderPaint.setStrokeWidth(mBorderWidth);

            int mBorderRadius = (size - mBorderWidth) / 2;
            canvas.drawCircle(r, r, mBorderRadius, mBorderPaint);
        }
        return bitmap;
    }

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

如果不知道怎么使用,请跳过此文章!!!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
关于Glide监听,可以使用RequestListener接口实现。例如: ``` Glide.with(context) .load(imageUrl) .listener(new RequestListener<Drawable>() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { // 失败时的逻辑处理 return false; } @Override public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) { // 成功时的逻辑处理 return false; } }) .into(imageView); ``` 其中,onLoadFailed()方法在失败时被调用,返回值表示是否要继续展示占位图;onResourceReady()方法在成功时被调用,返回值同样表示是否要继续展示占位图。 关于Glide的拓展,可以使用Glide的Transformation接口实现。例如,实现高斯模糊: ``` public class BlurTransformation extends BitmapTransformation { private static final int MAX_RADIUS = 25; private static final int DEFAULT_DOWN_SAMPLING = 1; private Context context; private int radius; private int sampling; public BlurTransformation(Context context) { this(context, MAX_RADIUS, DEFAULT_DOWN_SAMPLING); } public BlurTransformation(Context context, int radius) { this(context, radius, DEFAULT_DOWN_SAMPLING); } public BlurTransformation(Context context, int radius, int sampling) { super(); this.context = context; this.radius = radius; this.sampling = sampling; } @Override protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) { int width = toTransform.getWidth(); int height = toTransform.getHeight(); int scaledWidth = width / sampling; int scaledHeight = height / sampling; Bitmap bitmap = pool.get(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); canvas.scale(1 / (float) sampling, 1 / (float) sampling); Paint paint = new Paint(); paint.setFlags(Paint.FILTER_BITMAP_FLAG); canvas.drawBitmap(toTransform, 0, 0, paint); bitmap = FastBlurUtil.doBlur(bitmap, radius, true); return bitmap; } @Override public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) { messageDigest.update(("blur(" + radius + "," + sampling + ")").getBytes(CHARSET)); } } ``` 其中,FastBlurUtil.doBlur()方法是自定义的快速高斯模糊算法,可以在GitHub上搜索相关的库。 还可以实现圆形图片: ``` public class CircleTransformation extends BitmapTransformation { public CircleTransformation(Context context) { super(context); } @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 squared = Bitmap.createBitmap(toTransform, 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的拓展示例,可以根据需求进行修改和扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值