Skip to content Skip to sidebar Skip to footer

Setting The Opacity Of Drawing Canvas In Android

I am working on a function that drawing some transparent line on the canvas, the problem is , there is some 'ball shape' on the line as the screenshot show. Here is my code: canvas

Solution 1:

The ball shapes are where each line segment overlaps the previous one. You can fix this by using a second image overlaid on top of the image that you are editing.

Initialize the overlay image to completely transparent and make it the same size as the image you are editing.

ImageViewoverlayImageView= findViewById(R.id.overlay);
BitmapoverlayBitmap= Bitmap.createBitmap(imageWidth, imageHeight, Bitmap.Config.ARGB_8888);
overlayBitmap.erase(0x00000000); // transparent
overlayImageView.setImageBitmap(overlayBitmap);

Inside setAlpha() set the alpha of the overlay image to the alpha value.

overlayImageView.setImageAlpha((float)alpha / 255.0f);

When the user is drawing a line, in the case MotionEvent.ACTION_MOVE block, draw the line onto the overlay image instead, but at full opacity. Because all line segments are drawn at full opacity, there won't be any ball shapes where they overlap, but the line will still appear transparent because of the alpha value applied to the overlay image.

In case MotionEvent.ACTION_UP, transfer the line onto the image by drawing the overlay image onto the target image using canvas draw calls, using the alpha value set in setAlpha(), and then clear the overlay image to transparent.

Post a Comment for "Setting The Opacity Of Drawing Canvas In Android"