PorterDuff.Mode.CLEAR in canvas not working for 5.0 or above version : Android

559 Views Asked by At

i am using below code for clear part of image,

Paint round_brush;

class TouchView extends View
{

    Paint pTouch;
    int X = -100;
    int Y = -100;
    Canvas c2;

    public TouchView(Context context, Bitmap background_bitmap_, Bitmap foreground_bitmap_)
    {
        super(context);

        setFocusable(true);
        setFocusableInTouchMode(true);

        background_bitmap = null;
        foreground_bitmap = null;

        background_bitmap = background_bitmap_;

        foreground_bitmap = foreground_bitmap_.copy(Bitmap.Config.ARGB_8888, true);

        c2 = new Canvas(foreground_bitmap);

        pTouch = new Paint(); //Paint.ANTI_ALIAS_FLAG
        pTouch.setAlpha(0xFF);
        pTouch.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        pTouch.setColor(Color.TRANSPARENT);
        //pTouch.setMaskFilter(new BlurMaskFilter(15, Blur.NORMAL));

        setLayerType(View.LAYER_TYPE_SOFTWARE, null);

        round_brush = new Paint();
        // smooths out the edges of what is being drawn
        round_brush.setAntiAlias(true);
        // set color 
        round_brush.setColor(Color.GRAY);
        // set style
        round_brush.setStyle(Paint.Style.STROKE);
        // set stroke
        round_brush.setStrokeWidth(4);

    }

    @Override
    public boolean onTouchEvent(MotionEvent ev)
    {

        switch (ev.getAction())
        {

            case MotionEvent.ACTION_DOWN:
            {

                X = (int) ev.getX();
                Y = (int) ev.getY();

                invalidate();

                break;
            }

            case MotionEvent.ACTION_MOVE:
            {

                X = (int) ev.getX();
                Y = (int) ev.getY();

                invalidate();
                break;

            }

            case MotionEvent.ACTION_UP:

                break;

        }
        return true;
    }

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

        //draw background
        canvas.drawBitmap(background_bitmap, new Matrix(), null);

        //cropping part of above image
        c2.drawCircle(X, Y, brushSize, pTouch);

        canvas.drawCircle(X, Y, brushSize, round_brush);

        //draw the overlay over the background
        canvas.drawBitmap(foreground_bitmap, new Matrix(), null);

    }

}

while i test on 4.4 or lower device it works as expected and everything is fine ! but on 5.0 or above device it draws black circle ! why? what is wrong? is any special condition for 5.0 or above device?

My test device result,

  1. Moto E (4.4) OK
  2. Moto E (5.0.2) Fail
  3. Samsung(4.1.2) OK
  4. Yureka (5.0.2) Fail
0

There are 0 best solutions below