10

我正在屏幕上制作气泡动画,但完成动画时间后气泡停止。如何重复动画或使其无限?

bub.animate();
bub.animate().x(x2).y(y2);
bub.animate().setDuration(animationTime);       
bub.animate().setListener(new AnimatorListenerAdapter() {

    @Override
    public void onAnimationStart(Animator animation) {
        animators.add(animation); 
    } 

    @Override
    public void onAnimationRepeat(Animator animation) {
    }

    @Override
    public void onAnimationEnd(Animator animation) {
    }
});
4

6 回答 6

13

由于ViewPropertyAnimator仅适用于简单动画,因此请使用更高级的ObjectAnimator类 - 基本上是方法setRepeatCount和另外的 setRepeatMode

于 2014-08-17T00:08:51.027 回答
7

这实际上是可能的。这是旋转视图的示例:

        final ViewPropertyAnimator animator = view.animate().rotation(360).setInterpolator(new LinearInterpolator()).setDuration(1000);

        animator.setListener(new android.animation.Animator.AnimatorListener() {
            ...

            @Override
            public void onAnimationEnd(final android.animation.Animator animation) {
                animation.setListener(null);
                view.setRotation(0);
                view.animate().rotation(360).setInterpolator(new LinearInterpolator()).setDuration(1000).setListener(this).start();
            }

        });

您也可以使用“ withEndAction ”代替监听器。

于 2016-01-06T14:01:16.027 回答
6

您可以使用CycleInterpolator. 例如,像这样:

    int durationMs = 60000;
    int cycleDurationMs = 1000;
    view.setAlpha(0f);
    view.animate().alpha(1f)
            .setInterpolator(new CycleInterpolator(durationMs / cycleDurationMs))
            .setDuration(durationMs)
            .start();
于 2016-11-02T16:41:49.537 回答
6

这是 Kotlin 中的一个示例,通过在withEndAction中递归调用它来重复动画的简单方法

例子

private var animationCount = 0

private fun gyrate() {
    val scale = if (animationCount++ % 2 == 0) 0.92f else 1f
    animate().scaleX(scale).scaleY(scale).setDuration(725).withEndAction(::gyrate)
}

这会反复动画视图的大小,使其变小、恢复正常、变小、恢复正常等。这是一个非常简单的模式,可以重复您想要的任何动画。

于 2020-06-28T16:38:35.137 回答
0

在科特林你可以这样做。创建一个可运行的。在其中动画视图并设置withEndAction为可运行本身。最后运行可运行动画开始。

var runnable: Runnable? = null
runnable = Runnable {
    view.animate()
        .setDuration(10000)
        .rotationBy(360F)
        .setInterpolator(LinearInterpolator())
        .withEndAction(runnable)
        .start()
}
runnable.run()
于 2020-10-12T04:09:34.330 回答
0
final ViewPropertyAnimator animator = view.animate().rotation(360).setInterpolator(new LinearInterpolator()).setDuration(1000); //Animator object

    animator.setListener(new android.animation.Animator.AnimatorListener() {
        ...

        @Override
        public void onAnimationEnd(final android.animation.Animator animation) {
            animation.setListener(this); //It listens for animation's ending and we are passing this to start onAniationEnd method when animation ends, So it works in loop
            view.setRotation(0);
            view.animate().rotation(360).setInterpolator(new LinearInterpolator()).setDuration(1000).setListener(this).start();
        }

    });
于 2018-06-30T16:53:40.257 回答