0

我正在尝试在我的自定义 ImageView 类 AvatarView 中做点击效果。

我正在使用该代码:

        // **** Listener onTouch ****.

    // Añadimos un listener para su pulsación.
    this.setOnTouchListener(new OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {

            switch (event.getAction()) {

                case MotionEvent.ACTION_DOWN: 

                    // Establecemos un filtrado de color.
                    AvatarView.this.setColorFilter(0xe0f47521, PorterDuff.Mode.SRC_ATOP);
                    AvatarView.this.invalidate();
                    break;

                case MotionEvent.ACTION_UP: 

                    // Eliminamos el filtrado de color.
                    //AvatarView.this.clearColorFilter();
                    AvatarView.this.setColorFilter(null);
                    AvatarView.this.invalidate();
                    break;


            }

            return false;
        }
    });

此代码在我的 2.3 手机中完美运行,但在我的 4.1 平板电脑中失败,因为图像消失并且不再出现。

我还没有找到任何解决方案。任何的想法?

谢谢。

4

1 回答 1

0

最后,我找到了解决方案:有必要使用drawable:

        // **** Listener onTouch ****.

    // Añadimos un listener para su pulsación.
    this.setOnTouchListener(new OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {

            switch (event.getAction()) {

                case MotionEvent.ACTION_DOWN: 

                    // Establecemos un filtrado de color.
                    AvatarView.this.getDrawable().setColorFilter(0xe0f47521, PorterDuff.Mode.SRC_ATOP);
                    AvatarView.this.invalidate();
                    break;

                case MotionEvent.ACTION_UP: 

                    // Eliminamos el filtrado de color.
                    AvatarView.this.getDrawable().clearColorFilter();
                    AvatarView.this.invalidate();
                    break;

                case MotionEvent.ACTION_CANCEL: 

                    // Eliminamos el filtrado de color.
                    AvatarView.this.getDrawable().clearColorFilter();
                    AvatarView.this.invalidate();
                    break;

            }

            return false;
            //return true;
        }
    });

现在,它工作正常。

谁能告诉我我应该返回真还是假?有什么不同?

谢谢。

于 2013-04-08T18:23:36.837 回答