How to rotate imageView 90º?

Asked

Viewed 912 times

1

Hello, I’m making an Image editing app and I need to know how to rotate 90º of an imageView and replace the original with the image that has rotted I searched the sites and found this way to do, but when I rotate, the image gets too small and I can’t rotate it again. Thanks in advance, all tip and help is welcome!

public void rotacionar(View v){
    resultView;  //é a minha imageView
    Matrix m = new Matrix();
    resultView.setScaleType(ImageView.ScaleType.MATRIX);
    m.postRotate(180, 200, 200);
    resultView.setImageMatrix(m);
}
  • 1

    No need to do it by hand, there are several libraries that already do it by default.

  • 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..

  • The advantage of doing the hand is to save processing. If Imageview will always be rotated it is best to do it by hand.

1 answer

1


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!

  • 1

    I found this form very interesting, but there in the first line where you put (R.drawable.minha_imagem) that is the question. The image I want is already inside the imageView, so I need to know how to take the image of the imageView, modify it and return it. :)

  • I edited my answer! see if it suits you! Greetings!

  • Now it was, thanks for the help!! D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.