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
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
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
).
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);