Android Canvas Clear With Transparency


Answer :

Use the following.

 canvas.drawColor(Color.TRANSPARENT,Mode.CLEAR); 

The solution was to create a secondary canvas and bitmap to draw on. My Custom View's onSizeChanged() method looked like

@Override protected void onSizeChanged(int w, int h, int oldw, int oldh) {     super.onSizeChanged(w, h, oldw, oldh);      bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);     bitmap.eraseColor(Color.TRANSPARENT);     temp = new Canvas(bitmap); } 

and the onDrawMethod looks like

@Override protected void onDraw(Canvas canvas) {     super.onDraw(canvas);      temp.drawColor(Color.argb(80, 0, 0, 0));     temp.drawCircle(centerPosX, centerPosY, 200, transparentPaint);     canvas.drawBitmap(bitmap, 0, 0, null); } 

where transparentPaint is declared in the onstructor as

transparentPaint = new Paint(); transparentPaint.setColor(getResources().getColor(android.R.color.transparent)); transparentPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); transparentPaint.setAntiAlias(true); 

Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android How Can I Convert A String To A Editable