Glide 4.0 实现圆形+描边
Posted wlytctw
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Glide 4.0 实现圆形+描边相关的知识,希望对你有一定的参考价值。
Glide 绘制边框
圆形Glide 内部已经实现。但描边效果需要我们自己绘制。在项目当中使用 CircleImageView 实现该效果,但是Glide 与 CircleImageView 组合使用重现种种问题,虽说网上有相关解决方案,但是想想还是从根源上解决,为此我们需要 继承BitmapTransformation ,重写 transform 实现前述所说效果。直接看实现:
效果图如下:
圆形描边效果实现。
代码:
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import java.security.MessageDigest;
import jp.wasabeef.glide.transformations.BitmapTransformation;
/**
* Glide 的图形变换可参考Glide 示例源码
* @link com.bumptech.glide.load.resource.bitmap.CenterCrop
* @link com.bumptech.glide.load.resource.bitmap.BitmapTransformation
*
*/
public class GlideCircleBorderTransform extends BitmapTransformation
private final String ID = getClass().getName();
private Paint mBorderPaint;
private float borderWidth;
private int borderColor;
public GlideCircleBorderTransform(float borderWidth, int borderColor)
this.borderWidth = borderWidth;
this.borderColor = borderColor;
mBorderPaint = new Paint();
mBorderPaint.setColor(borderColor);
mBorderPaint.setStyle(Paint.Style.STROKE);
mBorderPaint.setAntiAlias(true);
mBorderPaint.setStrokeWidth(borderWidth);
mBorderPaint.setDither(true);
@Override
protected Bitmap transform(Context context, BitmapPool bitmapPool, Bitmap bitmap, int i, int i1)
return circleCrop(bitmapPool, bitmap);
private Bitmap circleCrop(BitmapPool bitmapPool, Bitmap source)
int size = Math.min(source.getWidth(), source.getHeight());
int x = (source.getWidth() - size) / 2;
int y = (source.getHeight() - size) / 2;
Bitmap square = Bitmap.createBitmap(source, x, y, size, size);
Bitmap result = bitmapPool.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();
//设置 Shader
paint.setShader(new BitmapShader(square, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
paint.setAntiAlias(true);
float radius = size / 2f;
//绘制一个圆
canvas.drawCircle(radius, radius, radius, paint);
/************************描边*********************/
//注意:避免出现描边被屏幕边缘裁掉
float borderRadius = radius - (borderWidth / 2);
//画边框
canvas.drawCircle(radius, radius, borderRadius, mBorderPaint);
return result;
@Override
public void updateDiskCacheKey(MessageDigest messageDigest)
messageDigest.update(ID.getBytes(CHARSET));
@Override
public boolean equals(Object o)
return o instanceof GlideCircleBorderTransform;
@Override
public int hashCode()
return ID.hashCode();
使用:
/**
* 加载图片-- 圆形+描边展示---不展示渐进动画
* @param glideContextWrapper
* @param imageView
* @param url
* @param borderWidth
* @param borderColor
* @param placeholder
*/
public static void loadCircle(GlideContextWrapper glideContextWrapper, ImageView imageView, String url, float borderWidth, int borderColor,@IdRes int placeholder)
RequestOptions options = new RequestOptions()
.placeholder(placeholder)
.fallback(placeholder)
.centerCrop()
.bitmapTransform(new GlideCircleBorderTransform(borderWidth, borderColor))
.diskCacheStrategy(DiskCacheStrategy.DATA);
ImageLoader.getRequestManager(glideContextWrapper)
.load(url)
.apply(options)
.into(imageView);
结束!
以上是关于Glide 4.0 实现圆形+描边的主要内容,如果未能解决你的问题,请参考以下文章
Glide的加载图片的帮助类,用来把图片圆角或者改成圆形图片