Drawing holes on canvas

I am trying to create such a form in the onDraw method of a custom view.

Unfortunately, I cannot “cut” a transparent circle on the canvas (by drawing a circle using Color.Transparent).

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

Custom shape

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: I got the exact shape I wanted using Color.WHITE: Result achieved with Color.WHITE

Decision

 @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)); 

Note: createBitamp and the new canvas must be moved from the onDraw method.

+7
android android canvas
source share
1 answer

There are questions that may be identified for your situation:

draw a path with a hole (android)

Android Canvas - Draw a Hole

Punch a hole in the overlay of a rectangle with acceleration enabled HW in the view

http://www.mail-archive.com/ android-developers@googlegroups.com /msg232764.html

+9
source share

All Articles