How to animate a view in Android and have it stay in the new position/size?

AndroidAnimation

Android Problem Overview


I currently have a view in my Android app and the view is playing a frame animation. I want to animate the view to increase its size to 150%. When I apply a scale animation to it, and the scale animation is completed, I want the viewer to stay at that new size for the rest of the activity's life cycle. Unfortunately right now when the scale-up animation is complete, the view snaps back to the original size. How can I get it to keep the new animated transformation?

I'm using

myView.startAnimation(AnimationUtils.loadAnimation(mContext,R.anim.scaleUp150));

Thanks!

Android Solutions


Solution 1 - Android

Make sure you add below attributes to the root element in your animation xml:

android:fillAfter="true" 
android:fillEnabled="true"

Solution 2 - Android

try constructing the animation from code and setting the properties like this

anim.setFillEnabled(true);
anim.setFillAfter(true);

Solution 3 - Android

if you want to put your animation in an xml, it may need this to help:

<set
android:fillEnabled="true"
android:fillAfter="true"
xmlns:android="http://schemas.android.com/apk/res/android">

<translate
    android:fromYDelta="0"
    android:toYDelta="-20%p"
    android:duration="7000" />

</set>

https://stackoverflow.com/a/6519233/371749

please also appreciate the other answer, helped me :) good luck!

Solution 4 - Android

Nowadays it has become very easy:

view.animate().x(valueX).y(valueY).setDuration(500).start();

(In this snippet ViewPropertyAnimator has been used).

There is possible to append multiple other options either.

The View will be located in the new position.

Solution 5 - Android

EDIT: Qlimax's answer is better, but since the OP hasn't returned to change the checkmark and I can't delete an accepted answer, I'll copy it up here: just set fillAfter=true fillEnabled=true

My original answer (which works, but is mildly ridiculous by comparison) follows:

For things to work as expected after the animation, at least according to this thread, you will have to write an onAnimationEnd handler, and in there, manually adjust the "real" (pre-transformation) bounds of your view to match the end result of the scale animation.

Solution 6 - Android

Actually, as the animation you are using seems to be one embedded in the Android framework, I'm not sure you can change anything in it.
However, you can create you own animation by following the example in the documentation. And you will have to put android:fillAfter="true" if you want the scaling to be kept after the end of the animation.

Solution 7 - Android

Put android:fillAfter="true" in the SET tag - as putting them in animation tag confines them to the "transition parameters" or "during/while animating parameters". The parameters in the SET will apply the transformation you are looking for after it finishes the animations.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjustinlView Question on Stackoverflow
Solution 1 - AndroidQlimaxView Answer on Stackoverflow
Solution 2 - AndroidMina WissaView Answer on Stackoverflow
Solution 3 - AndroidcV2View Answer on Stackoverflow
Solution 4 - AndroidAndrewView Answer on Stackoverflow
Solution 5 - AndroidWalter MundtView Answer on Stackoverflow
Solution 6 - AndroidSephyView Answer on Stackoverflow
Solution 7 - AndroidtakrishnaView Answer on Stackoverflow