如何在 Android 上制作弹跳动画?

How to do a bounce animation on Android?

我还不擅长 Android 动画,但我需要实现像这样的弹跳动画 Animation :

我一直在尝试 BounceInterpolatorandroid:interpolator="@android:anim/bounce_interpolator" translate +scale animations 但我没有得到效果

注:如果能给个说明就更好了

您看到的基本上只是两个以重复模式链接在一起的 TranslateAnimation。由于白线从屏幕左侧和右侧离开,因此它看起来就像在弹跳。 ObjectAnimator class 应该提供您需要的一切。假设白线是位于屏幕中央的 View v。 像 :

final ObjectAnimator animation = ObjectAnimator.ofFloat(v, "translationX", ...f);
    animation.setDuration(ms);
    animation.start();

应该让你开始,其中 (...f) 参数是将其向右移动的正浮点值或将其向左移动的负值在 X 轴上。

个人比较喜欢ViewPropertyAnimatorClass,它可以把多个动画串起来,非常好用。 f.e.:

v.animate()
    .translationXBy(...f)
    .setInterpolator(new AccelerateDecelerateInterpolator())
    .setDuration(ms);

此处与 (...f) 参数相同。 Interpolator 是可选的,只是让它看起来更逼真。 对于未来的动画,我建议检查 docs 所有可用的方法。

您可以设置一个 Animator.Listener,允许您执行操作 f.e。当动画结束时。所以f.e。一旦右边的动画结束,你就可以开始左边的动画,反之亦然,但我宁愿给你一些工作来找出如何重复动画,而不是为你做所有的工作。 我只能补充一点,我不建议您使用 AnimationSet, which has a method setRepeatMode(),这可能对您的任务有好处,但根据我的经验,我可以说,这是相当有问题的,而且通常无法按预期工作。