Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Rotate image in imageview by an angle

People also ask

How to get image rotation in Android?

Use ExifInterface for rotate image. Use this method for get correct value to be rotate captured image from camera. And put this code in Activity result method and get value to rotate image... String selectedImage = data.


Another simple way to rotate an ImageView:
UPDATE:
Required imports:

import android.graphics.Matrix;
import android.widget.ImageView;

Code: (Assuming imageView, angle, pivotX & pivotY are already defined)

Matrix matrix = new Matrix();
imageView.setScaleType(ImageView.ScaleType.MATRIX);   //required
matrix.postRotate((float) angle, pivotX, pivotY);
imageView.setImageMatrix(matrix);

This method does not require creating a new bitmap each time.

NOTE: To rotate an ImageView on ontouch at runtime you can set onTouchListener on ImageView & rotate it by adding last two lines(i.e. postRotate matrix & set it on imageView) in above code section in your touch listener ACTION_MOVE part.


mImageView.setRotation(angle) with API>=11


If you're supporting API 11 or higher, you can just use the following XML attribute:

android:rotation="90"

It might not display correctly in Android Studio xml preview, but it works as expected.


There are two ways to do that:

1 Using Matrix to create a new bitmap:

imageView = (ImageView) findViewById(R.id.imageView);
Bitmap myImg = BitmapFactory.decodeResource(getResources(), R.drawable.image);

Matrix matrix = new Matrix();
matrix.postRotate(30);

Bitmap rotated = Bitmap.createBitmap(myImg, 0, 0, myImg.getWidth(), myImg.getHeight(),
        matrix, true);

imageView.setImageBitmap(rotated);

2 use RotateAnimation on the View you want to Rotate, and make sure the Animation set to fillAfter=true, duration=0, and fromDegrees=toDgrees

 <?xml version="1.0" encoding="utf-8"?>
<rotate
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:fromDegrees="45"
  android:toDegrees="45"
  android:pivotX="50%"
  android:pivotY="50%"
  android:duration="0"
  android:startOffset="0"
/>

and Inflate the Animation in code:

Animation rotation = AnimationUtils.loadAnimation(this, R.anim.rotation);
myView.startAnimation(rotation);

I know this is insanely late, but it was helpful for me so it may help others.

As of API 11, you can set the absolute rotation of an ImageView programmatically by using the imageView.setRotation(angleInDegrees); method.

By absolute, I mean you can repeatedly call this function without having to keep track of the current rotation. Meaning, if I rotate by passing 15F to the setRotation() method, and then call setRotation() again with 30F, the image's rotation with be 30 degrees, not 45 degrees.

Note: This actually works for any subclass of the View object, not just ImageView.


For Kotlin,

mImageView.rotation = 90f //angle in float

This will rotate the imageView rather than rotating the image

Also, though its a method in View class. So you can pretty much rotate any view using it.