5

这是我需要实现的效果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>

明星动画.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>

但是AnimatorSet不能设置repeatMode

如果我设置objectAnimator's repeatMode,第二个objectAnimator将不会显示。

我该怎么办?

4

3 回答 3

2

在一个线程中循环它直到 ImageView 被销毁怎么办?

完美运行的代码示例:

  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();

或者,您可以使用azizbekian提到的registerAnimationCallback()后跟此 git gist 代码示例

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();
    }
}
于 2017-07-28T07:31:18.870 回答
2

几乎是您想要的,并且在纯 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>

与您想要的不同之处在于路径修剪的方向。但它看起来很棒。

于 2017-07-28T17:34:44.747 回答
1

您可以在动画上设置一个侦听器,并在start()它再次完成后立即:


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

请注意,这registerAnimationCallback()从支持库版本 25.3.0 开始可用。

于 2017-07-24T09:02:44.600 回答