Joachim
Joachim

Reputation: 375

Image don'T get invisible

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);
    }
}

Upvotes: 0

Views: 74

Answers (1)

JohanShogun
JohanShogun

Reputation: 2976

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

Upvotes: 2

Related Questions