完成后,TranslateAnimation跳回到开始点

Bra*_*rad 1 android android-animation

我的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);
Run Code Online (Sandbox Code Playgroud)

如何让它留在最终位置?

mpo*_*isf 8

旧的(3.0之前的)动画API表现得有些奇怪 - 当它动画时,它只是转换绘制视图的位置,但实际上并不会相对于其父视图移动视图.

为了获取视图留在原地当动画完成后,你可以设置Animation.AnimationListener你的TranslateAnimation.在onAnimationEnd()侦听器的方法中,通过操纵视图将视图移动到其最终的静止位置LayoutParams.

  • 另一个值得一提的重要事项是,在处理动画结束时会有一些奇怪的错误,其中内容可能会意外移动.这可以通过在onAnimationEnd内部对象上调用clearAnimation,或者通过子类化视图,直接在子类中实现onAnimationEnd来解决.请参阅:http://stackoverflow.com/questions/4750939/android-animation-is-not-finished-in-onanimationend/5641518#comment9690906_5641518 (2认同)

Yas*_*mar 8

使用animation.setFillAfter(true)您的动画在翻译月底停止它.

http://developer.android.com/reference/android/view/animation/Animation.html#setFillAfter(boolean)

  • *看起来像它有效,但就像在链接中所说的那样,点击事件在之后的*原始*位置处理.这就像屏幕被绘制在适当的位置,但控件仍然是他们原来的位置. (4认同)