【问题标题】:Unable to make view completely GONE after using TranslateAnimation使用 TranslateAnimation 后无法使视图完全消失
【发布时间】:2013-04-18 16:06:46
【问题描述】:

我曾使用TranslateAnimation 并上下滑动视图。

但是,我意识到,即使在我向下滑动视图并在其可见性中使用View.GONE 之后,视图仍然能够接收触摸事件。

您可以通过单击按钮使橙色视图从屏幕底部消失来产生相同的问题。然后,当您点击屏幕底部时,您会意识到自定义视图的触摸事件仍在触发中。

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

            }

        });
    }

}

可以在此处找到演示此问题的完整源代码:

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

如何让我的自定义视图在我向下滑动后不接收触摸事件?

【问题讨论】:

  • 试着把customView.setEnabled(false)放在customView.setVisibility(View.GONE)之前

标签: android


【解决方案1】:

我不清楚您为什么使用setAnimation() 而不是startAnimation(),因为您似乎没有为动画设置开始时间。

另一方面,我发现将具有关联动画的视图设置为 GONE 并不能使其真正“消失”。相反,您必须首先使用 clearAnimation() 删除动画。

所以,改为这样:

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

我不记得了,但您可能需要 post() onAnimationEnd() 的内容而不是立即运行代码才能使其正常生效。

【讨论】:

  • 谢谢!我们是否也必须使用后技术才能向上滑动?您认为必须使用clearAnimation 使其完全消失是正常行为吗?
  • 我不确定为此目的使用clearAnimation 是否是“正确的”行为,这正是我发现的必要条件。除此之外,如果您的最低支持平台是 API 11 (Honeycomb) 或更高版本,我强烈建议您考虑使用新的 Animator 框架而不是 Animation。这是一个更好的框架,没有这些奇怪的问题。 developer.android.com/reference/android/animation/Animator.html
  • 不幸的是,我仍然需要支持 Gingerbread,因为它仍然很受欢迎。你试过nineoldandroids.com 吗?就我而言, ValueAnimator 适合替代 TranslateAnimation 吗?
  • 是的,我用的是 NineOldAndroids。但是,这并不能解决clearAnimation 的问题,因为 NOA 只是在底层使用旧的动画。
猜你喜欢
  • 2012-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-12
  • 2011-01-27
  • 1970-01-01
  • 2014-04-03
  • 1970-01-01
相关资源
最近更新 更多