How to rotate imageView 90º?

1

Hi, I'm doing an Image editing app and I need to know how to do a 90-degree rotation of an imageView and replace the original with the rotated image I searched the sites and found this way, but when I do the rotation, the image gets very small and I can not rotate it again. Thank you in advance, every 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);
}
    
asked by anonymous 08.12.2015 / 23:18

1 answer

1

Try the Next!

First let's turn a Drawable into 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 ImageView :

imageView.setImageBitmap(rotated);

I hope I have helped!

Greetings!

    
09.12.2015 / 15:23