How to blit() in android?

o0'. picture o0'. · Apr 11, 2010 · Viewed 8.7k times · Source

I'm used to handle graphics with old-school libraries (allegro, GD, pygame), where if I want to copy a part of a bitmap into another... I just use blit.

I'm trying to figure out how to do that in android, and I got very confused. So... we have these Canvas that are write-only, and Bitmaps that are read-only? It seems too stupid to be real, there must be something I'm missing, but I really can't figure it out.

edit: to be more precise... if bitmaps are read only, and canvas are write only, I can't blit A into B, and then B into C?

Answer

Steve Haley picture Steve Haley · Apr 21, 2010

The code to copy one bitmap into another is like this:

Rect src = new Rect(0, 0, 50, 50); 
Rect dst = new Rect(50, 50, 200, 200);  
canvas.drawBitmap(originalBitmap, src, dst, null);

That specifies that you want to copy the top left corner (50x50) of a bitmap, and then stretch that into a 150x150 Bitmap and write it 50px offset from the top left corner of your canvas.

You can trigger drawing via invalidate() but I recommend using a SurfaceView if you're doing animation. The problem with invalidate is that it only draws once the thread goes idle, so you can't use it in a loop - it would only draw the last frame. Here are some links to other questions I've answered about graphics, they might be of use to explain what I mean.


In response to the comments, here is more information: If you get the Canvas from a SurfaceHolder.lockCanvas() then I don't think you can copy the residual data that was in it into a Bitmap. But that's not what that control is for - you only use than when you've sorted everything out and you're ready to draw.

What you want to do is create a canvas that draws into a bitmap using

Canvas canvas = new Canvas(yourBitmap)

You can then do whatever transformations and drawing ops you want. yourBitmap will contain all the newest information. Then you use the surface holder like so:

Canvas someOtherCanvas = surfaceHolder.lockCanvas()
someOtherCanvas.drawBitmap(yourBitmap, ....)

That way you've always got yourBitmap which has whatever information in it you're trying to preserve.