无法使视图使用TranslateAnimation后完全消失 [英] Unable to make view completely GONE after using TranslateAnimation

查看:420
本文介绍了无法使视图使用TranslateAnimation后完全消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用了 TranslateAnimation 和上下滑动的景色。

I had used TranslateAnimation and slide up and down a view.

不过,我知道,即使在我滑下的观点,并在其知名度使用 View.GONE ,认为仍然能够接收触摸事件。

However, I realize, even though after I slide down the view, and use View.GONE in its visibility, the view still able to receive touch event.

您可以产生同样的问题,通过点击按钮,使橙色视图从屏幕底部消失。然后,当你点击屏幕的底部,你会发现仍然被触发自定义视图的触摸事件。

You can produce the same problem, by clicking on the button to make the orange color view disappear from the bottom of the screen. Then, when you click on the bottom of the screen, you will realize touch event of the custom view is still being triggered.

public class MainActivity extends Activity {

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

        int color = getResources().getColor(android.R.color.holo_orange_light);
        // construct the RelativeLayout
        final RelativeLayout customView = new RelativeLayout(this) {
            @Override
            public boolean onTouchEvent(MotionEvent event) {
                this.setPressed(true);
                Log.i("CHEOK", "OH NO! TOUCH!!!!");
                return super.onTouchEvent(event);
            }
        };
        customView.setBackgroundColor(color);

        final FrameLayout frameLayout = (FrameLayout)this.findViewById(R.id.frameLayout); 
        frameLayout.addView(customView, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, 100, Gravity.BOTTOM));
        customView.setVisibility(View.GONE);


        Button button = (Button)this.findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (customView.getVisibility() != View.VISIBLE) {
                    // Slide up!
                    TranslateAnimation anim=new TranslateAnimation(0,0,100,0);
                    anim.setFillAfter(true);
                    anim.setDuration(200);

                    Log.i("CHEOK", "VISIBLE!!!");
                    customView.setVisibility(View.VISIBLE);         
                    customView.setAnimation(anim);   
                    customView.setEnabled(true);
                } else {
                    // Slide down!
                    TranslateAnimation anim=new TranslateAnimation(0,0,0,100);
                    anim.setFillAfter(true);
                    anim.setDuration(200);            

                    // HELPME : Not sure why, after I hide the view by sliding it down,
                    // making it View.GONE and setEnabled(false), it still able to 
                    // receive touch event.
                    Log.i("CHEOK", "GONE!!!");
                    customView.setVisibility(View.GONE);
                    customView.setAnimation(anim);
                    customView.setEnabled(false);
                }

            }

        });
    }

}

完整的源$ C ​​$ C来证明这个问题可以在这里找到:

The complete source code to demonstrate this problem can be found here :

https://www.dropbox.com/s/1101dm885fn5hzq/animator_bug.zip

我怎样才能让我的自定义视图不接收触摸事件之后,我不得不将其向下滑动?

推荐答案

它为什么你正在使用的我也不清楚 setAnimation()而不是 startAnimation(),因为它不会出现您已设置的开始时间是你的动画。

It is not clear to me why you are using setAnimation() instead of startAnimation() as it does not appear that you have set a start time on your animations.

在另一方面,我发现一个视图设置为GONE虽然它有一个相关的动画不会让真正的水涨船高。相反,你必须首先了解如何使用摆脱动画 clearAnimation()

On another note, I have found that setting a view to GONE while it has an associated animation does not make it truly "GONE." Instead, you must first get rid of the animation using clearAnimation().

所以,这样的事情,而不是:

So, something like this instead:

public void onClick(View v) {
    if (customView.getVisibility() != View.VISIBLE) {
        // Slide up!
        TranslateAnimation anim=new TranslateAnimation(0,0,100,0);
        anim.setFillAfter(true);
        anim.setDuration(200);

        customView.setVisibility(View.VISIBLE);         
        customView.setEnabled(true);
        customView.startAnimation(anim);   
    } else {
        // Slide down!
        TranslateAnimation anim=new TranslateAnimation(0,0,0,100);
        anim.setFillAfter(true);
        anim.setDuration(200);
        anim.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationEnd(Animation animation) {
                customView.clearAnimation();
                customView.setVisibility(View.GONE);
                customView.setEnabled(false);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                // nothing to do
            }

            @Override
            public void onAnimationStart(Animation animation) {
                // nothing to do
            }
        }
    }
}

我不记得,但你可能要后() onAnimationEnd的内容()而不是运行code的立即使其生效正常。

I don't recall, but you may have to post() the contents of onAnimationEnd() instead of running the code immediately for it to take effect properly.

这篇关于无法使视图使用TranslateAnimation后完全消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆