0

iI want that the image goes from the left side to the right side of the screen and then disappears the translateanimation works. but with

img.setVisibility(View.GONE);

everything disappears, not only the image.

public class MainActivity extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ImageView img = (ImageView) findViewById(R.id.img_animation);

        wander(img);

        img.setVisibility(View.GONE);
    }

    public void wander(ImageView img)
    {
        TranslateAnimation animation = new TranslateAnimation(0.0f, 400.0f,0.0f, 0.0f);
        animation.setDuration(500);
        animation.setRepeatCount(1);
        animation.setRepeatMode(1);
        animation.setFillAfter(true);
        img.startAnimation(animation);
    }
}
JohanShogun
  • 2,956
  • 21
  • 30
Joachim
  • 375
  • 1
  • 12

1 Answers1

2

Your current code starts the animation, then hides your image without waiting for the animation to finish. Use a listener like this to determine that the animation is over.

public class MainActivity extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ImageView img = (ImageView) findViewById(R.id.img_animation);

        wander(img);
    }

    public void wander(ImageView img)
    {
        TranslateAnimation animation = new TranslateAnimation(0.0f, 400.0f,0.0f, 0.0f);
        animation.setDuration(500);
        animation.setRepeatCount(1);
        animation.setRepeatMode(1);
        animation.setFillAfter(false);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

           @Override
           public void onAnimationEnd(Animation animation) {
               final ImageView img = (ImageView) findViewById(R.id.img_animation);
               img.setVisibiltiy(View.GONE);
           }

           @Override
           public void onAnimationRepeat(Animation animation) {
           }
        });
        img.startAnimation(animation);
    }
}

For more information: Android Animation Listener

Community
  • 1
  • 1
JohanShogun
  • 2,956
  • 21
  • 30