我正在尝试在自定义视图的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);
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));
注意:createBitamp和新画布应该移出onDraw方法。