【问题标题】:Stop AnimatedVectorDrawable animation after playing播放后停止 AnimatedVectorDrawable 动画
【发布时间】:2018-10-27 11:57:29
【问题描述】:

我有一个 AnimatedVectorDrawable,它在点击时从播放状态变为暂停状态。

...
 animatedVectorDrawable.registerAnimationCallback(new Animatable2.AnimationCallback() {
                    @Override
                    public void onAnimationEnd(Drawable drawable) {
                      //Reset the image back to its original state

                      //What I've tried so far
                      /* img.setImageResource(R.drawable.original_state)
                        animatedVectorDrawable.stop()
                        animatedVectorDrawable.reset() */
                    }
                });
 animatedVectorDrawable.start();

但是,我无法成功将其恢复到原始状态,以便再次播放。我该如何解决这个问题?

【问题讨论】:

    标签: android animation vector android-vectordrawable animatedvectordrawable


    【解决方案1】:

    你可以这样做:

        final Handler mainHandler = new Handler(Looper.getMainLooper());
        animatedVector.registerAnimationCallback(new Animatable2.AnimationCallback() {
            @Override
            public void onAnimationEnd(final Drawable drawable) {
                mainHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        animatedVectorDrawable.start();
                    }
                });
            }
        });
        animatedVectorDrawable.start();
    

    【讨论】:

    • 这让它来回循环
    • @user8284066 你可以定义一个布尔变量并仅在变量等于 true 时启动动画。
    【解决方案2】:

    要获取可绘制对象的初始状态,请使用animatedVectorDrawable.seekTo(0),您可以调用animatedVectorDrawable.stop()

    【讨论】:

    • .seekTo() 不是函数
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-21
    • 1970-01-01
    • 2013-08-03
    • 2019-10-11
    • 1970-01-01
    • 2012-12-07
    • 1970-01-01
    相关资源
    最近更新 更多