Jai Kotia
Jai Kotia

Reputation: 176

How to set ImageView permanently at a position after TranslateAnimation

I referenced the other questions but couldn't find a solution, also I am fairly new to programming.

So I implemented a TranslateAnimation on my ImageView but once the animation ends it returns to its original position. I used Override onAnimationEnd but that doesn't seem to work. Can someone figure out what should I be doing?

public class PackAnimation extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.pack_animation);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    String s = getIntent().getStringExtra("CHOICE");


    final ImageView pandya = (ImageView) findViewById(R.id.pandya);

    final int amountToMoveRight = 600;
    final int amountToMoveDown = 0;

    TranslateAnimation anim = new TranslateAnimation(0, amountToMoveRight, 0, amountToMoveDown);
    anim.setDuration(100);

    anim.setAnimationListener(new TranslateAnimation.AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) { }

        @Override
        public void onAnimationRepeat(Animation animation) { }

        @Override
        public void onAnimationEnd(Animation animation)
        {
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)pandya.getLayoutParams();
            params.topMargin += amountToMoveDown;
            params.leftMargin += amountToMoveRight;
            pandya.setLayoutParams(params);
        }
    });

    pandya.startAnimation(anim);
}

 }

Upvotes: 1

Views: 261

Answers (2)

Bob
Bob

Reputation: 13865

You can use anim.setFillAfter(true) to the TranslateAnimation and get the button stay at the new position, but the button clicks will not work at the new position.

So use ViewPropertyAnimator or ObjectAnimator. These are property animation and will change the position of the View, whereas the TranslateAnimation is View animation and will not change the actual property of the View.

For example, using ViewPropertyAnimator:

pandya.animate()
    .translationX(amountToMoveRight)
    .translationY(amountToMoveDown);

Refer the blog for more info:

Finally, the previous animations changed the visual appearance of the target objects... but they didn't actually change the objects themselves. You may have run into this problem. Let's say you want to move a Button from one side of the screen to the other. You can use a TranslateAnimation to do so, and the button will happily glide along to the other side of the screen. And when the animation is done, it will gladly snap back into its original location. So you find the setFillAfter(true) method on Animation and try it again. This time the button stays in place at the location to which it was animated. And you can verify that by clicking on it - Hey! How come the button isn't clicking? The problem is that the animation changes where the button is drawn, but not where the button physically exists within the container. If you want to click on the button, you'll have to click the location that it used to live in. Or, as a more effective solution (and one just a tad more useful to your users), you'll have to write your code to actually change the location of the button in the layout when the animation finishes.

Upvotes: 1

A. Badakhshan
A. Badakhshan

Reputation: 1043

Use anim.setFillAfter(true); and it will remain on the final position after the end of animation.

Upvotes: 0

Related Questions