Android Projects

Android Development Books

Friday

Draw an image using Bitmap class object in android

This is a sample program to draw an image using Bitmap class object.
Underlying Algorithm:
Basic description of algorithm in step by step form:
1.) Create a Project DrawBitmap.
2.) To draw an image we will create a inner class BitmapView which will extends View :
class BitmapView extends View
{
    public BitmapView(Context context) {
            super(context);
    }
    @Override
    public void onDraw(Canvas canvas) {
        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.five);
        canvas.drawColor(Color.BLACK);
        canvas.drawBitmap(bmp, 10, 10, null);
    }
}
3.) Put an image in res/drawable
4.) Run the application.
Steps to Create:
1.) Open Eclipse. Use the New Project Wizard and select Android Project Give the respective project name i.e. DrawBitmap. Enter following information:
Project name: DrawBitmap
Build Target: Android 2.3
Application name: DrawBitmap
Package name: com.sample.DrawBitmap
Create Activity: DrawBitmap

On Clicking Finish DrawBitmap code structure is generated with the necessary Android Packages being imported along with DrawBitmap.java. DrawBitmap class will look like following:

package com.sample.DrawBitmap; 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 DrawBitmap extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new BitmapView(this));
    }

    class BitmapView extends View {
        public BitmapView(Context context) {
            super(context);
        }

        @Override
        public void onDraw(Canvas canvas) {
            Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.five);
            canvas.drawColor(Color.BLACK);
            canvas.drawBitmap(bmp, 10, 10, null);
        }
    }
}
Output –The final output:
 
If you want to learn more details about this article click here
.
Share this post
  • Share to Facebook
  • Share to Twitter
  • Share to Google+
  • Share to Stumble Upon
  • Share to Evernote
  • Share to Blogger
  • Share to Email
  • Share to Yahoo Messenger
  • More...

0 comments

Thanks for your comment

:) :-) :)) =)) :( :-( :(( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ :-$ (b) (f) x-) (k) (h) (c) cheer

Related Posts Plugin for WordPress, Blogger...
© Google Android Lovers
Designed by BlogThietKe Cooperated with Duy Pham
Released under Creative Commons 3.0 CC BY-NC 3.0