TranslateAnimation 完成后跳回起点
我的 TranslateAnimation 可以工作,但在动画结束时,它会跳回原始位置。
LinearLayout rl = (LinearLayout) findViewById(R.id.navPanel);
animation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 3.0f
);
animation.setDuration(500);
rl.startAnimation(animation);
我怎样才能让它停留在最终位置?
My TranslateAnimation works, but at the end of the animiation, it jumps back to the original location.
LinearLayout rl = (LinearLayout) findViewById(R.id.navPanel);
animation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 3.0f
);
animation.setDuration(500);
rl.startAnimation(animation);
How can I make it stay at the end location?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
旧的(3.0 之前的)动画 API 的行为有点奇怪 - 当它制作动画时,它只是转换视图的绘制位置,但实际上并不相对于其父视图移动视图。
为了让视图在动画完成时保持不动,您可以在
TranslateAnimation
上设置一个Animation.AnimationListener
。在侦听器的 onAnimationEnd() 方法中,您可以通过操作视图上的 LayoutParams 将视图移动到其最终静止位置。The old (pre-3.0) Animation API behaves a bit weird - when it's animating, it merely translates where the view is drawn, but doesn't actually move the view with respect to its parents.
In order to get the view to stay put when the animation is done, you can set an
Animation.AnimationListener
on yourTranslateAnimation
. In theonAnimationEnd()
method of the listener, you move the view to its final resting place by manipulating theLayoutParams
on the view.对动画使用
animation.setFillAfter(true)
使其在翻译结束时停止。http://developer.android.com/reference/android/view/animation/ Animation.html#setFillAfter(布尔值)
use
animation.setFillAfter(true)
for your animation to stop it at the end of translation.http://developer.android.com/reference/android/view/animation/Animation.html#setFillAfter(boolean)
同样的问题 https://stackoverflow.com/a/6519233/1253065
使用
动画.setFillAfter(true) , 动画.setFillEnabled(true)
this same problem https://stackoverflow.com/a/6519233/1253065
use
animation.setFillAfter(true) , animation.setFillEnabled(true)
为了避免重复代码,您可以子类化 AnimatorListenerAdapter 并创建您自己的 AnimatorListener 类,在动画之后重新附加视图,然后在需要的地方使用它而不是默认的 AnimatorListener。
类:
以及如何使用它:
该解决方案假设您使用relativelayout或framelayout作为父级。此外,它只关心位置,而不关心大小或其他可以设置动画的属性。
To avoid repetitive code, you can subclass the AnimatorListenerAdapter and create your own AnimatorListener class where you re-attach the view after the animation, then use it wherever needed instead of the default AnimatorListener.
The class:
And how you use it:
This solution assumes you are using RelativeLayout or FrameLayout for the parent. Also, it just cares about the position, not size or other properties that can be animated as well.