RotateAnimation behaves weird after changing the position of a View - android

I am using RotateAnimation to rotate the imageView to -30 degrees. And it works good. Now I change the position of the view by setting different values for X and Y. All good until now.
Now when I perform the same RotateAnimation again to this view after the position changed, it works weird (see the video link below) and I cannot find the reason. I have tried a lot but without success. It seems like related to the pivotX and pivotY after changing the position but I cannot find the actual cause.
Code for RotateAnimation
final RotateAnimation rotateAnim = new RotateAnimation(0f, -30f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 1f);
rotateAnim.setDuration(2000);
rotateAnim.setFillAfter(false);
ivFace.startAnimation(rotateAnim);
Code of changing the position of an imageview
ivFace.setX(300);
ivFace.setY(200);
Here I am posting the video also for better understanding
https://www.youtube.com/watch?v=uVWlrTNDKCM

Looks like the RotationAnimation is still rotating around the 'old' view position, probably because it is 'final' and thats why it is tied to the Views 'old' position the moment it is created. You could create a new RotationAnimation for every position change, which might solve the issue, although I would suggest using ViewPropertyAnimator and/or ObjectAnimator for both, translating your View to another position and rotating it.

Related

Can I rotate a spinner? Androidstudio

simple question I cant find the answer anywhere, is there a way to rotate a spinner, without rotating screen or anything just create a spinner which is rotated 90 degrees?
Thanks in advance.
On API Level 11+, you are welcome to rotate any View via android:rotation in your layout XML or setRotation() in Java. Whether this will give you something that your users will like, I cannot say.
Programmatically you can try something like this -
RotateAnimation animation = new RotateAnimation(fromDegrees, toDegrees, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
// Adding the time needed to rotate the image
animation.setDuration(250);
// Set the animation to stop after reaching the desired position. Without this it would return to the original state.
animation.setFillAfter(true);
//Then set the animation to your view
view.startAnimation(animation);

Android elevated view animation not smooth

I want to rotate a simple imageview which has an elevation of 5dp.
animRotate=ObjectAnimator.ofFloat(imgProgress, "rotationY", 0, 360);
animRotate.setDuration(ANIM_DURATION);
animRotate.setRepeatCount(5);
animRotate.start();
The animation for the above code is smooth if the android:elevation value for the ImageView is not set in the layout file. But when i set the elevation, the animation becomes jerky.
Can someone please suggest a fix?
Maybe the reason is that you create and run animatuion at once. As docs say, it is better first to init your animation
//OnCreate
animRotate=ObjectAnimator.ofFloat(imgProgress, "rotationY", 0, 360);
animRotate.setDuration(ANIM_DURATION);
animRotate.setRepeatCount(5);
And then when it is time for animation to be fired run it
animRotate.start();
Also, consider reading about what PivotX and PivotY are, it may be useful.
Also, using default interpolator will give strange result for rotating 5 times - i think using simple linear interpolator is much better choice.

Android rotation animation

I have a problem and I just can't manage to find a solution that works.
So, here is my problem. I have to make an ImageView to rotate itself, to 90 degree on orientation change.
I did that, and the image is actually rotating pretty awesome, but when the animation finishes, the image reset itself to previous position.
Here is the code I used to rotate the image:
Matrix matrix = mImageView.getImageMatrix();
RectF dst = new RectF();
matrix.mapRect(dst, new RectF(mImageView.getDrawable().getBounds()));
mAnimation = new RotateAnimation(0.0f, -90.0f, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
mAnimation.setDuration(5000);
mImageView.startAnimation(mAnimation);
mImageView.setImageMatrix(matrix);
mCurrentOrientation = 1;
you can use this to animation persist after it has been done:
mAnimation.setFillEnabled(true);
mAnimation.setFillAfter(true);
One thing you can do is set the imageview to an already rotated version of the image and then rotate it from -90 to 0 degrees. That will work with the API you are already using. If you make the two calls, one right after it will work correctly without stutter.
The other option is to use a later API version. The Object Animator animations do not return the view to it's original position. More on this here.
How to rotate a drawable by ObjectAnimator?
The code is simple, but this doesn't work with gingerbread.

How to explain the Coordinate system on Android animation?

As we know , the android coordinate system is start from the top left corner of the android screen. The x-axis is down growth and the y-axis is right growth.But I found it's not right for the animation.
For example, I initialized the TranslateAnimation using the constructed function:
TranslateAnimation ta = new TranslateAnimation(0.0f, 200, 0.0f, 200);
Does the coordinate system have changed ? I found it didn't start from the top left corner.
Then I initialized the other translateAnimation for moving up and right direction :
TranslateAnimation ta = new TranslateAnimation(0.0f, 200, 0.0f, -200);
ta.setReaptMode(Animation.REVERSE);
The same behavior would be found.
I am confused about it.
I believe that constructor for TranslateAnimation uses deltas. See this. Or look at the constructor sig. : (float fromXDelta, float toXDelta, float fromYDelta, float toYDelta). So if you want your anim. to jump up first, you could use a negative third ctor param.
More precisely:
An animation can never start until after the layout has been measured. One usually shouldn't have to worry about how this works beyond that the algorithm is mostly very good and you can take control of its strategies by setting layout parameters. In short, by the time an animation might be started, we know where you want the view to be on the screen, because you set layout parameters.
Translate animation then takes deltas from that position. So your current animation shouldn't start from the top left, but rather wherever those layout params were evaluated by onMeasure.
Some would say- how annoying. It's gonna get complicated even if you just want to do some simple up-down type animations... Well, here's an advisable development strategy; it snould make android animation development a breeeze. Set an animationListener on every animation. In onAnimationEnd, in possibly a parametized way, reset the layout parameters on the view your animating to where you expect it to be. That way, you'll get no surprising "jumps" when you re-apply an animation again. You may need to invalidate in some circumstances, or clearAnimation. The reason that this works is that the measure pass will be caused to come round again and you'll have a new offset for your TranslateAnimation. Finally, you may want to do all this resetting posted to the message queue of a view using post(Runnable runnable) in the listener, so you're off the last pass of the animation draw.
I too found android Animations can occasionally surprise you and cause jumpy behaviour. But if you do it like this, constructors taking delta params shouldn't be confusing again.

Android RotateAnimation - rotate arrow back and forth

In my application, I am rotating a clock hand to the desired amount of minutes. This works good with RotateAnimation. Now what I want to do is to return the hand back to the starting position (0 minutes).
For example:
I first rotate the hand like this:
final RotateAnimation anim = new RotateAnimation(0f, ammountDegress, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 1f);
anim.setFillAfter(true);
anim.setFillEnabled(true)
Then I would like to move the hand back to it's starting position, so that means I should be calling a RotateAnimation after the first animation ends.
The problem is that the pivots have changed, how I can I set the pivot position to the exact position as previous? So that the rotating point is again at the same position of the clock hand.
Edit To make it more clearer - I would like to have something like a fixed point pivot that doesn't change with the rotation, so I am always rotating the hand around the same point.
I hope you can understand what I mean.

Categories

Resources