Putting It All Together

With all this information, we can finally load and render some Bobs. Listing 4-14 shows you the source of the BitmapTest activity I wrote for demonstration purposes.

Listing 4-14. The BitmapTest Activity package com.badlogic.androidgames;

import java.io.IOException; import java.io.InputStream;

import android.app.Activity; import android.content.Context; import android.content.res.AssetManager; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Rect; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.Window; import android.view.WindowManager;

public class BitmapTest extends Activity { class RenderView extends View { Bitmap bob565; Bitmap bob4444; Rect dst = new Rect();

public RenderView(Context context) { super(context);

AssetManager assetManager = context.getAssets();

InputStream inputStream = assetManager.open("bobrgb888.png");

bob565 = BitmapFactory.decodeStream(inputStream);

inputStream.close();

Log.d("BitmapText",

"bobrgb888.png format: " + bob565.getConfig());

inputStream = assetManager.open("bobargb8888.png"); BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferredConfig = Bitmap.Config.ARGB_4444; bob4444 = BitmapFactory

.decodeStream(inputStream, null, options); inputStream.close(); Log.d("BitmapText",

"bobargb8888.png format: " + bob4444.getConfig());

// silently ignored, bad coder monkey, baaad! } finally {

// we should really close our input streams here.

protected void onDraw(Canvas canvas) { dst.set(50, 50, 350, 350); canvas.drawBitmap(bob565, null, dst, null); canvas.drawBitmap(bob4444, 100, 100, null); invalidate();

^Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedlnstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(windowManager.LayoutParams.FLAG_FULLSCREEN,

WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(new RenderView(this));

The onCreate() method of our activity is old hat, so let's move on to our custom View.

It has two Bitmap members, one storing an image of Bob (introduced in Chapter 3) in RGB565 format, and another storing Bob in ARGB4444 format. We also have a Rect member where we store the destination rectangle for rendering.

In the constructor of the RenderView class, we first load Bob into the bob565 member of the View. Note that the image is loaded from an RGB888 PNG file, and that the BitmapFactory will automatically convert this to an RGB565 image. To prove this, we also output the Bitmap.Config of the Bitmap to LogCat. The RGB888 version of Bob has an opaque white background, so no blending needs to be performed.

Next we load Bob from an ARGB8888 PNG file stored in the assets/ directory. To save some memory, we also tell the BitmapFactory to convert this image of Bob to an ARGB4444 bitmap. The factory may not obey this request (for unknown reasons). To see whether it was nice to us, we ouput the Bitmap.Config file of this Bitmap to LogCat as well.

The onDraw() method is puny. All we do is draw bob565 scaled to 250x250 pixels (from his original size of 160x183 pixels) and draw bob4444 on top of him, unscaled but blended (which is done automagically by the Canvas). Figure 4-14 shows you the two Bobs in all their glory.

Figure 4-14. Two Bobs on top of each other (at 480x800-pixel resolution)

LogCat reports that bob565 indeed has the color format Config.RGB_565, and that bob4444 was converted to Config.ARGB_4444. The BitmapFactory did not fail us!

Here are some things you should take away from this section:

Use the minimum color format you can get away with to conserve memory. This might, however, come at the price of less visual quality and slightly reduced rendering speed.

Unless absolutely necessary, refrain from drawing bitmaps scaled. If you know their scaled size, prescale them offline or during loading time.

Always make sure you call the Bitmap.recycle() method if you no longer need a Bitmap. Otherwise you'll get some memory leaks or run low on memory.

Using LogCat all this time for text output is a bit tedious. Let's see how we can render text via the Canvas.

NOTE: As with other classes, there's more to Bitmap than what I could describe in this couple of pages. I covered the bare minimum we need to write Mr. Nom. If you want more, check out the documentation on the Android Developers site.

0 0

Post a comment