Rotate Animation of an ImageView

390 views Asked by At

I have a camera application. Whenever the device orientation is changed from portrait to landscape and vice versa, I would like to animate the ImageView to his side.

Here's my code, the animation is done only on the first set, If the event of the orientation change occures again, nothing happens.

Here's the code:

  @Override
     public void onSensorChanged(SensorEvent event) {
         if (event.values[1]<6.5 && event.values[1]>-6.5) {
             if (orientation!=1) { // Portrait
                 animation = new RotateAnimation(0,90,RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
                 animation.setFillAfter(true); 
                 animation.setFillEnabled(true);
                 animation.setDuration(0);  
                 imageView.setAnimation(animation);
             }
             orientation=1;
         } else {
             if (orientation!=0) { // Landscape
                 animation = new RotateAnimation(0,270,RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
                 animation.setFillAfter(true); 
                 animation.setFillEnabled(true);
                 animation.setDuration(0);  
                 imageView.setAnimation(animation);
             }
             orientation=0;
         }
     }

Why does it happen, how can I make sure that whenever that event occures my animation will be applied to my imageView with other rotate paramters, and not using the same angle?

1

There are 1 answers

12
Sushil On

When you animate a View using the normal view animations, you're only animating the position at which it is drawn. You're not affecting its actual location in the layout. Assuming you intend to make the View change position permanently, you'll need to update the layout parameters of the View after the animation is complete to match the movement you make with the animation.

or after animation, do a setContentView with proper layout xml again..