Game Development Reference
In-Depth Information
after bitmap. We should therefore always dispose of any Bitmap instance that we no longer need
via the following method:
Bitmap.recycle();
Drawing Bitmaps
Once we have loaded our bitmaps, we can draw them via the Canvas . The easiest method to do
this looks as follows:
Canvas.drawBitmap(Bitmap bitmap, float topLeftX, float topLeftY, Paint paint);
The first argument should be obvious. The arguments topLeftX and topLeftY specify the
coordinates on the screen where the top-left corner of the bitmap will be placed. The last
argument can be null . We could specify some very advanced drawing parameters with the
Paint , but we don't really need those.
There's another method that will come in handy, as well:
Canvas.drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint);
This method is super-awesome. It allows us to specify a portion of the Bitmap to draw via
the second parameter. The Rect class holds the top-left and bottom-right corner coordinates
of a rectangle. When we specify a portion of the Bitmap via the src , we do it in the Bitmap 's
coordinate system. If we specify null , the complete Bitmap will be used.
The third parameter defines where to draw the portion of the Bitmap , again in the form of a Rect
instance. This time, the corner coordinates are given in the coordinate system of the target of
the Canvas , though (either a View or another Bitmap ). The big surprise is that the two rectangles
do not have to be the same size. If we specify the destination rectangle to be smaller in size
than the source rectangle, then the Canvas will automatically scale for us. The same is true if
we specify a larger destination rectangle, of course. We'll usually set the last parameter to null
again. Note, however, that this scaling operation is very expensive. We should only use it when
absolutely necessary.
So, you might wonder: If we have Bitmap instances with different color formats, do we need
to convert them to some kind of standard format before we can draw them via a Canvas ? The
answer is no. The Canvas will do this for us automatically. Of course, it will be a bit faster if we
use color formats that are equal to the native framebuffer format. Usually we just ignore this.
Blending is also enabled by default, so if our images contain an alpha component per pixel, it is
actually interpreted.
Putting It All Together
With all of this information, we can finally load and render some Bobs. Listing 4-14 shows the
source of the BitmapTest activity that we wrote for demonstration purposes.
 
Search WWH ::




Custom Search