/**
   * Draws value and label layout.
   *
   * @param canvas the canvas for drawing
   */
  private void drawValue(Canvas canvas) {
    valuePaint.setColor(valueTextColor);
    valuePaint.drawableState = getDrawableState();

    labelPaint.setColor(labelTextColor);
    labelPaint.drawableState = getDrawableState();

    Rect bounds = new Rect();
    itemsLayout.getLineBounds(visibleItems / 2, bounds);

    // draw label
    if (labelLayout != null) {
      canvas.save();
      canvas.translate(itemsLayout.getWidth() + LABEL_OFFSET, bounds.top);
      labelLayout.draw(canvas);
      canvas.restore();
    }

    // draw current value
    if (valueLayout != null) {
      canvas.save();
      canvas.translate(0, bounds.top + scrollingOffset);
      valueLayout.draw(canvas);
      canvas.restore();
    }
  }
  /**
   * Draws items.
   *
   * @param canvas the canvas for drawing
   */
  private void drawItems(Canvas canvas) {
    canvas.save();

    int top = itemsLayout.getLineTop(1);
    canvas.translate(0, -top + scrollingOffset);

    itemsPaint.setColor(ITEMS_TEXT_COLOR);
    itemsPaint.drawableState = getDrawableState();
    itemsLayout.draw(canvas);

    canvas.restore();
  }
示例#3
0
  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    // Draw the switch
    int switchLeft = mSwitchLeft;
    int switchTop = mSwitchTop;
    int switchRight = mSwitchRight;
    int switchBottom = mSwitchBottom;

    mTrackDrawable.setBounds(switchLeft, switchTop, switchRight, switchBottom);
    mTrackDrawable.draw(canvas);

    canvas.save();

    mTrackDrawable.getPadding(mTempRect);
    int switchInnerLeft = switchLeft + mTempRect.left;
    int switchInnerTop = switchTop + mTempRect.top;
    int switchInnerRight = switchRight - mTempRect.right;
    int switchInnerBottom = switchBottom - mTempRect.bottom;
    canvas.clipRect(switchInnerLeft, switchTop, switchInnerRight, switchBottom);

    mThumbDrawable.getPadding(mTempRect);
    final int thumbPos = (int) (mThumbPosition + 0.5f);
    int thumbLeft = switchInnerLeft - mTempRect.left + thumbPos;
    int thumbRight = switchInnerLeft + thumbPos + mThumbWidth + mTempRect.right;

    mThumbDrawable.setBounds(thumbLeft, switchTop, thumbRight, switchBottom);
    mThumbDrawable.draw(canvas);

    // mTextColors should not be null, but just in case
    if (mTextColors != null) {
      mTextPaint.setColor(
          mTextColors.getColorForState(getDrawableState(), mTextColors.getDefaultColor()));
    }
    mTextPaint.drawableState = getDrawableState();

    Layout switchText = getTargetCheckedState() ? mOnLayout : mOffLayout;
    if (switchText != null) {
      canvas.translate(
          (thumbLeft + thumbRight) / 2 - switchText.getWidth() / 2,
          (switchInnerTop + switchInnerBottom) / 2 - switchText.getHeight() / 2);
      switchText.draw(canvas);
    }

    canvas.restore();
  }