我试图在自定义视图的onDraw方法中绘制这样的形状.
不幸的是,我无法"剪切"画布上的透明圆圈(通过绘制带有Color.Transparent的圆圈).
我应该先在另一个位图中绘制形状,然后在onDraw提供的画布上绘制它吗?或者这是一种更好(更简单)的方法吗?

这是我尝试的代码(与Color.WHITE一起使用):
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setColor(Color.TRANSPARENT);
mPaint.setStrokeWidth(4);
mPaint.setStyle(Style.STROKE);
canvas.drawColor(getResources().getColor(R.color.black_overlay));
canvas.drawCircle(-3*(this.getBottom()-this.getTop())/4, (this.getTop()+this.getBottom())/2, this.getBottom()-this.getTop(), mPaint);
Run Code Online (Sandbox Code Playgroud)
PS:使用Color.WHITE时,我得到了我想要的确切形状:

解
@Override
public void onDraw(Canvas canvas)
{
mBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
mCanvas.drawColor(getResources().getColor(R.color.black_overlay));
mCanvas.drawCircle(-3*(getHeight())/4, (getHeight())/2, getHeight(), mPaint);
canvas.drawBitmap(mBitmap, 0, 0, null);
}
with
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setStrokeWidth(4);
mPaint.setStyle(Style.STROKE);
mPaint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
Run Code Online (Sandbox Code Playgroud)
注意:createBitamp和新画布应该移出onDraw方法.
我写了这个小程序来说明我的问题:
int main(int argc, char* argv[])
{
int i = 0;
while(1)
{
std::cout << i++ << std::endl;
Sleep(1000);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
因此,如果您按住垂直滚动条(查看日志或其他内容......),这个简单的程序将停止计数.
有办法避免这种情况吗?
干杯