コード例 #1
0
 protected void notifyObservers(final T model) {
   synchronized (listeners) {
     for (OnChangeListener<T> listener : listeners) {
       listener.onChange(model);
     }
   }
 }
コード例 #2
0
  /**
   * Reset the lightweight theme. Note: This should be called on the UI thread to restrict accessing
   * the bitmap to a single thread.
   */
  private void resetLightweightTheme() {
    ThreadUtils.assertOnUiThread(AssertBehavior.NONE);
    if (mBitmap == null) {
      return;
    }

    // Reset the bitmap.
    mBitmap = null;

    for (OnChangeListener listener : mListeners) {
      listener.onLightweightThemeReset();
    }
  }
コード例 #3
0
 @Override
 public void onClick(View v) {
   // TODO Auto-generated method stub
   mDeltX = mSwitchOn ? mMoveLength : -mMoveLength;
   mSwitchOn = !mSwitchOn;
   if (mListener != null) {
     mListener.onChange(this, mSwitchOn);
   }
   invalidate();
   mDeltX = 0;
 }
コード例 #4
0
ファイル: Joystick.java プロジェクト: wylliec/FRC-Drive
  @SuppressLint("ClickableViewAccessibility")
  @Override
  public boolean onTouch(View arg0, MotionEvent event) {

    actualX = event.getX();
    actualY = event.getY();
    virtualX = actualX - outerRadius;
    virtualY = outerRadius - actualY;
    double radius = outerRadius - innerRadius;

    switch (event.getAction()) {
      case MotionEvent.ACTION_DOWN: // Fall through ACTION_MOVE case
      case MotionEvent.ACTION_MOVE:
        if (Math.sqrt(Math.pow(virtualX, 2) + Math.pow(virtualY, 2)) >= radius) {
          double angle = Math.atan(virtualY / virtualX);
          if (virtualY > 0 && virtualX > 0) {
            virtualX = (float) (Math.cos(angle) * radius);
            virtualY = (float) (Math.sin(angle) * radius);
          } else if (virtualY > 0 && virtualX < 0) {
            virtualX = (float) -Math.abs(Math.cos(angle) * radius);
            virtualY = (float) Math.abs(Math.sin(angle) * radius);
          } else if (virtualY < 0 && virtualX < 0) {
            virtualX = (float) -Math.abs(Math.cos(angle) * radius);
            virtualY = (float) -Math.abs(Math.sin(angle) * radius);
          } else if (virtualY < 0 && virtualX > 0) {
            virtualX = (float) (Math.cos(angle) * radius);
            virtualY = (float) -Math.abs(Math.sin(angle) * radius);
          } else {
            virtualX = 0;
            virtualY = 0;
          }
          actualX = virtualX + outerRadius;
          actualY = outerRadius - virtualY;
        }
        break;

      case MotionEvent.ACTION_UP:
        virtualX = 0;
        virtualY = 0;
        actualX = virtualX + outerRadius;
        actualY = outerRadius - virtualY;
        break;
    }
    invalidate();
    // Convert joystick range
    virtualX = (float) ((128 / radius) * virtualX);
    virtualY = (float) ((128 / radius) * virtualY);
    // Convert to byte for packets
    xAxis = (byte) (virtualX);
    yAxis = (byte) (virtualY);
    onChange.onChange(xAxis, yAxis);
    return true;
  }
コード例 #5
0
  @Override
  public boolean onTouchEvent(MotionEvent event) {
    // TODO Auto-generated method stub
    switch (event.getAction()) {
      case MotionEvent.ACTION_DOWN:
        mLastX = event.getX();
        break;
      case MotionEvent.ACTION_MOVE:
        mCurrentX = event.getX();
        mDeltX = (int) (mCurrentX - mLastX);
        // 如果开关开着向左滑动,或者开关关着向右滑动(这时候是不需要处理的)
        if ((mSwitchOn && mDeltX < 0) || (!mSwitchOn && mDeltX > 0)) {
          mFlag = true;
          mDeltX = 0;
        }

        if (Math.abs(mDeltX) > mMoveLength) {
          mDeltX = mDeltX > 0 ? mMoveLength : -mMoveLength;
        }
        invalidate();
        return true;
      case MotionEvent.ACTION_UP:
        if (Math.abs(mDeltX) > 0 && Math.abs(mDeltX) < mMoveLength / 2) {
          mDeltX = 0;
          invalidate();
          return true;
        } else if (Math.abs(mDeltX) > mMoveLength / 2 && Math.abs(mDeltX) <= mMoveLength) {
          mDeltX = mDeltX > 0 ? mMoveLength : -mMoveLength;
          mSwitchOn = !mSwitchOn;
          if (mListener != null) {
            mListener.onChange(this, mSwitchOn);
          }
          invalidate();
          mDeltX = 0;
          return true;
        } else if (mDeltX == 0 && mFlag) {
          // 这时候得到的是不需要进行处理的,因为已经move过了
          mDeltX = 0;
          mFlag = false;
          return true;
        }
        return super.onTouchEvent(event);
      default:
        break;
    }
    invalidate();
    return super.onTouchEvent(event);
  }
コード例 #6
0
ファイル: PromptInput.java プロジェクト: neojski/gecko-dev
 protected void notifyListeners(String val) {
   if (mListener != null) {
     mListener.onChange(this);
   }
 }
コード例 #7
0
 public void remGroupFromTrainingFeed(TrainingFeed trainingFeed, Groups group) {
   trainingFeed.deleteGroup(group);
   if (listener != null) {
     listener.onGroupRemoved(trainingFeed);
   }
 }
コード例 #8
0
 public void change(T value) {
   for (OnChangeListener<T> listener : listeners) listener.change(value);
 }
コード例 #9
0
  /**
   * Set a new lightweight theme with the given bitmap. Note: This should be called on the UI thread
   * to restrict accessing the bitmap to a single thread.
   *
   * @param bitmap The bitmap used for the lightweight theme.
   * @param color The background/accent color used for the lightweight theme.
   */
  private void setLightweightTheme(Bitmap bitmap, String color) {
    if (bitmap == null || bitmap.getWidth() == 0 || bitmap.getHeight() == 0) {
      mBitmap = null;
      return;
    }

    // Get the max display dimension so we can crop or expand the theme.
    final int maxWidth = WindowUtils.getLargestDimension(mApplication);

    // The lightweight theme image's width and height.
    final int bitmapWidth = bitmap.getWidth();
    final int bitmapHeight = bitmap.getHeight();

    try {
      mColor = Color.parseColor(color);
    } catch (Exception e) {
      // Malformed or missing color.
      // Default to TRANSPARENT.
      mColor = Color.TRANSPARENT;
    }

    // Calculate the luminance to determine if it's a light or a dark theme.
    double luminance =
        (0.2125 * ((mColor & 0x00FF0000) >> 16))
            + (0.7154 * ((mColor & 0x0000FF00) >> 8))
            + (0.0721 * (mColor & 0x000000FF));
    mIsLight = luminance > 110;

    // The bitmap image might be smaller than the device's width.
    // If it's smaller, fill the extra space on the left with the dominant color.
    if (bitmapWidth >= maxWidth) {
      mBitmap = Bitmap.createBitmap(bitmap, bitmapWidth - maxWidth, 0, maxWidth, bitmapHeight);
    } else {
      Paint paint = new Paint();
      paint.setAntiAlias(true);

      // Create a bigger image that can fill the device width.
      // By creating a canvas for the bitmap, anything drawn on the canvas
      // will be drawn on the bitmap.
      mBitmap = Bitmap.createBitmap(maxWidth, bitmapHeight, Bitmap.Config.ARGB_8888);
      Canvas canvas = new Canvas(mBitmap);

      // Fill the canvas with dominant color.
      canvas.drawColor(mColor);

      // The image should be top-right aligned.
      Rect rect = new Rect();
      Gravity.apply(
          Gravity.TOP | Gravity.RIGHT,
          bitmapWidth,
          bitmapHeight,
          new Rect(0, 0, maxWidth, bitmapHeight),
          rect);

      // Draw the bitmap.
      canvas.drawBitmap(bitmap, null, rect, paint);
    }

    for (OnChangeListener listener : mListeners) {
      listener.onLightweightThemeChanged();
    }
  }