【发布时间】:2015-05-02 10:13:27
【问题描述】:
作为我项目的一部分,我需要为 Android 设备创建棋盘(可以移动棋子)。在我决定为片段动作添加动画之前,一切都运行良好。现在问题如下:如果代码看起来像您可能在下面看到的代码,则行为如下:
- 首先,X 棋子按我的预期移动;
- 但是当我开始为下一个移动设置动画时,X 块的图像会出现(尽管相应的 ImageView 保持为空)在上一个移动之前它所在的单元格中。我的意思是,它的副本出现在之前的位置,同时它还停留在当前单元格中;
final ImageView movingPiece = (ImageView) findViewById(prevCellId);
TranslateAnimation animation = new TranslateAnimation(0, dx, 0, dy);
animation.setDuration(500);
animation.setFillBefore(true);
animation.setFillAfter(true);
animation.setAnimationListener(new Animation.AnimationListener() {
public void onAnimationEnd(Animation animation) {
setPiece(prevCellId, null);
setPiece(newCellId, chosenPiece);
movingPiece.clearAnimation();
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationStart(Animation animation) {
}
});
movingPiece.startAnimation(animation);
为了使有关代码的一些要点清楚:setPiece 获取要放入一些内容的单元格的 id 和要放入单元格的对象。在方法内部,我将相应的 ImageView ImageResource 替换为相应的图片或 android.R.color.transparent(如果第二个参数是 null)。
请向我解释发生了什么以及如何实现所需的行为(我的意思是,没有奇怪地重新出现在其先前位置上的片段)。
提前致谢
【问题讨论】:
标签: android android-animation translate-animation