0

I trying to move a image from one place to other using animation,but after moving it coming back to original position, how to stop it to the moved position itself.

5 Answers5

1

After an Animation is done, use the method setFillAfter(true), to make the last animation state persist:

If fillAfter is true, the transformation that this animation performed will persist when it is finished.

Animation.setFillAfter/Before - Do they work/What are they for?

If you need to do something more specific, you could also set an animation listener and move your object at the end of the animation:

animation1.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                //change according to your needs
                myView.setX(0);
            }

            @Override
            public void onAnimationRepeat(Animation animation) { }
        });
Community
  • 1
  • 1
JDM
  • 892
  • 7
  • 9
1

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);
Pavan Nagadiya
  • 477
  • 3
  • 8
1

to let the image in the last place of the animation , try this :

TranslationAnimation ta = new TranslateAnimation(fromX, toX, 0, 0);
ta.setDuration(1000);
ta.setFillAfter(true); // this will let the image in the last place of the Animation
imageView.startAnimation(ta);
Houcine
  • 22,593
  • 13
  • 53
  • 83
0

See this blog post for a solution:

// first set the view's location to the end position
view.setLayoutParams(...);  // set to (x, y)

// then animate the view translating from (0, 0)
TranslationAnimation ta = new TranslateAnimation(-x, -y, 0, 0);
ta.setDuration(1000);
view.startAnimation(ta);
Vincent Mimoun-Prat
  • 26,900
  • 13
  • 74
  • 118
0

I'm sure you would have found the answer by now (I just did... so I am posting for others). Android seems to have officially shifted to a new animation framework called "Property Animation". This has been available since Honeycomb (3.0). This will essentially solve your problem as it animates the actual property values.

DEV GUIDE: http://developer.android.com/guide/topics/graphics/prop-animation.html

dineth
  • 9,042
  • 6
  • 28
  • 38