Share
Introduction
I read an article from the people of droidnova that was very interesting to get started with graphics in android. The first part of this series will show you, how to display an image in a normal View.Graphics in Android - Part I
First we create a new Project with the activity named Tutorial2D.We will see this:
1 |
package com.droidnova.android.tutorial2d; |
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.
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:
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).
Finally we will have this code.
1 |
package com.droidnova.android.tutorial2d; |
Start this on your Emulator / Device and it should look like that:






