2013-04-08 66 views
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平板电脑由于图像消失,没有再出现失败。

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

谢谢。

回答

0

最后,我找到了解决办法:有必要采取提拉:

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

现在,它工作正常。

有人可以说我是否应该返回true或false吗?有什么不同?

谢谢。

+1

false表示触摸事件未消耗,因此将传播到底层视图。真正消耗事件,所以儿童不会被通知事件 – AndacAydin 2015-06-04 12:15:11