简体   繁体   English

如何在android画布中绘制具有透明黑白背景的矩形

[英]How to draw rectangle with transparent black and white background in android canvas

How can I achieve a rectangle having black and white transparent background.如何实现具有黑白透明背景的矩形。 I want something like this https://i.stack.imgur.com/krsPI.jpg我想要这样的东西https://i.stack.imgur.com/krsPI.jpg

Please help me to achieve same result on Android SDK.请帮助我在 Android SDK 上实现相同的结果。 this is my function which creates rectangle using canvas.这是我使用画布创建矩形的函数。

private void drawRectangle() {
    Bitmap bitmap = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint();
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(Color.parseColor("#D20E0F02"));
    canvas.drawRect(400, 180, 80, 350, paint);
}

Android introduced BlendMode s in API 29, which is a pretty easy way to apply a zero-saturation colour to a Paint and use that to desaturate an image: Android 在 API 29 中引入了BlendMode ,这是一种将零饱和度颜色应用于Paint并使用它来降低图像饱和度的非常简单的方法:

Paint paint = new Paint();
paint.setBlendMode(BlendMode.SATURATION);
// just for illustration - it's black by default anyway (zero saturation)
paint.setColor(Color.GRAY);

canvas.drawRect(rect, paint);

中间有一个去饱和正方形的图像


Unfortunately, as far as I can tell, if you want compatibility with anything before API 29 you're out of luck - there's a bunch of BlendModeCompat classes but they fall back to PorterDuff blending on older APIs, and that can't do saturation, so it straight up won't work and you'll get a compatibility warning even if you use BlendModeCompat.SATURATION .不幸的是,据我所知,如果你想与 API 29 之前的任何东西兼容,那你就不走运了 - 有一堆BlendModeCompat类,但它们会退回到 PorterDuff 混合较旧的 API,并且不能饱和,所以它直接不起作用,即使您使用BlendModeCompat.SATURATION也会收到兼容性警告。

So instead, you can set a ColorMatrixColorFilter on your Paint , using a ColorMatrix with its saturation set to zero.因此,您可以在Paint上设置一个ColorMatrixColorFilter ,使用饱和度设置为零的ColorMatrix This isn't a blending mode, it just affects the look of the thing you're drawing - so you basically have to redraw part of the bitmap using this paint:这不是混合模式,它只会影响您正在绘制的东西的外观 - 所以您基本上必须使用此绘制重绘位图的一部分:

ColorMatrix matrix = new ColorMatrix();
matrix.setSaturation(0f);
paint.setColorFilter(new ColorMatrixColorFilter(matrix));

// I don't know how you're getting your bitmaps, but here's an example of pulling one
// from an ImageView, drawing on it, and setting it on the ImageView again
Bitmap bitmap = imageView.drawable.toBitmap()
Canvas canvas = new Canvas(bitmap);
// using the same rect so we're copying and pasting the exact same region
canvas.drawBitmap(bitmap, rect, rect, paint);
image.setImageBitmap(bitmap);

另一个中间有去饱和区域的位图

I don't know if there are any better options available (there are definitely some more complicated ones!) but hopefully that gives you some ideas about how you can adapt it to what you're doing.我不知道是否有更好的选择(肯定有一些更复杂的!),但希望这能给你一些关于如何适应你正在做的事情的想法。 Also you'll need another Paint to draw that red border around the rect afterwards!之后,您还需要另一个Paint在矩形周围绘制红色边框!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM