本文介绍了绘图和QUOT;孔"在一个画布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我想得出这样的形状,在自定义视图的OnDraw的方法。

I'm trying to draw a shape like this in the onDraw method of a custom view.

不幸的是,我不能一刀切画布上的透明圈,(通过绘制一个Color.Transparent一个圆圈)。

Unfortunately, I cannot "cut" the transparent circle on the canvas, (by drawing a circle with a Color.Transparent).

我应该先绘制形状的另一个位图,然后绘制它的OnDraw所提供的画布上?抑或是一个更好的(简单)的方式来做到这一点?

Should I first draw the shape in another bitmap then draw it on the canvas provided by onDraw ? Or is it a better (simpler) way to do this ?

下面是code我想(适用于Color.WHITE):

Here is the code I tried (works with 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的准确形状:

PS : I got the exact shape I wanted while using 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方法

Note : the createBitamp and the new canvas should be moved out of the onDraw Method.

推荐答案

有可revealnt您的具体情况的问题:

There are questions which may be revealnt for your situation :

逼平孔路径(安卓)

Android的画布上 - 画一个洞

Punch在一个矩形覆盖带有硬件加速上的孔查看启用

http://www.mail-archive.com/[email protected]/msg232764.html

这篇关于绘图和QUOT;孔"在一个画布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 02:20