Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Animation Position Resets After Complete

Tags:

android

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:

<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator">    <translate android:fromXDelta="0" android:toXDelta="-100%p" android:duration="500"/></set> 

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.

like image 544
LoneWolfPR Avatar asked Oct 07 '10 14:10

LoneWolfPR


People also ask

What happens if I turn off animations in Android?

The Android operating system often uses animations when you interact with your device. For instance, it might shrink apps into the background when you close them. If you are sensitive to these visual effects, you can use the Remove animations setting to turn them off.

What is twined animation in Android?

A tween animation can perform a series of simple transformations like position, size, rotation, and transparency. on the contents of a View object. So, if you have a TextView or ImageView object, you can move, rotate, grow, or shrink the text or image.


2 Answers

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

<set xmlns:android="http://schemas.android.com/apk/res/android"      android:interpolator="@android:anim/decelerate_interpolator"      android:fillAfter="true">      <translate          android:fromXDelta="0%"         android:toXDelta="-100%"         android:duration="1000"/>  </set> 

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

like image 171
Muhammad Babar Avatar answered Nov 09 '22 21:11

Muhammad Babar


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

like image 20
CommonsWare Avatar answered Nov 09 '22 20:11

CommonsWare