最近在调试android 动画时候发现一个很奇怪问题,网上搜索都说TranslateAnimation 动画运行后,实际位置要在动画结束的监听里面重新设置才会正确,不然物体位置还是在原位。
我根据网上所说之动画监听里面添加了动画结束监听,设置了物体到新位置,发现物体在视图里面还是错位,
后来经过调试发现,必须在动画结束时候移除动画,然后重新设置位置才会正确
AnimationSet set = new AnimationSet(true);
set.addAnimation(translateAnimation);
set.addAnimation(alpha);
set.setDuration(1000);
set.setFillAfter(true);
texas.startAnimation(set);
set.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
texas.clearAnimation();// 增加这句后,重新设置位置,物体才会移动正确
Log.d(TAG,"end Top:" + texas.getTop() + ",Y:" + texas.getY());
RelativeLayout.LayoutParams paramsTexas = new RelativeLayout.LayoutParams(30,30);
paramsTexas.addRule(RelativeLayout.ALIGN_PARENT_TOP);
paramsTexas.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
if(targetdirect == 0)
{
paramsTexas.setMargins(gridLayout.getLeft() + left + playerPanel.getWidth() / 2,(top + playerPanel.getHeight() + 40),0,0);
}
if(targetdirect == 1)
{
paramsTexas.setMargins((left - 40),top + playerPanel.getHeight() / 2,0,0);
}
if(targetdirect == 2)
{
paramsTexas.setMargins(gridLayout.getLeft() + left + playerPanel.getWidth() / 2,top - 60,0,0);
}
if(targetdirect == 3)
{
paramsTexas.setMargins(gridLayout.getLeft() + left + playerPanel.getWidth() + 40,top + playerPanel.getHeight()/2,0,0);
}
texas.setLayoutParams(paramsTexas);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});