public void slideview(final float p1, final float p2) { TranslateAnimation animation = new TranslateAnimation(p1, p2, 0, 0); animation.setInterpolator(new OvershootInterpolator()); animation.setDuration(durationMillis); animation.setStartOffset(delayMillis); animation.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)(p2-p1); int top = view.getTop(); int width = view.getWidth(); int height = view.getHeight(); view.clearAnimation(); view.layout(left, top, left+width, top+height); } }); view.startAnimation(animation); }
slideview(0, -distance);
过程中遇到的问题:
1、动画执行完成后,view回到原位
TranslateAnimation animation = new TranslateAnimation(p1, p2, 0, 0); animation.setInterpolator(new OvershootInterpolator()); animation.setDuration(durationMillis); animation.setStartOffset(delayMillis); view.startAnimation(animation);
2、点击按钮时,view在初始位置会先闪一下,再执行动画
经过查资料得知,animation.setFillAfter(true); 只是将view移动到了目标位置,但是view绑定的点击事件还在原来位置,导致点击时会先闪一下
animation.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)(p2-p1); int top = view.getTop(); int width = view.getWidth(); int height = view.getHeight(); view.clearAnimation(); view.layout(left, top, left+width, top+height); } });
在动画执行完毕后(onAnimationEnd)设置view的位置,同时要clearAnimation()
Android移动view动画问题--停在动画的最后一帧(转),布布扣,bubuko.com
Android移动view动画问题--停在动画的最后一帧(转)
原文:http://www.cnblogs.com/ni-qiu/p/3718962.html