简体   繁体   English

Android删除屏幕外的imageViews

[英]Android delete off screen imageViews

So I have a programatically created ImageView inside of a LinearLayout, and there are two buttons. 因此,我在LinearLayout内以编程方式创建了ImageView,并且有两个按钮。

One button uses a TranslateAnimation to make the ImageView move left. 一个按钮使用TranslateAnimation使ImageView向左移动。 The other button uses a TranslateAnimation to make the ImageView move right. 另一个按钮使用TranslateAnimation使ImageView向右移动。

I want it so that when the ImageView moves off screen, it is deleted. 我想要它,以便当ImageView移出屏幕时,将其删除。

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. 我知道有一个名为removeView的方法,您可以在其中将视图传递到其参数中,然后删除该视图,但是我不知道如何检查ImageView是否在屏幕外。

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. 除了isOffScreen()并不是一个真正的方法,而且我找不到用于ImageViews的任何方法来检查它是否在屏幕外。

TLDR : How can I check whether or not an ImageView is off-screen? TLDR:如何检查ImageView是否在屏幕外? (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. 由于您使用的是TranslateAnimation,如果您已经知道到动画结束时您的视图将不在屏幕上,则可以设置AnimationListener,在动画完成时将其从视图树中删除。

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? 顺便说一句,您是否有理由要删除视图,而不是将Visibilty设置为GONE?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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