Java Android自定义视图不支持';t以正确的方式处理透明度/alpha
我正在绘制自定义视图。在这个视图中,我使用两个不同的paint和path对象来绘制画布。我基本上画了两个重叠的形状。添加alpha后,视图中重叠的部分比图像的其余部分更暗。这是不需要的,但我不知道如何修复它 这是我的代码剪辑,展示了我如何在NewButtonView.java中使用alphaJava Android自定义视图不支持';t以正确的方式处理透明度/alpha,java,android,android-custom-view,alpha,Java,Android,Android Custom View,Alpha,我正在绘制自定义视图。在这个视图中,我使用两个不同的paint和path对象来绘制画布。我基本上画了两个重叠的形状。添加alpha后,视图中重叠的部分比图像的其余部分更暗。这是不需要的,但我不知道如何修复它 这是我的代码剪辑,展示了我如何在NewButtonView.java中使用alpha Paint paint = new Paint(); int color = 0x33ffffff; int borderColor = 0xFF000000; paint.setColor(color)
Paint paint = new Paint();
int color = 0x33ffffff;
int borderColor = 0xFF000000;
paint.setColor(color);
paint.setAntiAlias(true);
paint.setStrokeWidth(strokeWidth);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStyle(Paint.Style.FILL);
大约31分钟后。。。它们显示了我想要的效果
它们基本上显示了以下图像:
添加透明度并获得此图像:意外结果
他们的结局是:期望的结果
有人知道如何获得这种期望的效果吗?如视频中所述,您可以使用
画布#saveLayerAlpha(…)
来实现这一点。不使用它也可以获得类似的效果。我以后再讨论这个问题
让我们创建一个示例视图:
public class SampleView extends View {
// Declare Paint objects
Paint paintColor, paintBorder;
public SampleView(Context context) {
super(context);
// Initialize and set up Paint objects
paintColor = new Paint();
paintBorder = new Paint();
paintColor.setAntiAlias(true);
paintBorder.setAntiAlias(true);
paintBorder.setColor(Color.BLACK);
paintBorder.setStyle(Style.STROKE);
paintBorder.setStrokeWidth(10);
// Just a random image to 'see' the difference
setBackground(getResources().getDrawable(R.drawable.hor_lines));
}
@Override
protected void onDraw(Canvas canvas) {
// Save layer alpha for Rect that covers the view : alpha is 90 / 255
canvas.saveLayerAlpha(0, 0, getWidth(), getHeight(), 90,
Canvas.HAS_ALPHA_LAYER_SAVE_FLAG);
// Draw first circle, and then the border
paintColor.setColor(Color.RED);
canvas.drawCircle(getWidth() / 3, getHeight() / 2,
getWidth() / 4 - 20, paintColor);
canvas.drawCircle(getWidth() / 3, getHeight() / 2,
getWidth() / 4 - 15, paintBorder);
// Draw second circle, and then the border
paintColor.setColor(Color.BLUE);
canvas.drawCircle(2 * getWidth() / 3, getHeight() / 2,
getWidth() / 4 - 20, paintColor);
canvas.drawCircle(2 * getWidth() / 3, getHeight() / 2,
getWidth() / 4 - 15, paintBorder);
// Finally, restore the canvas
canvas.restore();
}
}
发生了什么:
saveLayerAlpha(..)
时,会分配屏幕外位图canvas.restore()
时,此位图将传输到屏幕上的画布,并且我们在saveLayerAlpha(..)
中提供的alpha值将应用到屏幕外位图saveLayerAlpha(..)
:
输出:
仅供参考,上图中的基本容器是背景设置为以下jpeg:LinearLayout
以及用作SampleView背景的可绘制表格:
// Just a random image to 'see' the difference
setBackground(getResources().getDrawable(R.drawable.hor_lines));
取自:。您可以使用完整alpha在位图中绘制所有内容,然后更改位图的alpha
(很抱歉,这与其说是答案,不如说是评论,但堆栈溢出不允许我发表评论)您确定要更改的是alpha吗?看起来你想要的效果仍然是零透明度,只是变亮了。是的,我需要透明度,因为我使用的是纹理背景,必须要看到。我最好的想法是看看是否有办法将重叠的形状绘制为一个形状(或在绘制后将它们合并为一个)。将alpha添加到合成形状中应该会产生你想要的效果是的,只是我画了两个圆圈,彼此重叠,当我只能画两个圆圈时,画那种类型的“双目形状”似乎是不必要的。我可能画错了树,但是你不应该在容器上设置alpha而不是按钮吗?你也可以创建一个100%不透明度的cavanas绘制图像,然后降低cavanas不透明度。@MarcosEusebi你将如何更改画布的透明度/不透明度?对不起,我不介意。将图像连接到位图中,然后将位图绘制到cavnas并设置位图的不透明度。注意,文档建议使用硬件层作为视图,而不是saveLayer*方法。
// Just a random image to 'see' the difference
setBackground(getResources().getDrawable(R.drawable.hor_lines));