0

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?

idish
  • 3,190
  • 12
  • 53
  • 85
  • look at this : http://stackoverflow.com/questions/39661711/android-imageview-rotate-animation-working-only-once [i have solved the problem](http://stackoverflow.com/questions/39661711/android-imageview-rotate-animation-working-only-once) – Mingo Apr 27 '17 at 03:52

1 Answers1

0

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

Sushil
  • 8,250
  • 3
  • 39
  • 71
  • The thing is, that when I use the setContentView, the layout "lags", and I want to avoid that. I know that there are some camera applications that do what I try to. They don't load the whole layout again, but show only a rotate animation to some views. – idish Aug 16 '13 at 04:06
  • Then while rotating try to change the actual layout position. Most of them do it this way – Sushil Aug 16 '13 at 04:08
  • What I meant is override onAnimationEnd() and inside that change the layout using view.layout(newleft, newTop, newleft + child.getMeasuredWidth(), newTop); You can use some other api as well like "requestLayout" etc. Have a look at them as per your need – Sushil Aug 16 '13 at 04:12
  • I don't really understand, could you please provide a little example or a link I can look at? – idish Aug 16 '13 at 04:18
  • http://stackoverflow.com/questions/8489830/change-position-of-view-after-end-of-animation – Sushil Aug 16 '13 at 04:23
  • http://www.clingmarks.com/how-to-permanently-move-view-with-animation-effect-in-android/400 – Sushil Aug 16 '13 at 04:25
  • Thank you very much, I'll take a look at these later because I'm from my device ^^ – idish Aug 16 '13 at 05:06
  • I was taking a quick look at your links. I might have not explained myself well. Using the rotate animation I want my image view to stay at the same position and rotate around itself. I hope you understand now :) – idish Aug 16 '13 at 05:12
  • Btw, if I try to click on my imageview after the rotation, nothing happens (it work before I set the animation) – idish Aug 16 '13 at 05:17
  • this is what I was about to say you. You can confirm my point by trying to click on the button. Nothing will happen. Now after animation just put setContentView(R.id.layout) again and click will happen fine.. – Sushil Aug 16 '13 at 05:18
  • I would suggest you a simple solution. Keep two layout for landscape and portrait. Alsways after in onAnimationEnd, do setContentView(correct layout). It will redo the layout and things will work. Other way obviousy is to call view.layout or requestLayout. As in your case postion didnt change, just call view.layout() again. – Sushil Aug 16 '13 at 05:22
  • Hi Sushil, sorry for taking too long to response. As for your first solution (about setting the layout again after the animation ends), I try to prevent of doing it as there will still be some kind of a "lag" after the animation ends. I think this is how the normal Camera application of android 4.3 works. It has some kind of a lag when you change orientation. As for your second solution, I didn't really understand how can I use the view.layout function, I was trying to look for a documentation, but I didn't understand what 4 paramaters I should specify. Sorry for bothering :) – idish Aug 16 '13 at 19:43