Reputation: 7866
On button click I would like to make an image move either down or right. The issue I am running into is that the movement of the image only occurs once. In onClick() method I am just calling either one of my move methods. Here are the move methods.
private void moveLeftToRight() {
animation = new TranslateAnimation(0.0f, 200.0f, 0.0f, 0.0f);
animation.setDuration(1500); // animation duration
animation.setFillAfter(true);
iv1.startAnimation(animation); // start animation
}
private void moveUpToDown() {
animation = new TranslateAnimation(0.0f, 0.0f, 0.0f, 200.0f);
animation.setDuration(1500); // animation duration
animation.setFillAfter(true);
iv1.startAnimation(animation); // start animation
}
I can call either one of these one time for the first time. Thereafter the animation methods do not work. I am wondering if I need to reset or something. Any ideas? Thanks in advance.
Upvotes: 2
Views: 59
Reputation: 3568
When you pass your coordinates to TranslateAnimation
those coordinates are relative to 0,0 and not the views coordinates.
To fix this we will get the views coordinates with view.bottom
, view.left
etc.
Then add the values you'd like to transform by to those values, pass those to TranslateAnimation
, and your translation will work as expected.
Upvotes: 2