How to swing bitmap drawable resource folder image to any specified given Angle-Degrees above canvas on button click.
In this tutorial we are simply rotating the bitmap image which is called from drawable folder and rotate that image to 45° angle. So here is the complete step by step tutorial for Rotate Bitmap image on Canvas in android programmatically.
Note : Please download sample image form below and put inside drawable-hdpi folder.
Below is the demo image download this image and copy into drawable-hdpi folder :
How to Rotate Bitmap image on Canvas in android programmatically.
Code for MainActivity.java file.
package com.rotatebitmaponcanvas_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.Matrix; 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; Matrix matrix; int bitmap1Width, bitmap1Height, CanvasWidth,CanvasHeight; int degreesAngle = 45 ; @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(); matrix = matrix = new Matrix(); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub CreateBitmap(); bitmap2 = Bitmap.createBitmap( 400, 300, Bitmap.Config.RGB_565 ); CreateCanvas(); GetWidthHeight(); matrix.setRotate( degreesAngle, bitmap1Width, bitmap1Height ); matrix.postTranslate( CanvasWidth - bitmap1Width, CanvasHeight - bitmap1Height ); DrawCanvas(); imageview.setImageBitmap(bitmap2); } }); } public void CreateBitmap(){ bitmap1 = BitmapFactory.decodeResource( resources, R.drawable.demo_image ); } public void CreateCanvas(){ canvas = new Canvas(bitmap2); canvas.drawColor(Color.CYAN); } public void GetWidthHeight(){ bitmap1Width = bitmap1.getWidth() / 2 ; bitmap1Height = bitmap1.getHeight() / 2 ; CanvasWidth = canvas.getWidth() / 2 ; CanvasHeight = canvas.getHeight() / 2 ; } public void DrawCanvas(){ canvas.drawBitmap( bitmap1, matrix, paint ); } }
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.rotatebitmaponcanvas_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_centerHorizontal="true" android:layout_centerVertical="true" android:text="Click here to Rotate a Bitmap image on Canvas in android programmatically" /> </RelativeLayout>
Screenshot: