简体   繁体   English

Android canvas clipPath:使剪切区域透明

[英]Android canvas clipPath: make clipped areas transparent

I've created a custom view in Android which requires a curved edge which I've implemented using canvas.clipPath . 我在Android中创建了一个自定义视图,该视图需要使用canvas.clipPath实现的弯曲边缘。 This works with the exception that the clipped region shows a grey background colour instead of being transparent. 除了剪切区域显示灰色背景颜色而不是透明的以外,这是可行的。

在此处输入图片说明

In this image the right hand black colour is the content, which should show through the clipped region. 在此图像中,右边的黑色是内容,应通过剪切的区域显示出来。 The green is the view, and the grey to the immediate right of the view is the region which should be transparent, eg in this example the grey should be black. 绿色是视图,视图最右边的灰色是应该透明的区域,例如,在此示例中,灰色应该是黑色。

Here is my (Xamarin) code: 这是我的(Xamarin)代码:

public override void Draw(Canvas canvas)
{
    var path = new Path();
    path.AddRect(0,0,this.Width-100,this.Height, Path.Direction.Cw);
    path.AddArc(new RectF(this.Width-200, -100, this.Width, this.Height+100), 270, 180);
    canvas.ClipPath(path);
    base.Draw(canvas);
}

Is is possible to make the clipped regions of a canvas transparent? 是否可以使画布的裁剪区域透明?

(By the way I'm aware I shouldn't be instantiating objects in Draw , this is just for brevity) (通过我知道的方式,我不应该在Draw实例化对象,这只是为了简洁)

I was barking up the wrong tree, so would have been hard for anyone to answer without additional information! 我在树上叫错了树,所以如果没有其他信息,任何人都很难回答!

The leftmost grey was actually the correct background colour. 最左边的灰色实际上是正确的背景色。 The right most grey was a dimmed version of the true grey because the curved panel is actually a menu hidden in a drawer layout, so the default Android dimming was coming into play. 最右边的灰色是真灰色的变暗版本,因为弯曲的面板实际上是隐藏在抽屉布局中的菜单,因此默认的Android变暗正在发挥作用。

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

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