Erase Bitmap Parts Using Porterduff Mode

Erase bitmap parts using PorterDuff mode

First thought, I'm not sure if setting alpha to 0 on your erase paint object is a good idea. That might make the whole thing ineffective.

Also, you should always use Bitmap.Config.ARGB_8888 if you're dealing with alphas.

If you're having trouble with the PorterDuff stuff, though, I would suggest simplifying your approach to ONLY do that (temporarily). That will help you narrow down the part which isn't working. Comment out everything to do with touch and view updates.

Then you can single out what part of the drawing isn't working right. Set up your constructor like this:

DrawView()
{
/* Create the background green bitmap */
...

/* Create foreground transparent bitmap */
...

/* Draw a blue circle on the foreground bitmap */
...

/* Apply the foreground to the background bitmap
using a PorterDuff method */
...
}

onDraw()
{
/* Simply draw the background bitmap */
...
}

If you set things up like that, you should be able to tell how your PD method is affecting the green bitmap, and change things accordingly.

Android Paint PorterDuff.Mode.CLEAR

The problem with the fingerpaint code is that what you see is not the same that is compressed into the png. Look at onDraw(). First you draw the screen white. Then you add the Bitmap. Because you used Porter Duff Clear the erased part of the bitmap contains actually transparent black pixels (value 0x00000000). But because you have the white background these black pixels show as white.

To fix this either change your save code to do the same thing as the draw code

 try {
fos = new FileOutputStream(file);
Bitmap saveBitmap = Bitmap.createBitmap(mBitmap);
Canvas c = new Canvas(saveBitmap);
c.drawColor(0xFFFFFFFF);
c.drawBitmap(mBitmap,0,0,null);
saveBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
saveBitmap.recycle();
...

or don't use PortDuff.Clear:

    case ERASE_MENU_ID:
mPaint.setColor(Color.WHITE);

PorterduffXfermode: Clear a section of a bitmap

The problem is hardware acceleration. Turn it OFF for the particular View that you are painting with CLEAR. If you're using a custom view, do this in the constructors:

if (android.os.Build.VERSION.SDK_INT >= 11) 
{
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}

You can also call setLayerType on a view reference.

Eraser with PorterDuff.Mode.CLEAR not working in canvas

So instead of having a layout on which i add my View. I put one parent layout that includes one layout and one imageview.
I put the background image on the imageview. I put an transparent picture on the child layout. and then bring the child layout to front and it is working now

Drawing on Canvas - PorterDuff.Mode.CLEAR draws black! Why?

PorterDuff.Mode.CLEAR doesn't work with hardware acceleration. Just set

view.setLayerType(View.LAYER_TYPE_SOFTWARE, null); 

Works perfectly for me.

Black rect PorterDuff.Mode.CLEAR in ViewGroup

i think what you are trying in @Override protected boolean drawChild is not a best thing, i would simply do the following:

@Override
protected void dispatchDraw(Canvas canvas) {
canvas.save(); // it saves the current clip, if not use CLIP_SAVE_FLAG
canvas.clipRect(childrenRect)
super.dispatchDraw(canvas); // draw your clipped children
canvas.restore(); // now no clip
draw_your_headers(canvas);
}

How to program for eraserPaint as on canvas in android?

I am not exactly aware of your code. but setColor works for me. But you can try one of the following code though not an exact solution but for your reference...

  1. http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/FingerPaint.html
  2. http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/TouchPaint.html
  3. Best one (as I suggest) : http://som-itsolutions.blogspot.com/2010/12/freeware-android-paint.html


Related Topics



Leave a reply



Submit