Android TranslationAnimation 无法正常工作

发布于 2024-11-06 10:46:07 字数 934 浏览 0 评论 0原文

我正在尝试在屏幕上制作卡片动画。

我的代码如下所示:

private void animate(ImageView  animationSource, ImageView  animationTarget, int animationDuration, int animationDelay) {
    int[] animationSourcePosition = {0,0};
    int[] animationTargetPosition = {0,0};

    animationSource.getLocationInWindow(animationSourcePosition);
    animationTarget.getLocationInWindow(animationTargetPosition);

    TranslateAnimation cardDealingAnimation = new TranslateAnimation(
        Animation.ABSOLUTE,animationSourcePosition[0],
        Animation.ABSOLUTE,animationTargetPosition[0],
        Animation.ABSOLUTE,animationSourcePosition[1],
        Animation.ABSOLUTE,animationTargetPosition[1]);

    cardDealingAnimation.setDuration(animationDuration);
    cardDealingAnimation.setStartOffset(animationDelay);
    animationTarget.startAnimation(cardDealingAnimation);
}

发生的情况是它只是弹出在新位置,而不显示中间的位置。 有趣的是,在我的一个目标上,它有时确实会出现。

I am trying to animate cards across the screen.

My code looks like this:

private void animate(ImageView  animationSource, ImageView  animationTarget, int animationDuration, int animationDelay) {
    int[] animationSourcePosition = {0,0};
    int[] animationTargetPosition = {0,0};

    animationSource.getLocationInWindow(animationSourcePosition);
    animationTarget.getLocationInWindow(animationTargetPosition);

    TranslateAnimation cardDealingAnimation = new TranslateAnimation(
        Animation.ABSOLUTE,animationSourcePosition[0],
        Animation.ABSOLUTE,animationTargetPosition[0],
        Animation.ABSOLUTE,animationSourcePosition[1],
        Animation.ABSOLUTE,animationTargetPosition[1]);

    cardDealingAnimation.setDuration(animationDuration);
    cardDealingAnimation.setStartOffset(animationDelay);
    animationTarget.startAnimation(cardDealingAnimation);
}

What happens is that it just pops up in the new place without showing the places in-between.
Funnily enough, on one of my targets it does sometimes show up.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

¢蛋碎的人ぎ生 2024-11-13 10:46:07

我在这里看到两个问题。

一个问题是您要获取对象在窗口中的位置,而您应该只使用对象在其父容器中的位置。它更容易、更快速,而且更符合您的需求 - 对象位于其布局中。如果它们不在同一个容器中,那么您可能需要获取窗口或屏幕中的位置,然后仅处理增量值(它们位置的差异,而不是绝对位置本身)。

您可以通过 View 上的 getLeft() 和 getTop() 方法获取这些简单的容器位置。

第二个问题是 TranslateAnimation 参数的作用与您想象的不同。数值(第二个、第四个、第六个和第八个值)将目标对象的起始位置和结束位置指定为其当前位置的偏移量。这就是为什么他们在方法声明中包含“delta”一词。因此,您不应该发送对象位置的绝对坐标,而应该发送它们应该具有的增量。

假设您已将 xDelta 和 yDelta 计算为源位置和目标位置之间的 x 和 y 差值。如果您要将源动画设置到目标位置,那么您可以使用 0、xDelta、0、yDelta 等值作为这些值。

请注意,TranslateAnimation() 并不实际移动对象 - 它只是将其绘制在不同的位置。因此,一旦动画完成,对象就会恢复到其原始位置。您可能需要设置 fillAfter 属性以在动画完成时保持其在屏幕上的结束位置。

There are two problems I see here.

One problem is that you're getting the position of your objects in the Window, where you should just use the position of the objects in their parent container. It's easier, faster, and more what you're looking for - where the objects live in their layout. If they do not live in the same container, then you may need to get the position in the window or the screen, and then just deal with delta values (the difference in their locations, not the absolute locations themselves).

You can get these simple container positions from the getLeft() and getTop() methods on View.

The second problem is that the TranslateAnimation parameters don't do what you think they do. The numeric values (the second, fourth, sixth, and eighth values) specify the starting and ending locations of the target object as offsets from its current location. That's why they have the word 'delta' in them in the method declaration. So you should not send in the absolute coordinates of the object positions their, but, rather, the deltas that they should have.

Let's suppose you have calculated an xDelta and yDelta as the difference in x and y between the source and target positions. If you're animating the source to the target position, then you would use something like 0, xDelta, 0, yDelta for these values.

Note that TranslateAnimation() does not physically move the object - it just draws it in a different place. So as soon as the animation is complete, the object will snap back to its original location. You may want to set the fillAfter property to maintain its end location on the screen when the animation completes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文