Android development: Loading bitmaps

If you want to add graphics to your application you can use the following information

Create a custom Activity like this

public class GameActivity extends Activity implements Observer{

	private static final String TAG = "StrategoGameActivity";
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(new StrategoGameView(this));
		ActionBar actionBar = getActionBar();
		actionBar.hide();
        
	}
    
	
	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		
	}

}

Create your own View ( in this example StrategoGameView)

public class StrategoGameView extends View{

public StrategoGameView(Context context){
 super(context);
 initView(context);
}

private InitializeView(Context c){
...
}

}

I personally find it a good practice to call an initialize method in your constructor. This way you can decide if you want to make it public so when needed you can reinitialize easily.

Now if you want to add a Bitmap to your application you need to do the following things

Add a res/drawable directory to your project

create some fancy images and save them in this directory.

After a rebuild you can get a pointer to the using the R.drawable object like this

Bitmap mybitmap = BitmapFactory.decodeResource(context.getResources(),R.drawable.cardtop_blue);

to draw the bitmap to the screen you need to add a onDraw method to your View like this:

protected void onDraw(Canvas canvas){

canvas.drawBitmap(mybitmap,x,y, null);


}

 

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.