Ejemplo n.º 1
0
 @Override
 public boolean onTouchEvent(MotionEvent event) {
   switch (event.getAction()) {
     case MotionEvent.ACTION_DOWN:
     case MotionEvent.ACTION_MOVE:
       {
         currentColorCircle = findNearestByPosition(event.getX(), event.getY());
         int selectedColor = getSelectedColor();
         initialColor = selectedColor;
         setColorToSliders(selectedColor);
         invalidate();
         break;
       }
     case MotionEvent.ACTION_UP:
       {
         int selectedColor = getSelectedColor();
         if (listeners != null) {
           for (OnColorSelectedListener listener : listeners) {
             try {
               listener.onColorSelected(selectedColor);
             } catch (Exception e) {
               // Squash individual listener exceptions
             }
           }
         }
         setColorToSliders(selectedColor);
         setColorText(selectedColor, true);
         setColorPreviewColor(selectedColor);
         invalidate();
         break;
       }
   }
   return true;
 }
Ejemplo n.º 2
0
  @SuppressLint("ClickableViewAccessibility")
  @Override
  public boolean onTouchEvent(MotionEvent event) {
    getParent().requestDisallowInterceptTouchEvent(true);

    // Convert coordinates to our internal coordinate system
    float x = event.getX() - mTranslationOffset;
    float y = event.getY() - mTranslationOffset;

    switch (event.getAction()) {
      case MotionEvent.ACTION_DOWN:
        // Check whether the user pressed on the pointer.
        float[] pointerPosition = calculatePointerPosition(mAngle);
        if (x >= (pointerPosition[0] - mColorPointerHaloRadius)
            && x <= (pointerPosition[0] + mColorPointerHaloRadius)
            && y >= (pointerPosition[1] - mColorPointerHaloRadius)
            && y <= (pointerPosition[1] + mColorPointerHaloRadius)) {
          mSlopX = x - pointerPosition[0];
          mSlopY = y - pointerPosition[1];
          mUserIsMovingPointer = true;
          invalidate();
        }
        // Check whether the user pressed on the center.
        else if (x >= -mColorCenterRadius
            && x <= mColorCenterRadius
            && y >= -mColorCenterRadius
            && y <= mColorCenterRadius
            && mShowCenterOldColor) {
          mCenterHaloPaint.setAlpha(0x50);
          setColor(getOldCenterColor());
          invalidate();
        }
        // Check whether the user pressed anywhere on the wheel.
        else if (Math.sqrt(x * x + y * y) <= mColorWheelRadius + mColorPointerHaloRadius
            && Math.sqrt(x * x + y * y) >= mColorWheelRadius - mColorPointerHaloRadius
            && mTouchAnywhereOnColorWheelEnabled) {
          mUserIsMovingPointer = true;
          invalidate();
        }
        // If user did not press pointer or center, report event not handled
        else {
          getParent().requestDisallowInterceptTouchEvent(false);
          return false;
        }
        break;
      case MotionEvent.ACTION_MOVE:
        if (mUserIsMovingPointer) {
          mAngle = (float) Math.atan2(y - mSlopY, x - mSlopX);
          mPointerColor.setColor(calculateColor(mAngle));

          setNewCenterColor(mCenterNewColor = calculateColor(mAngle));

          if (mSVbar != null) {
            mSVbar.setColor(mColor);
          }

          invalidate();
        }
        // If user did not press pointer or center, report event not handled
        else {
          getParent().requestDisallowInterceptTouchEvent(false);
          return false;
        }
        break;
      case MotionEvent.ACTION_UP:
        mUserIsMovingPointer = false;
        mCenterHaloPaint.setAlpha(0x00);

        if (onColorSelectedListener != null && mCenterNewColor != oldSelectedListenerColor) {
          onColorSelectedListener.onColorSelected(mCenterNewColor);
          oldSelectedListenerColor = mCenterNewColor;
        }

        invalidate();
        break;
      case MotionEvent.ACTION_CANCEL:
        if (onColorSelectedListener != null && mCenterNewColor != oldSelectedListenerColor) {
          onColorSelectedListener.onColorSelected(mCenterNewColor);
          oldSelectedListenerColor = mCenterNewColor;
        }
        break;
    }
    return true;
  }
 @Override
 public void onClick(View v) {
   if (mOnColorSelectedListener != null) {
     mOnColorSelectedListener.onColorSelected(mColor);
   }
 }