标签:
http://www.cnblogs.com/eoiioe/archive/2012/08/29/2662546.html
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回到原位
开始时动画效果只写了这么多,发现动画执行完,view会回到原位。
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()
标签:
原文地址:http://www.cnblogs.com/zhou-guobao/p/4647970.html