当我尝试在 ImageView
上应用翻译动画时,图像完全消失了。我不确定为什么会发生这种情况,因为我通过 eclipse 调试器运行了这段代码,并且 x_start 和 x_final 的值似乎是正确的。
关于为什么会发生这种情况或者我如何最终让我的 TranslateAnimation
工作有什么想法吗?
chargeButton.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int x_cord = (int) event.getRawX();
int y_cord = (int) event.getRawY();
switch (event.getActionMasked()){
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
chargeButton.setLayoutParams(setPosition(x_cord, false));
break;
case MotionEvent.ACTION_UP:
int x_start = x_cord;
x_cord = 0;
slowMove(x_start, x_cord, false);
chargeButton.setLayoutParams(setPosition(x_cord, false));
break;
default:
break;
}
return true;
}
});
}
public void slowMove(int x_start, int x_final, boolean pay)
{
Animation transAnimation = new TranslateAnimation(x_start, x_final, 0, 0);
transAnimation.setFillAfter(true);
transAnimation.setDuration(1000);
if (pay)
payButton.startAnimation(transAnimation);
else
{
chargeButton.clearAnimation();
chargeButton.startAnimation(transAnimation);
}
}
更多背景信息,此函数是从 MotionEvent.ACTION_UP
情况下的 onTouchListener
调用的。
我觉得好像我需要使用 Animation.Relative_TO_SELF 或类似的东西来定位。但是,当我只有 ImageViews 的绝对定位 时,我不确定该怎么做。
我们将不胜感激任何和所有的想法。
最佳答案
不要使用布局参数来移动 View - 这真的不是一个好主意。我一直沿着那条路走下去,但没有什么好结果。
考虑扩展 View 并设置您自己的自定义 View ,您可以在其中完全控制发生的事情。
关于Android - 翻译动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8192417/