Search code examples
androidpaintporter-duff

Android PorterDuff.Mode.CLEAR not working correctly when background is BLACK


I need a little help with the application that I'm working. I'm trying to create an application for painting and there is one problem which I noticed a few days ago and now I decide to make some research to solve it. When I use PorterDuff.Mode.CLEAR to use my brush as eraser it's working as it should be while my background is white. But if I set my background in other color ( for example Black like : mCanvas.drawColor(Color.BLACK); ) and after I use the eraser, the places where I use the eraser are painted with white color. Here is how I'm setting the variables for my eraser :

erase.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            stamp=false;
            MODE = ERASER_MODE;
            mPaint.setColorFilter(null);
            mPaint.setShader(null);
            mPaint.setMaskFilter(null);
            mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        }
    });

So any ideas why it's happening and how can I fix that problem?

Thanks in advance!


Solution

  • Actually I find where were my problem. In my Custom View that I was using for drawing and etc I was doing this

     @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            super.onSizeChanged(w, h, oldw, oldh);
            mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
            mCanvas = new Canvas(mBitmap);
            mCanvas.drawColor(parentColor); //parentColor = currentBackgroundColor
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            canvas.drawColor(Color.WHITE);
            canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
            canvas.drawPath(mPath, mPaint);
        }
    

    and I had to change only on my