Restart AnimatorSet of AnimatedVectorDrawableCompat
Asked Answered
D

3

5

This is I need Realized effect by AnimatedVectorDrawableCompat.

vector_drawable_anim.xml

<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
                 android:drawable="@drawable/vector_drawable">
    <target
        android:name="star"
        android:animation="@animator/star_anim"/>
</animated-vector>

vector_drawable.xml

<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="500px"
        android:height="500px"
        android:viewportHeight="500"
        android:viewportWidth="500">
    <group
        android:name="star_group"
        android:scaleX="5.0"
        android:scaleY="5.0">
        <path
            android:name="star"
            android:pathData="M 50.0,90.0 L 82.9193546357,27.2774101308 L 12.5993502926,35.8158045183 Z"
            android:strokeColor="@color/colorAccent"
            android:strokeWidth="1"/>
    </group>
</vector>

star_anim.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:ordering="sequentially">

    <objectAnimator
        android:duration="1000"
        android:propertyName="trimPathStart"
        android:valueFrom="1"
        android:valueTo="0"/>

    <objectAnimator
        android:duration="1000"
        android:propertyName="trimPathEnd"
        android:valueFrom="1"
        android:valueTo="0"/>

</set>

But the AnimatorSet can't set repeatMode.

If I set objectAnimator's repeatMode, the second objectAnimator won't show.

What should I do?

Dough answered 24/7, 2017 at 8:34 Comment(2)
#27616944Accession
or add android:repeatCount="-1" <objectAnimator android:propertyName="pathData" android:repeatCount="-1"Marenmarena
C
3

What about looping it inside a thread until the ImageView is destroyed?

Perfectly working code sample:

  ImageView animatedImageView = (ImageView) findViewById(R.id.iv);
  AnimatedVectorDrawableCompat animatedVectorDrawable = (AnimatedVectorDrawableCompat) animatedImageView.getDrawable();

        new Thread(new Runnable() {
            public void run() {
                while (animatedImageView != null) {
                    try {
                        animatedImageView.post(new Runnable() {
                            @Override
                            public void run() {
                                animatedVectorDrawable.start();
                            }
                        });
                        Thread.sleep(500);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

Alternatively, you can use the registerAnimationCallback() mentioned by azizbekian followed by this git gist code sample:

import android.app.Activity;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.Animatable2;
import android.graphics.drawable.AnimatedVectorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageView iv = (ImageView) findViewById(R.id.pin);
        final AnimatedVectorDrawable avd = (AnimatedVectorDrawable) iv.getDrawable();
        avd.registerAnimationCallback(new Animatable2.AnimationCallback() {
            @Override
            public void onAnimationEnd(Drawable drawable) {
                avd.start();
            }
        });
        avd.start();
    }
}
Concuss answered 28/7, 2017 at 7:31 Comment(4)
If I use post to delay avd.start() , It can work. I think it maybe was a bug when at onAnimationEnd() the animator isRunning!Dough
Yes! I've personally tested it with working results. @csxConcuss
If I am using your first method, I am getting - Caused by: java.lang.ClassCastException: android.graphics.drawable.AnimatedVectorDrawable cannot be cast to androidx.vectordrawable.graphics.drawable.AnimatedVectorDrawableCompat The Second method is available from 23+.Prescience
@AmanVerma AnimatedVectorDrawable is from android.graphics and AnimatedVectorDrawable is from androidx.vectordrawable. You have mixed up an androix widget with android widget. Check your imports and layout.Concuss
R
3

Almost what you want, and in pure XML:

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:ordering="together">

    <objectAnimator
        android:duration="1000"
        android:propertyName="trimPathStart"
        android:repeatCount="infinite"
        android:startOffset="1000"
        android:valueFrom="1"
        android:valueTo="0" />

    <objectAnimator
        android:duration="1000"
        android:propertyName="trimPathStart"
        android:repeatCount="infinite"
        android:startOffset="1000"
        android:repeatMode="reverse"
        android:valueFrom="0"
        android:valueTo="1" />

</set>

The difference from what you want is the direction of the path trimming back. But it looks awesome.

Rodd answered 28/7, 2017 at 17:34 Comment(0)
K
1

You can set a listener on animation and as soon as it finishes start() it once more:


    AnimatedVectorDrawableCompat avdc = ...;
    avdc.registerAnimationCallback(new Animatable2Compat.AnimationCallback() {
        @Override
        public void onAnimationEnd(Drawable drawable) {
            avdc.start();
        }
    });

Note, that registerAnimationCallback() is available starting from support libs version 25.3.0.

Kus answered 24/7, 2017 at 9:2 Comment(5)
I can't find the Animatable2Compat,how to load Animatable2CompatDough
It is available starting from support libs version 25.3.0.Kus
but the AnimatedVectorDrawableCompat use ObjectAnimator below 21 and the AnimatorSet wouldn't repeat .Dough
Cannot understand what you mean. Can you rephrase? How does updating support library version conflict with "below 21" functionality?Kus
I mean It can't repeat the Animated at android-19.Dough

© 2022 - 2024 — McMap. All rights reserved.