8

I have two bitmaps and I create them in onCreate().

Bitmap bmp1 = BitmapFactory.decodeResource(getResources(),id);
Bitmap bmp2 = BitmapFactory.decodeResource(getResources(),id);

bmp1 and bmp2 are same. I modify bmp2 in my application. After my job is done, I click "Clear" button. I am trying to copy bmp1 (clean image) to bmp2(changed image) when I click "Clear" button. But I don't want to use createBitmap() or copy() function. Because these are create new Bitmap objects. I want to use only my two bitmaps (bmp1 and bmp2). How can I copy bmp1 to bmp2? I search google but everbody do this with createBitmap() or copy().

Thanks.

dec
  • 315
  • 2
  • 5
  • 15
  • How about `Canvas.drawBitmap()`? – Alan Jun 11 '13 at 13:05
  • @Alan, I draw my image with drawBitmap(). After I modified image (bmp2) I need clean image (bmp1). In my scenario bmp1 keeps always clean and bmp2 is always on screen. When I need clean image I copy bmp1 to bmp2. But how? – dec Jun 11 '13 at 13:13
  • If you are making photo editing app or alike, you can use `Canvas` on the bmp2, and make use of `Canvas.save()` and `Canvas.restore()` – Alan Jun 11 '13 at 13:22
  • Thank you @Alan. But I'm new on Android. Can you provide example. I use surfaceView for my image modification. How should I use it? – dec Jun 11 '13 at 13:27
  • This may help http://stackoverflow.com/questions/2618355/how-to-blit-in-android – Alan Jun 11 '13 at 13:54

1 Answers1

15

I solved my problem

First I created bmp1,bmp2 and canvas for bmp2:

bmp1 = BitmapFactory.decodeResource(cont.getResources(), R.drawable.image);
bmp2 = bmp1.copy(bmp1.getConfig(), true);
canvasBmp2 = new Canvas( bmp2 );

When I want to copy bmp1 to bmp2:

canvasBmp2.drawBitmap(bmp1, 0, 0, null);

@Override
protected void onDraw(Canvas canvas) 
{
    canvas.drawBitmap(bmp2, 0, 0, null);
}
dec
  • 315
  • 2
  • 5
  • 15