1

In my app I am giving a functionality to edit an image from the filepath. Now I want to add a feature to change the image orientation with animation and then save it.

I am able to change the orientation but don't know how to add animation to it.

Here's my code.

ImageView img;
Bitmap bmp;
Bitmap rotatedBMP;
String imageFilePath="";
File imgFile;
public void rotate(){
    if(rotatedBMP == null){
        if(imgFile != null && imgFile.exists())
            bmp = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
    }else
        bmp = rotatedBMP;
    // Getting width & height of the given image.
            int w = bmp.getWidth();
            int h = bmp.getHeight();
            Matrix mtx = new Matrix();
            mtx.preRotate(90);
            rotatedBMP = Bitmap.createBitmap(bmp, 0, 0, w, h, mtx, true);
            /*BitmapDrawable bmd = new BitmapDrawable(rotatedBMP);
            img.setImageDrawable(bmd);*/
            img.setImageBitmap(rotatedBMP);
}
test
  • 546
  • 1
  • 6
  • 18
  • use this u r changing direction ofimage not roation do this http://samir-mangroliya.blogspot.in/p/android-rotation-animation.html – Naveen Tamrakar Sep 24 '13 at 14:01

1 Answers1

1

There's a conflict between your desire to animate rotate a view, and your desire to rotate and save the actual bitmap.

If you want to rotate the image view, the good way to do that is to rotate the canvas in an onDraw method with an animation update step, or to use Android's helper for that like RotateAnimation. Drop us a comment if you want examples.

That's the "good way" because it doesn't create a new bitmap on every animation frame, which would be very expensive.

However, you've got a conflict that you also want to rotate the bitmap itself, rather than just the view, so as to save it. This is fine given it's not on every animation step.

You consequently will have to save the user's chosen transformations you want to apply to the bitmap as late as possible. Then animate the view however is appropriate in response to the user. On the save, you'd concat all the users transformations on writing to the file. Or give up on the idea of animating it.

My implementation idea would be to store a second matrix locally and have each user input both update the canvas transforms and that matrix. You'd then 'get hold' of the bitmap as it were when the save button is pressed and apply it to the updated transformation matrix.

Happy to elaborate as requested.

Some sources

Android: Rotate image in imageview by an angle (mentions issues with creating many bitmaps too)

Rotating a view in Android (gauravjain0102's answer is underrated!)

Community
  • 1
  • 1
Tom
  • 1,773
  • 15
  • 23