【问题标题】:Android - TranslateAnimation related to image positionAndroid - 与图像位置相关的 TranslateAnimation
【发布时间】:2014-02-11 04:05:42
【问题描述】:

我正在制作翻译动画,但遇到了这个问题。

imgLogo = new ImageButton(this);
imgLogo.setLayoutParams(new ViewGroup.LayoutParams(100, 100));
imgLogo.setBackgroundResource(R.drawable.twitter);
imgLogo.setX(0.0f);
imgLogo.setY(0.0f);
layout.addView(imgLogo);

final TranslateAnimation moveRight = new TranslateAnimation(0.0f, 410.0f,0.0f, 0.0f);

moveRight.setFillAfter(true);
moveRight.setFillEnabled(true);
moveRight.setDuration(1000);

动画在这段代码中运行良好,图像从 (0,0)->(410,0) 移动。

但是,当我改变图像 x=0 的位置时; y=800。然后运行动画,图像只是在(0,800)处消失并出现在(410,800)处,不再移动。

谁能帮我解决这个问题?谢谢。

【问题讨论】:

  • 你能指出你想用这个动画实现什么吗?另外我不认为使用屏幕的像素大小来生成动画的最佳方式
  • 我希望图像在任何时候从左到右或从右到左移动。起初,我尝试使用 xml,但它没有用。在我的代码中,图像位于 (0, 0) 并移动到 (410, 0)。如果位置是 (0, 800),那么它应该移动到 (410, 800),对吗?但它只是在 (0, 800) 处消失并出现在 (410, 800) 处。 :(
  • 试试我的答案,如果您认为我的答案对您来说不完整,我可以发布更完整的代码,但我认为您只需使用那行代码即可

标签: android animation translate-animation


【解决方案1】:

在构建动画时尝试使用 RELATIVE_TO_PARENT,只是一个从右向左或从左向右滑动的示例:

Animation slide = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, right?+1.0f:-1.0f,Animation.RELATIVE_TO_PARENT, 0.0f,Animation.RELATIVE_TO_PARENT, 0.0f,Animation.RELATIVE_TO_PARENT, 0.0f); 

【讨论】:

  • 感谢您的回答。但问题是图像的位置。我已经使用位置 (0, 0) 尝试了您的代码。动画幻灯片 = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 对吗?+1.0f:-1.0f,Animation.RELATIVE_TO_PARENT, 0.0f,Animation.RELATIVE_TO_PARENT, 0.0f,Animation.RELATIVE_TO_PARENT, 0.0f);它在移动。但是对于位置 (0, 800),它失败了。
  • 我找到了解决方案。它与图像的 x,y 相关。我已将其更改为 setMargin() 而不是使用 setX 和 setY :)
  • 不要忘记在此处发布您的答案,以帮助其他有相同问题的人
  • 我确实找到了解决方案。我没有使用 setX()、setY(),而是改为 setMargins()。并正常使用动画。 marginParams2.setMargins(37, 898, 0, 0); btnFriend.setLayoutParams(layoutParams2);此外,如果有人需要在平移动画后更新对象的真实位置,那么应该使用:ObjectAnimator。 ObjectAnimator transAnimation= ObjectAnimator.ofFloat(btnFriendOF, "translationX", -192, 0); transAnimation.setDuration(400);//设置持续时间 transAnimation.start();
猜你喜欢
  • 2012-01-17
  • 1970-01-01
  • 2023-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多