Glide加载圆形图
Glide.with(context)
.load(url)
.apply(RequestOptions.bitmapTransform(new CircleCrop()))
.into(view);
Glide加载圆角
Glide.with(context)
.load(url)
.apply(RequestOptions.bitmapTransform(new RoundedCorners(圆角度数)))
.into(view);
Bitmap 修改为圆形图
原理:解析bitmap 重新绘制
public Bitmap toRoundBitmap(Bitmap bitmap) {
bitmap = Bitmap.createScaledBitmap(bitmap, 400, 400, true);
Bitmap bm = Bitmap.createBitmap(400, 400, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bm);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
// 这里需要先画出一个圆
canvas.drawCircle(200, 200, 200, paint);
// 圆画好之后将画笔重置一下
paint.reset();
// 设置图像合成模式,该模式为只在源图像和目标图像相交的地方绘制源图像
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, 0, 0, paint);
return bm;
}
dp值转换成代码中的int值
int v1 = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpValue, getResources().getDisplayMetrics());
Android 中 图片的拼接思路
public static Bitmap toConformBitmap(Bitmap head, Bitmap body, Bitmap san) {
if (head == null) {
return null;
}
int headWidth = head.getWidth();
int bodywidth = body.getWidth();
int fotwid = san.getWidth();
int headHeight = head.getHeight();
int bodyheight = body.getHeight();
int footerheight = san.getHeight();
//生成三个图片合并大小的Bitmap
Bitmap newbmp = Bitmap.createBitmap(bodywidth, headHeight + bodyheight + footerheight, Bitmap.Config.ARGB_8888);
Canvas cv = new Canvas(newbmp);
cv.drawBitmap(head, 0, 0, null);// 在 0,0坐标开始画入headBitmap
//因为手机不同图片的大小的可能小了 就绘制白色的界面填充剩下的界面
if (headWidth < bodywidth) {
Bitmap ne = Bitmap.createBitmap(kebianwidth - headWidth, headHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(ne);
canvas.drawColor(Color.WHITE);
cv.drawBitmap(ne, headWidth, 0, null);
}
cv.drawBitmap(body, 0, headHeight, null);// 在 0,headHeight坐标开始填充课表的Bitmap
cv.drawBitmap(san, 0, headHeight + bodyheight, null);// 在 0,headHeight +bodyheight坐标开始填充课表的Bitmap
//因为手机不同图片的大小的可能小了 就绘制白色的界面填充剩下的界面
if (fotwid < bodywidth) {
Bitmap ne = Bitmap.createBitmap(bodywidth - fotwid, footerheight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(ne);
canvas.drawColor(Color.WHITE);
cv.drawBitmap(ne, fotwid, headHeight + bodyheight, null);
}
cv.save(Canvas.ALL_SAVE_FLAG);// 保存
cv.restore();// 存储
//回收
head.recycle();
body.recycle();
san.recycle();
return newbmp;
}
利用Glide 分别设置四个角为圆角
public class CornerTransform extends BitmapTransformation {
private static final String ID = "com.bumptech.glide.load.resource.bitmap.RoundedCorners";
private static final byte[] ID_BYTES = ID.getBytes(CHARSET);
private final int mRightTop;
private final int mRightBottom;
private final int mLeftTop;
private final int mLeftBottom;
private int radius;
public CornerTransform(int mRightTop, int mRightBottom, int mLeftTop, int mLeftBottom) {
this.mRightTop = mRightTop;
this.mRightBottom = mRightBottom;
this.mLeftTop = mLeftTop;
this.mLeftBottom = mLeftBottom;
this.radius = (int) (mRightTop + mRightBottom + mLeftBottom + mLeftTop) / 4;
}
@Override
protected Bitmap transform(@NonNull @NotNull BitmapPool pool, @NonNull @NotNull Bitmap toTransform, int outWidth, int outHeight) {
return roundedCorners(pool, toTransform, radius);
}
public Bitmap roundedCorners(@NonNull BitmapPool pool, @NonNull Bitmap inBitmap, int roundingRadius) {
// Alpha is required for this transformation.
Bitmap.Config safeConfig = getAlphaSafeConfig(inBitmap);
Bitmap toTransform = getAlphaSafeBitmap(pool, inBitmap);
Bitmap result = pool.get(toTransform.getWidth(), toTransform.getHeight(), safeConfig);
result.setHasAlpha(true);
BitmapShader shader = new BitmapShader(toTransform, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setShader(shader);
RectF rect = new RectF(0, 0, result.getWidth(), result.getHeight() );
Canvas canvas = new Canvas(result);
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
canvas.drawRoundRect(rect, roundingRadius, roundingRadius, paint);
//左上角
canvas.drawRoundRect(new RectF(0, 0, mLeftTop, mLeftTop), mLeftTop, mLeftTop, paint);
//右上角
canvas.drawRoundRect(new RectF(canvas.getWidth() - mRightTop, 0, canvas.getWidth(), mRightTop),mRightTop,mRightTop, paint);
//左下角
canvas.drawRoundRect(new RectF(0, canvas.getHeight() - mLeftBottom, mLeftBottom, canvas.getHeight()),mLeftBottom,mLeftBottom, paint);
//右下角
canvas.drawRoundRect(new RectF(canvas.getWidth() - mRightBottom, canvas.getHeight() - mRightBottom, canvas.getWidth(), canvas.getHeight()),mRightBottom,mRightBottom, paint);
canvas.setBitmap(null);
if (!toTransform.equals(inBitmap)) {
pool.put(toTransform);
}
return result;
}
private static Bitmap.Config getAlphaSafeConfig(@NonNull Bitmap inBitmap) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Avoid short circuiting the sdk check.
if (Bitmap.Config.RGBA_F16.equals(inBitmap.getConfig())) { // NOPMD
return Bitmap.Config.RGBA_F16;
}
}
return Bitmap.Config.ARGB_8888;
}
private static Bitmap getAlphaSafeBitmap(@NonNull BitmapPool pool, @NonNull Bitmap maybeAlphaSafe) {
Bitmap.Config safeConfig = getAlphaSafeConfig(maybeAlphaSafe);
if (safeConfig.equals(maybeAlphaSafe.getConfig())) {
return maybeAlphaSafe;
}
Bitmap argbBitmap = pool.get(maybeAlphaSafe.getWidth(), maybeAlphaSafe.getHeight(), safeConfig);
new Canvas(argbBitmap).drawBitmap(maybeAlphaSafe, 0 /*left*/, 0 /*top*/, null /*paint*/);
return argbBitmap;
}
@Override
public void updateDiskCacheKey(@NonNull @NotNull MessageDigest messageDigest) {
messageDigest.update(ID_BYTES);
byte[] radiusData = ByteBuffer.allocate(4).putInt(radius).array();
messageDigest.update(radiusData);
}
}
Glide.with(context)
.load(url)
.apply(RequestOptions.bitmapTransform(
new CornerTransform(右上,右下,左上,左下)))
.into(view);