目录
7. Glide自定义圆角
7.1 定义圆角类
定义:CornersTransform
代码如下:
public class CornersTransform extends BitmapTransformation {
private float radius;
public CornersTransform(int radius) {
this.radius = radius;
}
@Override
protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth,
int outHeight)
{
return cornersImage(pool, toTransform);
}
private Bitmap cornersImage(BitmapPool pool, Bitmap bitmapSource) {
Bitmap tempBitmap = null;
try {
if (bitmapSource == null) {
return null;
}
tempBitmap = pool.get(bitmapSource.getWidth(), bitmapSource.getHeight(),
Bitmap.Config.ARGB_8888);
if (tempBitmap == null) {
tempBitmap = Bitmap.createBitmap(bitmapSource.getWidth(),
bitmapSource.getHeight(), Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(tempBitmap);
Paint paint = new Paint();
paint.setShader(new BitmapShader(bitmapSource, BitmapShader.TileMode.CLAMP,
BitmapShader.TileMode.CLAMP));
paint.setAntiAlias(true);
RectF rectF = new RectF(0f, 0f, bitmapSource.getWidth(),
bitmapSource.getHeight());
canvas.drawRoundRect(rectF, radius, radius, paint);
} catch (Exception e) {
e.printStackTrace();
}
return tempBitmap;
}
@Override
public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
}
}
7.2 添加圆角
int radius = 30;
RequestOptions options = new RequestOptions().transform(new CornersTransform(radius));
Glide.with(getApplicationContext()).load(imageUrl).apply(options).into(imageView);