Jul
21
Android Development masterclass
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);
}
}
}















An alternative to your ProgressBar approach is AsyncTask and onProgressUpdate: http://developer.android.com/reference/android/os/AsyncTask.html
Keep up the Android articles.