コード例 #1
0
        @Override
        public boolean onTouch(View v, MotionEvent event) {

          if (mOutSideOnTouchListener != null) {
            mOutSideOnTouchListener.onTouch(v, event);
          }

          Drawable drawable = v.getBackground();
          if (drawable == null) return false;

          drawable.mutate();
          switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
              drawable.setColorFilter(Color.argb(100, 0, 0, 0), Mode.DST_IN); // 此处值可自行调整
              v.setBackgroundDrawable(drawable);
              break;
            case MotionEvent.ACTION_UP:
              drawable.clearColorFilter();
              v.setBackgroundDrawable(drawable);
              break;
            case MotionEvent.ACTION_CANCEL:
              drawable.clearColorFilter();
              v.setBackgroundDrawable(drawable);
              break;
            default:
              break;
          }

          return false;
        }
  public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_UP) {
      // coordinates of the touch action
      int xTouch = (int) event.getX();
      int yTouch = (int) event.getY();
      // transparency at the point of the touch
      int transparency = Color.alpha(img.getPixel(xTouch, yTouch));

      // determine if it was a click
      if (xTouch < img.getWidth() && yTouch < img.getHeight() && transparency > 100) {
        listener.onTouch(v, event);
      }
    }

    return true;
  }