完成后如何让动画再次运行?

How to get animation to work again after complete?

单击按钮时,我想让图像向下或向右移动。我 运行 遇到的问题是图像的移动只发生一次。在 onClick() 方法中,我只是调用我的移动方法之一。以下是移动方法。

private void moveLeftToRight() {
    animation = new TranslateAnimation(0.0f, 200.0f, 0.0f, 0.0f);
    animation.setDuration(1500); // animation duration
    animation.setFillAfter(true);
    iv1.startAnimation(animation); // start animation
}

private void moveUpToDown() {
    animation = new TranslateAnimation(0.0f, 0.0f, 0.0f, 200.0f);
    animation.setDuration(1500); // animation duration
    animation.setFillAfter(true);
    iv1.startAnimation(animation); // start animation
}

我第一次可以调用其中任何一个。此后动画方法不起作用。我想知道我是否需要重置或其他东西。有任何想法吗?提前致谢。

当您将坐标传递给 TranslateAnimation 时,这些坐标是相对于 0,0 而不是视图坐标。

为了解决这个问题,我们将使用 view.bottomview.left

获取视图坐标

然后将您要转换的值添加到这些值,将它们传递给 TranslateAnimation,您的翻译就会按预期工作。