83

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!

Samet ÖZTOPRAK
  • 3,112
  • 3
  • 32
  • 33
justinl
  • 10,448
  • 21
  • 70
  • 88

7 Answers7

150

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

android:fillAfter="true" 
android:fillEnabled="true"
Gagan
  • 1,497
  • 1
  • 12
  • 27
Qlimax
  • 5,241
  • 4
  • 28
  • 31
  • 7
    This is the real answer. **The OP should change this to be the accepted answer.** Thank you Qlimax!! (P.S. this is not documented in the "Animation" section of the Dev Guide, it is only documented in the javadocs for the Animation class. Such poor documentation!) – Neil Traft Sep 09 '10 at 01:36
  • 7
    where to put this code? in `` or in animation tags(`translate`|`rotate`|`scale`)? – xxxxxxxxxadfas Aug 02 '11 at 08:27
  • 5
    as mentioned below, you have to add it to the set paramter – cV2 Aug 13 '12 at 13:42
  • 3
    This does not work. I want the view's real size to change with the animation. It only changes the content. I am changing scale Y from 1.0 to 0.0 and the animation happens but leaves an empty space where the view used to be. Also, I tried View.GONE but that snaps the other views into the empty space. I need the other views to follow the scaling view's size while it shrinks. How to get that working? – Sudhanshu Jul 28 '14 at 20:11
  • 1
    Any one else reading this be noted that `fillAfter` will only keeps the view pixels stay while the actual view will still at initial position. You need to manually set `LayoutParams` of your view probably in onAminationEnd (listener). – Muhammad Babar Nov 14 '14 at 05:25
  • 1
    This can be create problem of touch event. see http://stackoverflow.com/questions/4728908/android-view-with-view-gone-still-receives-ontouch-and-onclick – W00di Sep 09 '16 at 08:41
  • This attribute doesn't needed: android:fillEnabled="true". This is required for this attribute: android:fillBefore. Look at this: https://developer.android.com/reference/android/view/animation/Animation.html#attr_android:fillAfter. – Arash Jul 23 '17 at 10:59
  • It wouldn't work with `android:fillEnabled="true"`. Only with `android:fillAfter="true" ` it will work properly – Marat Zangiev May 27 '22 at 13:44
53

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

anim.setFillEnabled(true);
anim.setFillAfter(true);
Mina Wissa
  • 10,923
  • 13
  • 90
  • 158
  • but is create new problem, i receive touch event on it.http://stackoverflow.com/questions/4728908/android-view-with-view-gone-still-receives-ontouch-and-onclick – W00di Sep 09 '16 at 08:51
32

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!

Community
  • 1
  • 1
cV2
  • 5,229
  • 3
  • 43
  • 53
23

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.

Andrew
  • 36,676
  • 11
  • 141
  • 113
2

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.

Walter Mundt
  • 24,753
  • 5
  • 53
  • 61
  • Hi Walter. I have overwritten the onAnimationEnd, but I don't know how to adjust the "real" bounds of the view to match the scale of the animation. I do know what exact dp I want it to end up, but I can't find the function to "setWidth()" or "setHeight()". – justinl Jul 28 '10 at 02:31
  • 1
    I believe those are stored in the LayoutParams. You should be able to do `view.getLayoutParams().width = newWidth; view.getLayoutParams().height = newHeight; view.requestLayout();`. (The latter call is necessary to inform the view tree that it needs to be recalculated.) – Walter Mundt Jul 28 '10 at 16:07
  • 2
    This is an incredibly roundabout way of doing things. All one needs to do is set attributes `android:fillEnabled` and `android:fillAfter` to both be true. – Neil Traft Sep 09 '10 at 01:38
1

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.

takrishna
  • 4,884
  • 3
  • 18
  • 35
1

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.

Sephy
  • 50,022
  • 30
  • 123
  • 131
  • 2
    scaleUp150 is a custom animation I wrote in xml (though it's extremely basic). I did put the android:fillAfter="true" inside the animation but it still snaps back to the original size :S – justinl Jul 28 '10 at 02:32