예제 #1
0
    @Override
    public boolean onTouchEvent(MotionEvent event) {
      float x = event.getX() - CENTER_X;
      float y = event.getY() - CENTER_Y;
      boolean inCenter = Math.sqrt(x * x + y * y) <= CENTER_RADIUS;

      switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
          mTrackingCenter = inCenter;
          if (inCenter) {
            mHighlightCenter = true;
            invalidate();
            break;
          }
        case MotionEvent.ACTION_MOVE:
          if (mTrackingCenter) {
            if (mHighlightCenter != inCenter) {
              mHighlightCenter = inCenter;
              invalidate();
            }
          } else {
            float angle = (float) Math.atan2(y, x);
            // need to turn angle [-PI ... PI] into unit [0....1]
            float unit = angle / (2 * PI);
            if (unit < 0) {
              unit += 1;
            }
            mCenterPaint.setColor(interpColor(mColors, unit));
            invalidate();
          }
          break;
        case MotionEvent.ACTION_UP:
          if (mTrackingCenter) {
            if (inCenter) {
              mListener.colorChanged(mCenterPaint.getColor());
            }
            mTrackingCenter = false; // so we draw w/o halo
            invalidate();
          }
          break;
      }
      return true;
    }
 private void setCurrentHueSaturation(float x, float y, double distance) {
   // get the angle of the point in the color wheel
   // note: the (-) is intentional due to the inversion of the y coordinate system (wrt cartesian)
   float angle = -(float) Math.atan2(y, x);
   // Log.d(TAG, String.format("Angle Rad: %f", angle));
   if (angle < 0.0f) {
     angle += (float) (2.0 * Math.PI);
   }
   // change angle to degrees
   float hue = (float) (angle * (180.0 / Math.PI));
   // Log.d(TAG, String.format("Angle Deg: %f", hue));
   // normalize the distance from center
   float saturation = (float) (distance / mRadius);
   // replace the hue and saturation with the new values
   mNewColor[0] = hue;
   // only replace the saturation if the value is within range, otherwise the crosshairs will be
   // drawn outside of the wheel
   if (saturation <= 1.0f) {
     mNewColor[1] = saturation;
   }
   updateListener();
   invalidate();
 }