fillAfter and fillEnabled not working in Android v

2019-02-05 20:33发布

问题:

I'm curious as to this behavior ... I'm currently setting the two values in the anim XML:

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

However, the transformation does not apply after the animation is done ... it always resets. When I set it programmatically via code it does seem to work:

    animation.setFillEnabled(true);
    animation.setFillAfter(true);

So I'm just curious how this should work, as I'd rather set it on the XML. Thanks!

回答1:

It also works if you don't have the set tag and are just doing translate or something like so.

    <translate xmlns:android="http://schemas.android.com/apk/res/android"       
    android:interpolator="@android:anim/linear_interpolator"       
    android:fromXDelta="0"
    android:toXDelta="0"
    android:fromYDelta="1000"
    android:toYDelta="-300" 
    android:startOffset="100"
    android:duration="1000"
    android:fillAfter="true" />


回答2:

I had the same problem, this worked for me:

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

Put the attributes fillEnabled and fillAfter in the Set tag.



回答3:

In general, use the fillAfter and fillEnabled on the root element

so either

<?xml version="1.0" encoding="utf-8"?>
<translate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true"
    android:fillEnabled="true"
    android:fromXDelta="0%"
    android:toXDelta="-100%"
    android:duration="500" />

OR

<?xml version="1.0" encoding="utf-8"?>
<set
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true"
    android:fillEnabled="true">
    <translate
        android:fromXDelta="0%"
        android:toXDelta="-100%"
        android:duration="500" />
</set>


回答4:

putting the fillEnabled and fillAfter attributes in the set tag helped solved the issue.