Android: Animation Position Resets After Complete

匿名 (未验证) 提交于 2019-12-03 07:50:05

问题:

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 to fix this. Here's the xml:

    

Here's the Java that I use to call it:

    homeScrn = (View)findViewById(R.id.homescreen);     slideLeftOut = AnimationUtils.loadAnimation(this, R.anim.slide_left_out);      //Set Click Listeners For Menu     btnHelp.setOnClickListener(new OnClickListener() {         public void onClick(View v) {             LayoutInflater.from(getApplicationContext()).inflate(R.layout.help, (ViewGroup)findViewById(R.id.subpage), true);             homeScrn.startAnimation(slideLeftOut);         }     });

So basically what happens is I inflate a view underneath one. Then I animate the view on top off to the left. As soon as it gets off screen and the animation is finished it resets its position back.

回答1:

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



回答2:

Finally got a way to work around,the right way to do this is setFillAfter(true),

if you want to define your animation in xml then you should do some thing like this

        

you can see that i have defined filterAfter="true" in the set tag,if you try to define it in translate tag it won't work,might be a bug in the framework!!

and then in the Code

Animation anim = AnimationUtils.loadAnimation(this, R.anim.slide_out); someView.startAnimation(anim);

OR

TranslateAnimation animation = new TranslateAnimation(-90, 150, 0, 0);  animation.setFillAfter(true);  animation.setDuration(1800);  someView.startAnimation(animation);

then it will surely work!!

Now this is a bit tricky it seems like the view is actually move to the new position but actually the pixels of the view are moved,i.e your view is actually at its initial position but not visible,you can test it if have you some button or clickable view in your view(in my case in layout),to fix that you have to manually move your view/layout to the new position

public TranslateAnimation (float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)  new TranslateAnimation(-90, 150, 0, 0);

now as we can see that our animation will starts from -90 x-axis to 150 x-axis

so what we do is set

someView.setAnimationListener(this);

and in

public void onAnimationEnd(Animation animation) {    someView.layout(150, 0, someView.getWidth() + 150, someView.getHeight()); }

now let me explain public void layout (int left, int top, int right, int botton)

it moves your layout to new position first argument define the left,which we is 150,because translate animation has animated our view to 150 x-axis, top is 0 because we haven't animated y-axis,now in right we have done someView.getWidth() + 150 basically we get the width of our view and added 150 because we our left is now move to 150 x-axis to make the view width to its originall one, and bottom is equals to the height of our view.

I hope you people now understood the concept of translating, and still you have any questions you can ask right away in comment section,i feel pleasure to help :)

EDIT Don't use layout() method as it can be called by the framework when ever view is invalidated and your changes won't presist, use LayoutParams to set your layout parameters according to your requirement



回答3:

I might be a bit late in replying, but I have the solution for this:

Just add android:fillAfter=true in your xml



回答4:

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



回答5:

You need to add this command:

final Animation animSlideLeft = new TranslateAnimation(0, 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!