Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make certain area of bitmap transparent on touch --> again

I'm trying to achieve the same results as per thread: Make certain area of bitmap transparent on touch. I'm stick to code presented in this answer: Lumis answer and acording to SteD this solution should work.

Unfortunately is not working for me (and also for another user: Make certain area of bitmap transparent on touch doesn't works, it's draw a black circle), I'm just getting black circle. I tried many things but not get this solve. Make background transparent as per suggestion from second thread do not make any difference.

After many experiments I found that transparency is working, when I'm set this

android:theme="@android:style/Theme.Translucent"

in my AndroidManifest.xml I can see everything under my application i.e. desktop. I went through code many times and cant see obvious mistake, only reason I thinking is cause this is Z order, but bitmaps and canvas do not maintenance z orders. Z ordering is done by drawing in certain order (which is correct in this code).

Is this some strange example of optimisation in android code, or I'm missing something in android manifest file?

like image 515
user1370623 Avatar asked May 02 '12 17:05

user1370623


2 Answers

Finally I found solution that's working:

@Override
public void onDraw(Canvas canvas){
    super.onDraw(canvas);

    //draw background
    canvas.drawBitmap(bgr, 0, 150, null);
    //copy the default overlay into temporary overlay and punch a hole in it                          
    c2.drawBitmap(overlayDefault, 0, 0, null); //exclude this line to show all as you draw
    c2.drawCircle(X, Y, 80, pTouch);
    //draw the overlay over the background
    //canvas.drawBitmap(overlay, 0, 0, null);

    Paint new_paint = new Paint(/*Paint.ANTI_ALIAS_FLAG*/);
    new_paint.setXfermode(new PorterDuffXfermode(Mode.SRC_ATOP));
    canvas.drawBitmap(overlay, 0, 0, new_paint);
}

But I don't understand why is working. I was investigate xfermodes with this source code: android Xfermodes demo and according to this image: Xfermodes image

It not make any sense, apart that xfermode make sure that overlay bitmap is bean drawn second (Z order) and transparency came to play. But Z order should be maintained by drawing order.

If somebody have better idea how to solve this, please share your knowledge.

Regards.

like image 187
user1370623 Avatar answered Nov 15 '22 09:11

user1370623


you can edit onDraw() method:

public void onDraw(Canvas canvas){
        super.onDraw(canvas);
        canvas.drawColor(Color.TRANSPARENT);
        canvas.drawBitmap(bgr, 0, 0, null);

        c2.drawCircle(X, Y, 10, pTouch);
        Paint new_paint = new Paint(/*Paint.ANTI_ALIAS_FLAG*/);
        new_paint.setXfermode(new PorterDuffXfermode(Mode.SRC_ATOP));
        canvas.drawBitmap(overlay, 0, 0, new_paint);
    }

I tried and it worked ! Hope this solution helps you

like image 27
Robert Stv Avatar answered Nov 15 '22 10:11

Robert Stv