@Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    // calculate the left and right values
    float right = this.getWidth() - this.getPaddingRight();
    float left = right - this.getWidth() + this.getPaddingLeft();
    float height = this.getHeight() - this.getPaddingTop() - this.getPaddingBottom();
    float radius = height / 2; // εœ“εŠεΎ‘

    oval.set(left, 0, right, height);
    if (this.isChecked()) {
      paint.setColor(Color.parseColor("#4B96C2"));
      // paint.setColor(Color.parseColor("#5a7fc0"));
      canvas.drawRoundRect(oval, radius, radius, paint);
      paint.setColor(Color.WHITE);
      canvas.drawCircle(right - radius, radius, radius - SWITCH_BOUND, paint);
    } else {
      paint.setColor(Color.GRAY);
      canvas.drawRoundRect(oval, radius, radius, paint);
      paint.setColor(Color.WHITE);
      canvas.drawCircle(left + radius, radius, radius - SWITCH_BOUND, paint);
    }

    canvas.save();
  }
Example #2
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();
  }
Example #3
0
  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    final Rect padding = mTempRect;
    final Drawable trackDrawable = mTrackDrawable;
    if (trackDrawable != null) {
      trackDrawable.getPadding(padding);
    } else {
      padding.setEmpty();
    }

    final int switchTop = mSwitchTop;
    final int switchBottom = mSwitchBottom;
    final int switchInnerTop = switchTop + padding.top;
    final int switchInnerBottom = switchBottom - padding.bottom;

    final Drawable thumbDrawable = mThumbDrawable;
    if (trackDrawable != null) {
      if (mSplitTrack && thumbDrawable != null) {
        final Insets insets = Insets.NONE;
        thumbDrawable.copyBounds(padding);
        padding.left += insets.left;
        padding.right -= insets.right;

        final int saveCount = canvas.save();
        canvas.clipRect(padding, Region.Op.DIFFERENCE);
        trackDrawable.draw(canvas);
        canvas.restoreToCount(saveCount);
      } else {
        trackDrawable.draw(canvas);
      }
    }

    final int saveCount = canvas.save();

    if (thumbDrawable != null) {
      thumbDrawable.draw(canvas);
    }

    canvas.restoreToCount(saveCount);
  }