使用TranslateAnimation特效后view会回到原来的位置,使用setFillAfter(true)后view位置不会返回但是view的实际位置还是原来的。
可以设置setAnimationListener在动画效果结束之后再将view进行位移,但是onAnimationEnd是在动画效果执行之后再执行的所以位移
view会造成布局闪一下可以使用clearAnimation()可以解决问题
TranslateAnimation translateAnimation = new TranslateAnimation(0.0f, 0.0f,0.0f,-60.0f); translateAnimation.setDuration(500); translateAnimation.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { view.clearAnimation(); int left = view.getLeft(); int top = view.getTop()-60; int width = view.getWidth(); int height = view.getHeight(); view.layout(left, top, left+width, top+height); } });
参考:http://www.cnblogs.com/eoiioe/archive/2012/08/29/2662546.html
http://stackoverflow.com/questions/8411903/android-translateanimation-on-button-flickers
PS:网上有人测出此方法在2.3中可能会异常
http://www.eoeandroid.com/forum.php?mod=redirect&goto=findpost&ptid=63857&pid=642522
另付方法如下
TranslateAnimation translateAnimation = new TranslateAnimation(0.0f, 0.0f,0.0f,-60.0f); translateAnimation.setDuration(500); translateAnimation.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { int left = view.getLeft(); int top = view.getTop()-60; int width = view.getWidth(); int height = view.getHeight(); view.layout(left, top, left+width, top+height); TranslateAnimation anim = new TranslateAnimation(0,0,0,0); view.setAnimation(anim); } });
最后还有其他解决方法,有兴趣的自己看看http://bbs.csdn.net/topics/360035885
TranslateAnimation动画结束后View的位移,布布扣,bubuko.com
TranslateAnimation动画结束后View的位移
原文:http://www.cnblogs.com/swenze/p/3643445.html