Official website for Linux User & Developer
FOLLOW US ON:
Jul
21

Android Development masterclass

by Kunal Deo

It’s time to go beyond the ‘hello world’ app. Let’s look into real-world situations and start doing big things with your Android development project…

Drawing a bitmap image
The android.graphics package provides BitMap and BitMapFactory classes to work with bitmaps (ie images comprising a map or array of bits) inside an Android application. Bitmaps can be read by using the following method:

Bitmap penguinBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.penguins);
canvas.drawBitmap(penguinBitmap, 0, 0,null);

In the following code example, we are drawing a .bmp file into a view and loading this instead of the main layout. To run this app, copy drag the  file penguins.bmp to the res/drawable-ldpi folder in Eclipse (or MOTODEV Studio). You can also copy the same into the other resource folder depending upon the devices you support.

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 //setContentView(R.layout.main);
 setContentView(new bitmapView(this));
 }

 private class bitmapView extends View{

 public bitmapView(Context context) {
 super(context);
 // TODO Auto-generated constructor stub
 }

 @Override
 protected void onDraw(Canvas canvas){
 Bitmap penguinBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.penguins);
 canvas.drawBitmap(penguinBitmap, 0, 0,null);
 }
 }

}
twitter follow us
Pages: 1 2 3 4 5 6
  • Tell a Friend
  • Follow our Twitter to find out about all the latest Linux news, reviews, previews, interviews, features and a whole more.

    One Comment »

    What's your opinion?

    Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

    Be nice. Keep it clean. Stay on topic. No spam.

    * Required fields