Android Glide加载圆形图片、圆角图片,部分圆角图片的使用方法
Android Glide加载圆形图片、圆角图片,部分圆角图片的使用方法
前言
通过本文,您可以实现以下效果:
Gilde圆形图片/头像
效果如图,只需要设置requestoption即可,添加circleCropTransform();
具体代码如下:
RequestOptions options = new RequestOptions()
.placeholder(R.drawable.loading)
.circleCropTransform();
Glide.with(mContext).asBitmap()
.load(imageUrl)
.apply(options)
.into(myImageView);
Gilde普通圆角图片即四个角都是圆角
效果如图,只需要设置requestoption即可,添加bitmapTransform(new RoundedCorners(20);
// 加载为四个都是圆角的图片 可以设置圆角幅度
RequestOptions options = new RequestOptions()
.placeholder(R.drawable.loading)
.bitmapTransform(new RoundedCorners(DipPx.dip2px(mContext, 10)));
Glide.with(mContext).asBitmap()
.load(imageUrl)
.apply(options)
.into(myImageView);
此处实现的时候用了一个工具类DipPx中dip2px,将dp值转化为像素值;
该类的具体代码如下:
// 工具类 提供px和dip的相互转化
public class DipPx {
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
}
Gilde对指定角设置圆角
效果如图左上角和右上角为圆角,左下右下为直角,其实现稍微有些复杂;首先需要新建一个class,CornerTransform实现Transformation
,具体代码如下:
public class CornerTransform implements Transformation {
private BitmapPool mBitmapPool;
private float radius;
private boolean isLeftTop, isRightTop, isLeftBottom, isRightBotoom;
/**
* 需要设置圆角的部分
*
* @param leftTop 左上角
* @param rightTop 右上角
* @param leftBottom 左下角
* @param rightBottom 右下角
*/
public void setNeedCorner(boolean leftTop, boolean rightTop, boolean leftBottom, boolean rightBottom) {
isLeftTop = leftTop;
isRightTop = rightTop;
isLeftBottom = leftBottom;
isRightBotoom = rightBottom;
}
/**
* @param context 上下文
* @param radius 圆角幅度
*/
public CornerTransform(Context context, float radius) {
this.mBitmapPool = Glide.get(context).getBitmapPool();
this.radius = radius;
}
@NonNull
@Override
public Resource transform(@NonNull Context context, @NonNull Resource resource, int outWidth, int outHeight) {
Bitmap source = resource.get();
int finalWidth, finalHeight;
//输出目标的宽高或高宽比例
float scale;
if (outWidth > outHeight) {
//如果 输出宽度 > 输出高度 求高宽比
scale = (float) outHeight / (float) outWidth;
finalWidth = source.getWidth();
//固定原图宽度,求最终高度
finalHeight = (int) ((float) source.getWidth() * scale);
if (finalHeight > source.getHeight()) {
//如果 求出的最终高度 > 原图高度 求宽高比
scale = (float) outWidth / (float) outHeight;
finalHeight = source.getHeight();
//固定原图高度,求最终宽度
finalWidth = (int) ((float) source.getHeight() * scale);
}
} else if (outWidth < outHeight) {
//如果 输出宽度 < 输出高度 求宽高比
scale = (float) outWidth / (float) outHeight;
finalHeight = source.getHeight();
//固定原图高度,求最终宽度
finalWidth = (int) ((float) source.getHeight() * scale);
if (finalWidth > source.getWidth()) {
//如果 求出的最终宽度 > 原图宽度 求高宽比
scale = (float) outHeight / (float) outWidth;
finalWidth = source.getWidth();
finalHeight = (int) ((float) source.getWidth() * scale);
}
} else {
//如果 输出宽度=输出高度
finalHeight = source.getHeight();
finalWidth = finalHeight;
}
//修正圆角
this.radius *= (float) finalHeight / (float) outHeight;
Bitmap outBitmap = this.mBitmapPool.get(finalWidth, finalHeight, Bitmap.Config.ARGB_8888);
if (outBitmap == null) {
outBitmap = Bitmap.createBitmap(finalWidth, finalHeight, Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(outBitmap);
Paint paint = new Paint();
//关联画笔绘制的原图bitmap
BitmapShader shader = new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
//计算中心位置,进行偏移
int width = (source.getWidth() - finalWidth) / 2;
int height = (source.getHeight() - finalHeight) / 2;
if (width != 0 || height != 0) {
Matrix matrix = new Matrix();
matrix.setTranslate((float) (-width), (float) (-height));
shader.setLocalMatrix(matrix);
}
paint.setShader(shader);
paint.setAntiAlias(true);
RectF rectF = new RectF(0.0F, 0.0F, (float) canvas.getWidth(), (float) canvas.getHeight());
//先绘制圆角矩形
canvas.drawRoundRect(rectF, this.radius, this.radius, paint);
//左上角圆角
if (!isLeftTop) {
canvas.drawRect(0, 0, radius, radius, paint);
}
//右上角圆角
if (!isRightTop) {
canvas.drawRect(canvas.getWidth() - radius, 0, canvas.getWidth(), radius, paint);
}
//左下角圆角
if (!isLeftBottom) {
canvas.drawRect(0, canvas.getHeight() - radius, radius, canvas.getHeight(), paint);
}
//右下角圆角
if (!isRightBotoom) {
canvas.drawRect(canvas.getWidth() - radius, canvas.getHeight() - radius, canvas.getWidth(), canvas.getHeight(), paint);
}
return BitmapResource.obtain(outBitmap, this.mBitmapPool);
}
@Override
public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
}
}
然后,调用,设置需要圆角的方位即可,具体实现如下:
CornerTransform cornerTransform = new CornerTransform(mContext, DipPx.dip2px(mContext, 10));
cornerTransform.setNeedCorner(true, true, false, false);
RequestOptions options = new RequestOptions()
.placeholder(R.drawable.loading)
.transform(cornerTransform);
Glide.with(mContext).asBitmap()
.load(imageUrl)
.apply(options)
.into(myImageView);
转载自:https://blog.csdn.net/u011180852/article/details/119381412