首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安卓: AnimatedVectorDrawable循环

安卓: AnimatedVectorDrawable循环
EN

Stack Overflow用户
提问于 2017-06-23 00:08:10
回答 5查看 13.4K关注 0票数 22

我正在使用AnimatedVectorDrawables玩https://shapeshifter.design/,导出的文件如下所示。我的研究告诉我,为了循环动画,我应该将android:repeatCount=“无限”和android:repeatMode=“重新启动”添加到objectAnimator。

将其添加到objectAnimator中只会重复这些项目中的一个。如何循环整个动画系列?,我希望动画在加载和重复时开始。

动画XML

代码语言:javascript
复制
<animated-vector
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:aapt="http://schemas.android.com/aapt">
    <aapt:attr name="android:drawable">
        <vector
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:width="50dp"
            android:height="50dp"
            android:viewportWidth="50"
            android:viewportHeight="50">
            <path
                android:name="_x34_"
                android:pathData="M 25 12.3 L 39.7 37.7 L 10.3 37.7 Z"
                android:fillColor="#ffffff"
                android:strokeColor="#000000"
                android:strokeWidth="1"
                android:strokeLineCap="round"
                android:strokeLineJoin="round"
                android:strokeMiterLimit="10"/>
        </vector>
    </aapt:attr>
    <target android:name="_x34_">
        <aapt:attr name="android:animation">
            <set xmlns:android="http://schemas.android.com/apk/res/android">
                <objectAnimator
                    android:propertyName="pathData"
                    android:duration="1000"
                    android:valueFrom="M 25 12.3 L 39.7 37.7 L 10.3 37.7 L 17.397 25.437 Z"
                    android:valueTo="M 10 10 L 40 10 L 40 40 L 10 40 Z"
                    android:valueType="pathType"
                    android:interpolator="@android:anim/overshoot_interpolator"/>
                <objectAnimator
                    android:propertyName="pathData"
                    android:startOffset="1000"
                    android:duration="1000"
                    android:valueFrom="M 40 10 L 25.581 10 L 10 10 L 10 40 L 25.349 40 L 40 40 L 40 10"
                    android:valueTo="M 36.3 18.7 L 25 10.4 L 13.7 18.7 L 12.8 31.5 L 25 39.6 L 37.2 31.5 L 36.3 18.7"
                    android:valueType="pathType"
                    android:interpolator="@android:interpolator/fast_out_slow_in"/>
                <objectAnimator
                    android:propertyName="pathData"
                    android:startOffset="2000"
                    android:duration="1000"
                    android:valueFrom="M 36.3 18.7 L 25 10.4 L 13.7 18.7 L 12.8 31.5 L 25 39.6 L 37.2 31.5 Z"
                    android:valueTo="M 25 10.2 L 12.2 17.6 L 12.2 32.4 L 25 39.8 L 37.8 32.4 L 37.8 17.6 Z"
                    android:valueType="pathType"
                    android:interpolator="@android:anim/overshoot_interpolator"/>
                <objectAnimator
                    android:propertyName="pathData"
                    android:startOffset="3000"
                    android:duration="1000"
                    android:valueFrom="M 31.365 13.88 L 25 10.2 L 18.268 14.092 L 12.2 17.6 L 12.2 25.465 L 12.2 32.4 L 25 39.8 L 37.8 32.4 L 37.8 25.581 L 37.8 17.6 L 31.365 13.88"
                    android:valueTo="M 33.7 13 L 25 10.2 L 16.3 13 L 10.9 20.4 L 10.9 29.6 L 16.3 37 L 25 39.8 L 33.7 37 L 39.1 29.6 L 39.1 20.4 L 33.7 13"
                    android:valueType="pathType"
                    android:interpolator="@android:anim/decelerate_interpolator"/>
                <objectAnimator
                    android:propertyName="pathData"
                    android:startOffset="4000"
                    android:duration="1000"
                    android:valueFrom="M 39.1 20.4 L 33.7 13 L 25 10.2 L 16.3 13 L 10.9 20.4 L 10.9 29.6 L 16.3 37 L 25 39.8 L 33.7 37 L 39.1 29.6 L 39.1 20.4"
                    android:valueTo="M 39.7 20 L 32.199 16.173 L 25 12.5 L 17.885 16.13 L 10.3 20 L 10 31 L 16.994 34.031 L 25 37.5 L 32.948 34.056 L 40 31 L 39.7 20"
                    android:valueType="pathType"
                    android:interpolator="@android:interpolator/fast_out_slow_in"/>
                <objectAnimator
                    android:propertyName="pathData"
                    android:startOffset="5000"
                    android:duration="1000"
                    android:valueFrom="M 10.3 20 L 25 12.5 L 39.7 20 L 40 31 L 25 37.5 L 10 31 L 10.3 20"
                    android:valueTo="M 17.995 24.403 L 25 12.3 L 32.23 24.792 L 39.7 37.7 L 25.581 37.7 L 10.3 37.7 L 17.995 24.403"
                    android:valueType="pathType"
                    android:interpolator="@android:interpolator/fast_out_slow_in"/>
            </set>
        </aapt:attr>
    </target>
</animated-vector>

实现动画的java代码如下所示:

代码语言:javascript
复制
final ImageView animationView = (ImageView) findViewById(R.id.animationView);
final AnimatedVectorDrawable drawable = (AnimatedVectorDrawable) getDrawable(R.drawable.avd_dice);
animationView.setImageDrawable(drawable);
drawable.start();
EN

回答 5

Stack Overflow用户

发布于 2018-05-16 12:50:16

您应该使用AnimatedVectorDrawableCompat而不是AnimatedVectorDrawable来支持更低的API,然后也可以编写回调并在其结束时启动动画。最后,您的代码应该如下所示:

代码语言:javascript
复制
val animated = AnimatedVectorDrawableCompat.create(this, R.drawable.avd_dice)
    animated?.registerAnimationCallback(object : Animatable2Compat.AnimationCallback() {
        override fun onAnimationEnd(drawable: Drawable?) {
            animationView.post { animated.start() }
        }

    })
    animationView.setImageDrawable(animated)
    animated?.start()
票数 22
EN

Stack Overflow用户

发布于 2017-07-17 09:32:25

您可以使用Animatable2.AnimationCallback并在onAnimationEnd中再次启动AnimatedVectorDrawable动画。

代码语言:javascript
复制
drawable.registerAnimationCallback(new Animatable2.AnimationCallback() {
    @Override
    public void onAnimationEnd(Drawable drawable) {
        avd.start();
    }
});

drawable.start();
票数 15
EN

Stack Overflow用户

发布于 2018-05-09 08:13:02

在objectAnimator中使用属性

代码语言:javascript
复制
android:repeatMode="restart"
    android:repeatCount="infinite"

你的目标就是这样

代码语言:javascript
复制
 <objectAnimator
    android:propertyName="pathData"
    android:duration="1000"
    android:valueFrom="path value"
    android:valueTo="some value"
    android:valueType="pathType"
    android:repeatMode="restart"
    android:repeatCount="infinite"
    android:interpolator="@android:anim/overshoot_interpolator"/>
票数 15
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44711131

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档