Rotate Image In Imageview By An Angle

Please follow and like us:

Rotate Bitmap Image In Android

This tutorial will help you to implement rotate bitmap image or ImageView in Android Example using Matrix with easy steps. Let’s Explore Rotate image in image view by angle.

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    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.nkdroid.sampleapp.MainActivity">


    <LinearLayout

        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="horizontal">

        <Button
            android:id="@+id/left10"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#c3000000"

            android:onClick="roclick"
            android:padding="10.0dip"
            android:text="-10"
            android:textColor="#ffffffff"
            android:textSize="10.0sp" />

        <Button
            android:id="@+id/rooriginal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#c3000000"

            android:onClick="roclick"
            android:padding="10.0dip"
            android:singleLine="false"
            android:text="Original"
            android:textColor="#ffffffff"
            android:textSize="10.0sp" />

        <Button
            android:id="@+id/right10"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#c3000000"

            android:onClick="roclick"
            android:padding="10.0dip"
            android:text="+10"
            android:textColor="#ffffffff"
            android:textSize="10.0sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/logo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/logo" />
    </LinearLayout>

</LinearLayout>

MainActivity.java

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    private Button left10,rooriginal,right10;
    private ImageView logo;
    private int rotateAngle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        left10= (Button) findViewById(R.id.left10);
        rooriginal= (Button) findViewById(R.id.rooriginal);
        right10= (Button) findViewById(R.id.right10);
        logo= (ImageView) findViewById(R.id.logo);

        left10.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rotateAngle -=10;
                rotateAngle %= 360;

                logo.setDrawingCacheEnabled(true);
                Bitmap bitmap = logo.getDrawingCache();
                logo.setImageBitmap(rotate(bitmap));
            }
        });

        rooriginal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rotateAngle =0;
                rotateAngle %= 360;

                logo.setDrawingCacheEnabled(true);
                Bitmap bitmap = logo.getDrawingCache();
                logo.setImageBitmap(rotate(bitmap));
            }
        });

        right10.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rotateAngle +=10;
                rotateAngle %= 360;
                logo.setDrawingCacheEnabled(true);
                Bitmap bitmap = logo.getDrawingCache();
                logo.setImageBitmap(rotate(bitmap));
            }
        });

    }


    public Bitmap rotate(Bitmap paramBitmap)
    {
        if (rotateAngle% 360 == 0) {
            return paramBitmap;
        }
        Matrix localMatrix = new Matrix();
        float f1 = paramBitmap.getWidth() / 2;
        float f2 = paramBitmap.getHeight() / 2;
        localMatrix.postTranslate(-paramBitmap.getWidth() / 2, -paramBitmap.getHeight() / 2);
        localMatrix.postRotate(rotateAngle);
        localMatrix.postTranslate(f1, f2);
        paramBitmap = Bitmap.createBitmap(paramBitmap, 0, 0, paramBitmap.getWidth(), paramBitmap.getHeight(), localMatrix, true);
        new Canvas(paramBitmap).drawBitmap(paramBitmap, 0.0F, 0.0F, null);
        return paramBitmap;
    }

}

 

Hope you liked out “Rotating ImageView in Android Using Matrix” Tutorial. If you find any problem while implementing this example then contact me via comment here. Glad to help you.

Stay connected for more interesting and latest android development tutorials

nirav kalola