Android: Animation Position Resets After Complete

后端 未结 9 1474
南笙
南笙 2020-11-27 13:30

I\'m using an xml defined animation to slide a view off the screen. The problem is, as soon as the animation completes it resets to its original position. I need to know how

相关标签:
9条回答
  • 2020-11-27 13:54

    You can use

    fillAfter=true
    fillEnabled=true
    

    your View will'not reset to original position, but if you have some buttons or something else in your view , their position will not change.

    You must use ObjectAnimator , it works from API 11 level . It changes View position automatic,

    here is the example

    ObjectAnimator objectAnimator= ObjectAnimator.ofFloat(mContent_container, "translationX", startX, endX);
    objectAnimator.setDuration(1000);
    objectAnimator.start();
    

    Thanks JUL for his answer

    If your app not found object animator, change the API level from Project -> properties -> Android , than import android.animation.ObjectAnimator;

    Regards Hayk

    0 讨论(0)
  • 2020-11-27 13:55

    The animations are working as expected. Just because you animated something does not mean you actually moved it. An animation only affects drawn pixels during the animation itself, not the configuration of the widgets.

    You need to add an AnimationListener, and in the onAnimationEnd() method, do something that makes your move permanent (e.g., removes the view on top from its parent, marks the view on top as having visibility GONE).

    0 讨论(0)
  • 2020-11-27 13:56

    I went to the same question and solved it with setting the marginLeft at OnAnimationEnd()..

    MarginLayoutParams params = (MarginLayoutParams) this.getLayoutParams(); params.setMargins(this.getWidth()*position, 0,0,0); this.setLayoutParams(params);

    0 讨论(0)
提交回复
热议问题