简体   繁体   中英

Android delete off screen imageViews

So I have a programatically created ImageView inside of a LinearLayout, and there are two buttons.

One button uses a TranslateAnimation to make the ImageView move left. The other button uses a TranslateAnimation to make the ImageView move right.

I want it so that when the ImageView moves off screen, it is deleted.

I know there's a method called removeView where you can just pass the view into its parameters and the view gets deleted, but I have no clue how to check if the ImageView is off-screen or not.

So in a nutshell, this is what I want to do

if(imageView.isOffScreen()){
     linearLayout.removeView(imageView);
}

Except, isOffScreen() isn't a real method, and I couldn't find any method for ImageViews that would check if it was off screen.

TLDR : How can I check whether or not an ImageView is off-screen? (And by off-screen, I mean you can't see it anymore on the phone screen.)

Since you're using a TranslateAnimation, if you already know that by the end of the animation your view will be off the screen, then you can set up an AnimationListener that will remove the View from the view tree when the animation is finished.

yourAnimation.setAnimationListener(new AnimationListener() {

    public void onAnimationStart(Animation anim) {};

    public void onAnimationRepeat(Animation anim) {};

    public void onAnimationEnd(Animation anim) {
        linearLayout.removeView(imageView);
    };

});      

This way you won't have to check the location of the view at any point, you can simply remove it when the animation is done.

As an aside, is there a reason you want to remove the view, instead of setting Visibilty to GONE?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM