The first part of this series will show you, how to display an image in a normal View.
First we create a new Project with the activity named Tutorial2D.
We will see this:
1 2 3 4 5 6 7 8 9 10 11 12 13 | package com.droidnova.android.tutorial2d; import android.app.Activity; import android.os.Bundle; public class Tutorial2D extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } } |
This should look familiar to you.
The first thing we should add is our custom view. Lets create an inner class named Panel which extends from the View class and override the method onDraw(Canvas) because we want to draw a bitmap per default.
The inner class looks like that.
1 2 3 4 5 6 7 8 9 | class Panel extends View { public Panel(Context context) { super(context); } @Override public void onDraw(Canvas canvas) { } } |
The next step is to program our onDraw(Canvas) method. We will use the default application icon for displaying.
To get the icon as bitmap, we have to use the BitmapFactory class.
1 | Bitmap _scratch = BitmapFactory.decodeResource(getResources(), R.drawable.icon); |
To make the background black, we will use the drawColor(Color) method of our Canvas object.
1 | canvas.drawColor(Color.BLACK); |
Finally we draw our bitmap on the coordinates 10/10.
1 | canvas.drawBitmap(_scratch, 10, 10, null); |
Finally the method will look like that:
1 2 3 4 5 6 | @Override public void onDraw(Canvas canvas) { Bitmap _scratch = BitmapFactory.decodeResource(getResources(), R.drawable.icon); canvas.drawColor(Color.BLACK); canvas.drawBitmap(_scratch, 10, 10, null); } |
Now we have to use our custom view to be displayed, so lets change the setContentView() (line 5). Additionally we want to have a window without a title (line 4).
1 2 3 4 5 6 | @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(new Panel(this)); } |
Finally we will have this code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | package com.droidnova.android.tutorial2d; import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.view.Window; public class Tutorial2D extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(new Panel(this)); } class Panel extends View { public Panel(Context context) { super(context); } @Override public void onDraw(Canvas canvas) { Bitmap _scratch = BitmapFactory.decodeResource(getResources(), R.drawable.icon); canvas.drawColor(Color.BLACK); canvas.drawBitmap(_scratch, 10, 10, null); } } } |
Start this on your Emulator / Device and it should look like that:

Comments
Leave a comment Trackback