概述
一、 参考glide的实现方法
RequestOptions options = new RequestOptions().bitmapTransform(new RoundedCorners(30));//图片圆角为30
Glide.with(this).load(bitmap) //图片地址
.apply(options)
.into(iv_normal);
通过RoundedCorners的Transfromation进行圆角实现,在TransformationUtil中通过BitmapShader来设置Paint,然后canvas.drawRect来实现。
public static Bitmap roundedCorners(
@NonNull BitmapPool pool, @NonNull Bitmap inBitmap, int roundingRadius) {
Preconditions.checkArgument(roundingRadius > 0, "roundingRadius must be greater than 0.");
// 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());
BITMAP_DRAWABLE_LOCK.lock();
try {
Canvas canvas = new Canvas(result);
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
canvas.drawRoundRect(rect, roundingRadius, roundingRadius, paint);
clear(canvas);
} finally {
BITMAP_DRAWABLE_LOCK.unlock();
}
if (!toTransform.equals(inBitmap)) {
pool.put(toTransform);
}
return result;
}
二、项目中的实现方案
计算四个角的path来canvas.drawPath,这样实现起来不公麻烦,而且会影响黑暗模式的适配
public void initRightDownPath() {
rightDownPath = new Path();
rightDownPath.moveTo(width, height);
rightDownPath.lineTo(width, height - radius);
RectF rectF = new RectF(width - 2 * radius, height - 2 * radius, width, height);
rightDownPath.arcTo(rectF, 0, 90, true);
rightDownPath.lineTo(width, height);
rightDownPath.close();
}
canvas.drawPath(leftUpPath, paint);
canvas.drawPath(rightUpPath, paint);
最后
以上就是震动金针菇为你收集整理的android图片圆角,Android图片圆角的实现方案的全部内容,希望文章能够帮你解决android图片圆角,Android图片圆角的实现方案所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复