Tag Archives: Android Game Programming Development lesson-1. Drawing an image using View.

Android Game Programming Development lesson-1. Drawing an image using View.

In this little tutorial we are going to see how to print an image in the android screen directly. We are going to extend a View object that is a direct way. We are going to use View because we want complete control over the screen.

Part 1 priting with onDraw using View :-

First of all create a class ”GameView” which Extends View as given below :-

package com.major.badboys;

import android.content.Context;
import android.view.SurfaceView;
import android.view.View;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;

public class GameView extends View
{

private Bitmap bmp;

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

bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);

}

@Override
protected void onDraw(Canvas canvas)
{
canvas.drawColor(0xff447766);

canvas.drawBitmap(bmp, 50, 100, null);
}

}

Now create the Main Activity which is going to use the above class in its setContentView(); method

Syntex: setContentView(new GameView(this));

package com.major.badboys;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;

public class Main extends Activity
{

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);

setContentView(new GameView(this));

}

}

 

In the View implementation the onDraw method is called directly by the holder in a code we can not see when the View is created. The holder is the object that contains the View.

Happy Programming ………

Skip to toolbar