Create Draw Bitmap image on Canvas in android programmatically

How to show drawable resource image as bitmap just above surface of canvas in android on button click.

Drawing drawable folder resource image above canvas by simply converting image into bitmap. After changing the image format into bitmap we can easily set that image on canvas area on activity screen. So here is the complete step by step tutorial for Create Draw Bitmap image on Canvas in android programmatically.

android-project-download-code-button

How to Create Draw Bitmap image on Canvas in android programmatically.

Code for MainActivity.java file.

 package com.drawbitmapimageoncanvas_android_examples.com;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {

 ImageView imageview;
 Button button;
 Bitmap bitmap1,bitmap2;
 Canvas canvas;
 Paint paint;
 Resources resources;
 int BitmapSize = 30;
 int width, height;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 imageview = (ImageView)findViewById(R.id.imageView1);
 button = (Button)findViewById(R.id.button1);
 
 resources = getResources();
 
 button.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 CreateBitmap();
 
 GetBitmapWidthHeight();
 
 bitmap2 = Bitmap.createBitmap(
 width,
 height,
 Bitmap.Config.RGB_565
 );
 
 DrawCanvas();
 
 imageview.setImageBitmap(bitmap2);
 
 }
 });
 }
 
 public void CreateBitmap(){
 
 bitmap1 = BitmapFactory.decodeResource(
 resources,
 R.drawable.demo_image
 );
 
 }
 
 public void GetBitmapWidthHeight(){
 
 width = bitmap1.getWidth() + BitmapSize * 2;
 height = bitmap1.getHeight() + BitmapSize * 2;
 
 }
 
 public void DrawCanvas(){
 
 canvas = new Canvas(bitmap2);
 
 canvas.drawColor(Color.CYAN);
 
 canvas.drawBitmap(
 bitmap1,
 BitmapSize,
 BitmapSize,
 null
 );
 
 }
}

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.drawbitmapimageoncanvas_android_examples.com.MainActivity" >

 <ImageView
 android:id="@+id/imageView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 />

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentBottom="true"
 android:layout_centerHorizontal="true"
 android:layout_marginBottom="60dp"
 android:text="Click here to Create Draw Bitmap image on Canvas in android programmatically" />

</RelativeLayout>

Screenshot:

Create Draw Bitmap image on Canvas in android programmatically

Click here to download Create Draw Bitmap image on Canvas in android programmatically project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *