วันพฤหัสบดีที่ 2 มกราคม พ.ศ. 2557

Android Graphics : Drawing Bitmaps


Android Graphics : Drawing Bitmaps

Using Bitmaps
While making a game with basic shapes such as lines or circles is a possibility, it’s not exactly sexy. We want an awesome artist to create sprites and backgrounds and all that jazz for us, which we can then load from PNG or JPEG files. Doing this on Android is extremely easy.

Loading and Examining Bitmaps
The Bitmap class will become our best friend. We load a bitmap from a file by using the BitmapFactory singleton. As we store our images in the form of assets, let’s see how we can load an image from the assets/ directory:

InputStream inputStream = assetManager.open("android.png");
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);

The Bitmap class itself has a couple of methods that are of interest to us. First we want
to get to know its width and height in pixels:

int width = bitmap.getWidth();
int height = bitmap.getHeight();

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.

Enum Values
Bitmap.Config ALPHA_8 Each pixel is stored as a single translucency (alpha) channel. 
Bitmap.Config ARGB_4444 This field was deprecated in API level 13. Because of the poor quality of this configuration, it is advised to use ARGB_8888 instead.  
Bitmap.Config ARGB_8888 Each pixel is stored on 4 bytes. 
Bitmap.Config RGB_565 Each pixel is stored on 2 bytes and only the RGB channels are encoded: red is stored with 5 bits of precision (32 possible values), green is stored with 6 bits of precision (64 possible values) and blue is stored with 5 bits of precision. 
Original on developer.android.com
http://developer.android.com/reference/android/graphics/Bitmap.Config.html

DrawingBitmapsActivity.java sourcecode

package android.example.drawingbitmap;

import java.io.IOException;
import java.io.InputStream;
import android.os.Bundle;
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.view.View;
import android.view.Window;
import android.view.WindowManager;

public class DrawingBitmapActivity extends Activity {
     
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(new RenderView(this));      
    }

    class RenderView extends View {
      Bitmap cow_565;
      Bitmap android_8888;
      Bitmap icon;
      Rect dst = new Rect();
   
      public RenderView(Context context) {
            super(context);
     
            try {
                  // Read from res/assets/***.png
                  AssetManager assetManager = context.getAssets();
                 
                  InputStream inputStream = assetManager.open("cow.png");
                  cow_565 = BitmapFactory.decodeStream(inputStream);
                  inputStream.close();
                             
                  inputStream = assetManager.open("android_logo.png");
                  BitmapFactory.Options options = new BitmapFactory.Options();
                  options.inPreferredConfig = Bitmap.Config.ARGB_8888;
                  android_8888 = BitmapFactory.decodeStream(inputStream, null, options);              
                  inputStream.close();
                 
                  // Read from Drawable
                  BitmapFactory.Options opt = new BitmapFactory.Options();
                  options.inJustDecodeBounds = true;
                  icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher, opt);
                 
                 
            } catch (IOException e) {
                  // 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(cow_565, null, dst, null);         
            canvas.drawBitmap(android_8888, 50, 400, null);
            canvas.drawBitmap(icon, 140, 750, null);
            invalidate();
      }
   
    } // RenderView
}




Download Android Graphics Drawing Bitmaps Example code

ไม่มีความคิดเห็น:

แสดงความคิดเห็น