1

I want to rotate an ImageView image. Here is my code:

Matrix mat = new Matrix();
mat.preRotate(90, ivImage.getWidth()/2, ivImage.getHeight()/2);
ivImage.setScaleType(ScaleType.MATRIX);
ivImage.setImageMatrix(mat);

but when I click on the rotate button, not only the image rotates, but it is scaled too, because the bitmap is larger then the ImageView.

I know I can use

b = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), mat, true);
ivImage.setImageBitmap(b);

but this is noticeably slower, and has some lag.

So my question is: how to rotate the image without being scaled and without recreating the bitmap?

nikmin
  • 1,803
  • 3
  • 28
  • 46

6 Answers6

3

Here is the code that I have used for rotation of image to 90 degrees.

         if(oldAngle == 0){
              newAngle = oldAngle + 90;
            }
            else if(oldAngle == 90)
                newAngle = oldAngle - 90;
              LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view.getLayoutParams();
              int centerX = layoutParams.leftMargin + (view.getWidth()/2);
              int centerY = layoutParams.topMargin + (view.getHeight()/2);
              RotateAnimation animation = new RotateAnimation(oldAngle, newAngle, centerX, centerY);
              animation.setDuration(0);
              animation.setRepeatCount(2);
              animation.setFillAfter(true);
              view.startAnimation(animation);

              oldAngle = newAngle;

Hope it will help you...

kamil
  • 579
  • 4
  • 15
  • This is great, but when the image rotates to 90 degrees the image is cut, the whole image is not visible, it's going out of the bounds of the image view. Any solution for that? – nikmin Apr 01 '13 at 11:49
  • @nikmin can you please attach the screen shot of that image – kamil Apr 01 '13 at 16:36
  • I think your linearlayout width or height is not adjustable when the view rotates. – kamil Apr 01 '13 at 16:39
  • Yes, it is not adjustable, because the image is using all the available space when I load it in landscape. And when I rotate it, it gets out of the bounds because the width becomes the height after I rotate it. But never mind, I found another solution that includes both rotate and crop. I'm using [this](https://github.com/biokys/cropimage). – nikmin Apr 02 '13 at 07:51
0

How about using an XML animation?

<?xml version="1.0" encoding="utf-8"?>
    <rotate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:interpolator="@android:anim/linear_interpolator"
    android:toDegrees="360"
    android:pivotX="50%"
    android:pivotY="50%"
    android:duration="1000"
    android:startOffset="0"
    android:repeatCount="infinite"
    />

And then use it with something like;

 rotation = AnimationUtils.loadAnimation(this, R.anim.clockwise_rotation);
 buttonRefresh.startAnimation(rotation);

ps. This infinitely spins around. So needs some adjustments.

Stefan de Bruijn
  • 6,289
  • 2
  • 23
  • 31
0

use this

final Random generator = new Random();
        int n = 10000;
        n = generator.nextInt(n);
        final Matrix mtx = new Matrix();
        mtx.postRotate(n);
        rotator = Bitmap.createBitmap(all, 0, 0,
                all.getWidth(), all.getHeight(), mtx,
                true);
        view.setImageBitmap(rotator);

this will rotate the image randomly. just change.nextInt(n) to .nextInt(AnyNumberYouLife) its fast :)

Kosh
  • 6,140
  • 3
  • 36
  • 67
0

I found that for my purpose it was the best solution to use Canvas instead of ImageView. And for the rotating of the image I chose to use another library that can be found here

nikmin
  • 1,803
  • 3
  • 28
  • 46
0

here's a nice solution for putting a rotated drawable for an imageView:

Drawable getRotateDrawable(final Bitmap b, final float angle) {
    final BitmapDrawable drawable = new BitmapDrawable(getResources(), b) {
        @Override
        public void draw(final Canvas canvas) {
            canvas.save();
            canvas.rotate(angle, b.getWidth() / 2, b.getHeight() / 2);
            super.draw(canvas);
            canvas.restore();
        }
    };
    return drawable;
}

usage:

Bitmap b=...
float angle=...
final Drawable rotatedDrawable = getRotateDrawable(b,angle);
root.setImageDrawable(rotatedDrawable);

another alternative:

private Drawable getRotateDrawable(final Drawable d, final float angle) {
    final Drawable[] arD = { d };
    return new LayerDrawable(arD) {
        @Override
        public void draw(final Canvas canvas) {
            canvas.save();
            canvas.rotate(angle, d.getBounds().width() / 2, d.getBounds().height() / 2);
            super.draw(canvas);
            canvas.restore();
        }
    };
}

also, if you wish to rotate the bitmap, but afraid of OOM, you can use an NDK solution i've made here

Community
  • 1
  • 1
android developer
  • 114,585
  • 152
  • 739
  • 1,270
0

I've found the easiest way to rotate an ImageView is to do this:

view.animate().rotationBy(degrees);

Very easy to implement and android handles all the rest

Hope this helps! :)

Machine Tribe
  • 871
  • 9
  • 13