Try The Next!
First let’s turn one Drawable in Bitmap:
public static Bitmap drawableToBitmap (Drawable drawable) {
Bitmap bitmap = null;
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
if(bitmapDrawable.getBitmap() != null) {
return bitmapDrawable.getBitmap();
}
}
if(drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
} else {
bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
Let’s rotate the image, not the ImageView:
Bitmap myImg = drawableToBitmap(imageView.getDrawable() );
Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap rotated = Bitmap.createBitmap(myImg, 0, 0, myImg.getWidth(), myImg.getHeight(), matrix, true);
After rotating, we add it to the ImageView:
imageView.setImageBitmap(rotated);
I hope I helped!
Greetings!
No need to do it by hand, there are several libraries that already do it by default.
– Androiderson
Could you indicate me any? I would be very grateful , I found other ways to do with Animation, Rotateanimation, setAnimation, etc.. But I can’t replace the 180º rotated result with the initial..
– CristianCotrena
The advantage of doing the hand is to save processing. If Imageview will always be rotated it is best to do it by hand.
– Lucas Lima