Search code examples
androidanimationandroid-vectordrawablehuawei-mobile-servicesanimatedvectordrawable

AnimatedVectorDrawable - reset() is not working properly on some Huawei devices


We use AnimatedVectorDrawable.reset() to reset the animation to its initial state. This works well on most of the devices. While testing we noticed that on some Huawei devices when reset() is called the animation starts running.

Reproduced on these Huawei devices:

  • ANE-LX1 - Android 8.0.0, EMIU 8.0.0
  • LYA-L29 - Android 10, EMIU 10.0.0

Any ideas what can I do to fix the problem?


Solution

  • To fix the problem I updated the animation to end in the state it starts. This allowed me to stop using AnimatedVectorDrawable.reset().

    Section "Stale state" from this article helped me resolve the problem. Here is the important part of the section:

    On older devices I found that their ‘state’ wasn’t being reset (to how it was defined in the VectorDrawable) on each loop. ... To fix this I added a zero length animation to set properties to their expected value at the start of each loop so that they’re ready to be animated e.g.:

    <set>
        <objectAnimator
            android:propertyName="fillAlpha"
            android:valueFrom="0"
            android:valueTo="0"
            android:duration="0" />
        <objectAnimator
            android:propertyName="fillAlpha"
            android:valueFrom="0"
            android:valueTo="1"
            android:startOffset="1900"
            android:duration="60"
            android:interpolator="@android:interpolator/linear" />
    </set>
    

    This is the part that resets the animation to its initial state:

        <objectAnimator
            android:propertyName="fillAlpha"
            android:valueFrom="0"
            android:valueTo="0"
            android:duration="0" />
    

    valueFrom and valueTo are equal to valueFrom in the second objectAnimator.