Android and cut (remove) the shape from the bitmap

How do you cut a section from a bitmap ??? I want this section / form to be deleted. Leave it transparent instead of the section .. Let's say the shape is a square or a square.

enter image description here

enter image description here

+5
source share
3 answers

You must do this using the Porter-Duff color filter and Canvas:

public void punchHole(Bitmap bitmap, float cx, float cy, float radius) {
    Canvas c = new Canvas(bitmap);
    Paint paint = new Paint();
    paint.setColorFilter(new PorderDuffColorFilter(0, PorderDuff.Mode.CLEAR));
    c.drawCircle(cx, cy, radius, paint);
}

Strike>

Well, that was wrong. However, using the Porter-Duff transfer mode works:

public void punchHole(Bitmap bitmap, float cx, float cy, float radius) {
    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint();
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    canvas.drawCircle(cx, cy, radius, paint);
}

(Of course, the bitmap passed as an argument must be modifiable).

+10
source

Use the Bitmap.setPixel (x, y, Color) function to set the desired pixels to transparent.

eg:

Bitmap bmp = ...;
bmp.setPixel (100,100,Color.TRANSPARENT);

x/y 100 100. , ...

0

Have you tried to draw a circle with a transparent color, ARGB = 0,0,0,0?

0
source

All Articles