Android - 使用 canvas 在位图之外设置带有背景颜色的图像叠加层
Android - Set Image overlay with background color outside of bitmap using canvas
我正在处理相机 Activity 叠加层,我已经成功地在矩形外部创建了不透明颜色(不透明黑色)。而且我还成功地在大矩形内创建了一个小矩形。请看下图。如您所见,小矩形上有蓝色背景。问题是,我想在图像(位图)之外制作蓝色背景,而不是覆盖图像。问题是,如果我用透明填充更改图像,蓝色背景将全部覆盖。如何使蓝色背景仅覆盖图像的外部?我已经尝试从 google 中找到所有可能的答案,但对我来说并不走运,也许我开始时采用了错误的方法,需要建议。
这是我的代码
bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
Canvas osCanvas = new Canvas(bitmap);
RectF outerRectangle = new RectF(0, 0, getWidth(), getHeight());
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(getResources().getColor(R.color.opaque_black));
paint.setAlpha(99);
osCanvas.drawRect(outerRectangle, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));
paint.setColor(Color.TRANSPARENT);
paint.setStyle(Paint.Style.FILL);
RectF r1 = new RectF(
(float) (xStartingPoint),
(float) (yStartingPoint),
(float) (parentWidth),
(float) (parentHeight));
osCanvas.drawRoundRect(r1, (float) (cornerRadiusRatio * parentHeight),
(float) (cornerRadiusRatio * parentHeight), paint);
paint.setStrokeWidth(strokeWidth);
paint.setColor(getResources().getColor(R.color.colorDeepSky));
paint.setStyle(Paint.Style.STROKE);
RectF r2 = new RectF(
(float) (xStartingPoint),
(float) (yStartingPoint),
(float) (parentWidth),
(float) (parentHeight));
osCanvas.drawRoundRect(r2, (float) (cornerRadiusRatio * parentHeight),
(float) (cornerRadiusRatio * parentHeight), paint);
if (callbackMsg.equals(AppConstant.KTP_SELF)) {
paint.setColor(getResources().getColor(R.color.colorDeepSky));
drawRect(osCanvas, paint,
(float) (xChildStartingPoint),
(float) (yChildStartingPoint),
(float) (childWidth + xChildStartingPoint + 0.3 * childWidth),
(float) (childHeight + 0.7 * yChildStartingPoint));
}
RectF outerRectangle2 = new RectF(
(float) xChildStartingPoint,
(float) yChildStartingPoint,
(float) (childWidth + xChildStartingPoint + 0.3 * childWidth),
(float) (childHeight + 0.7 * yChildStartingPoint));
Paint paint2 = new Paint(Paint.ANTI_ALIAS_FLAG);
paint2.setColor(getResources().getColor(R.color.blue));
paint2.setAlpha(99);
osCanvas.drawRect(outerRectangle2, paint2);
Rect r3 = new Rect(
(int) (xChildStartingPoint),
(int) (yChildStartingPoint),
(int) (childWidth + xChildStartingPoint + 0.3 * childWidth),
(int) (childHeight + 0.7 * yChildStartingPoint));
paint2 = new Paint(Paint.ANTI_ALIAS_FLAG);
paint2.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));
paint2.setColor(Color.TRANSPARENT);
paint2.setStyle(Paint.Style.FILL);
Bitmap mbitmap = BitmapFactory.decodeResource(getResources(),R.drawable.icon_profile2);
osCanvas.drawBitmap(mbitmap, null, r3, paint2);
谢谢
好的,找到答案
我正在使用 PorterDuff
和 DST_OUT
模式
注意:
如果你想让图片背景透明,外面的区域填充颜色,使用DST_OUT
如果要使外部区域填充颜色
,请使用DST_IN
这是代码,我使用的是 Kotlin
var paint2 = Paint(Paint.ANTI_ALIAS_FLAG)
paint2.color = resources.getColor(R.color.opaque_black)
osCanvas.drawRect(outerRectangle2, paint2)
paint2 = Paint(Paint.ANTI_ALIAS_FLAG)
val r3 = Rect(
xChildStartingPoint.toInt(),
yChildStartingPoint.toInt(),
(childWidth + xChildStartingPoint + 0.3 * childWidth).toInt(),
(childHeight + 0.7 * yChildStartingPoint).toInt())
paint2.xfermode = PorterDuffXfermode(PorterDuff.Mode.DST_IN)
val mBitmap = BitmapFactory.decodeResource(resources, R.drawable.selfie_outline)
osCanvas.drawBitmap(mBitmap, null, r3, paint2)
我正在处理相机 Activity 叠加层,我已经成功地在矩形外部创建了不透明颜色(不透明黑色)。而且我还成功地在大矩形内创建了一个小矩形。请看下图。如您所见,小矩形上有蓝色背景。问题是,我想在图像(位图)之外制作蓝色背景,而不是覆盖图像。问题是,如果我用透明填充更改图像,蓝色背景将全部覆盖。如何使蓝色背景仅覆盖图像的外部?我已经尝试从 google 中找到所有可能的答案,但对我来说并不走运,也许我开始时采用了错误的方法,需要建议。
这是我的代码
bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
Canvas osCanvas = new Canvas(bitmap);
RectF outerRectangle = new RectF(0, 0, getWidth(), getHeight());
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(getResources().getColor(R.color.opaque_black));
paint.setAlpha(99);
osCanvas.drawRect(outerRectangle, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));
paint.setColor(Color.TRANSPARENT);
paint.setStyle(Paint.Style.FILL);
RectF r1 = new RectF(
(float) (xStartingPoint),
(float) (yStartingPoint),
(float) (parentWidth),
(float) (parentHeight));
osCanvas.drawRoundRect(r1, (float) (cornerRadiusRatio * parentHeight),
(float) (cornerRadiusRatio * parentHeight), paint);
paint.setStrokeWidth(strokeWidth);
paint.setColor(getResources().getColor(R.color.colorDeepSky));
paint.setStyle(Paint.Style.STROKE);
RectF r2 = new RectF(
(float) (xStartingPoint),
(float) (yStartingPoint),
(float) (parentWidth),
(float) (parentHeight));
osCanvas.drawRoundRect(r2, (float) (cornerRadiusRatio * parentHeight),
(float) (cornerRadiusRatio * parentHeight), paint);
if (callbackMsg.equals(AppConstant.KTP_SELF)) {
paint.setColor(getResources().getColor(R.color.colorDeepSky));
drawRect(osCanvas, paint,
(float) (xChildStartingPoint),
(float) (yChildStartingPoint),
(float) (childWidth + xChildStartingPoint + 0.3 * childWidth),
(float) (childHeight + 0.7 * yChildStartingPoint));
}
RectF outerRectangle2 = new RectF(
(float) xChildStartingPoint,
(float) yChildStartingPoint,
(float) (childWidth + xChildStartingPoint + 0.3 * childWidth),
(float) (childHeight + 0.7 * yChildStartingPoint));
Paint paint2 = new Paint(Paint.ANTI_ALIAS_FLAG);
paint2.setColor(getResources().getColor(R.color.blue));
paint2.setAlpha(99);
osCanvas.drawRect(outerRectangle2, paint2);
Rect r3 = new Rect(
(int) (xChildStartingPoint),
(int) (yChildStartingPoint),
(int) (childWidth + xChildStartingPoint + 0.3 * childWidth),
(int) (childHeight + 0.7 * yChildStartingPoint));
paint2 = new Paint(Paint.ANTI_ALIAS_FLAG);
paint2.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));
paint2.setColor(Color.TRANSPARENT);
paint2.setStyle(Paint.Style.FILL);
Bitmap mbitmap = BitmapFactory.decodeResource(getResources(),R.drawable.icon_profile2);
osCanvas.drawBitmap(mbitmap, null, r3, paint2);
谢谢
好的,找到答案
我正在使用 PorterDuff
和 DST_OUT
模式
注意: 如果你想让图片背景透明,外面的区域填充颜色,使用DST_OUT 如果要使外部区域填充颜色
,请使用DST_IN这是代码,我使用的是 Kotlin
var paint2 = Paint(Paint.ANTI_ALIAS_FLAG)
paint2.color = resources.getColor(R.color.opaque_black)
osCanvas.drawRect(outerRectangle2, paint2)
paint2 = Paint(Paint.ANTI_ALIAS_FLAG)
val r3 = Rect(
xChildStartingPoint.toInt(),
yChildStartingPoint.toInt(),
(childWidth + xChildStartingPoint + 0.3 * childWidth).toInt(),
(childHeight + 0.7 * yChildStartingPoint).toInt())
paint2.xfermode = PorterDuffXfermode(PorterDuff.Mode.DST_IN)
val mBitmap = BitmapFactory.decodeResource(resources, R.drawable.selfie_outline)
osCanvas.drawBitmap(mBitmap, null, r3, paint2)