Android - 与图像位置相关的TranslateAnimation

时间:2014-02-10 20:02:04

标签: android animation translate-animation

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

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),它不再移动了。

任何人都可以帮我解决这个问题吗?谢谢。

1 个答案:

答案 0 :(得分:0)

在构建动画时尝试使用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);