protected internal override void onDraw(android.graphics.Canvas canvas) { base.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); 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(); android.text.Layout switchText = getTargetCheckedState() ? mOnLayout : mOffLayout; canvas.translate((thumbLeft + thumbRight) / 2 - switchText.getWidth() / 2, (switchInnerTop + switchInnerBottom) / 2 - switchText.getHeight() / 2); switchText.draw(canvas); canvas.restore(); }
protected internal override void onMeasure(int widthMeasureSpec, int heightMeasureSpec ) { int widthMode = android.view.View.MeasureSpec.getMode(widthMeasureSpec); int heightMode = android.view.View.MeasureSpec.getMode(heightMeasureSpec); int widthSize = android.view.View.MeasureSpec.getSize(widthMeasureSpec); int heightSize = android.view.View.MeasureSpec.getSize(heightMeasureSpec); if (mOnLayout == null) { mOnLayout = makeLayout(mTextOn); } if (mOffLayout == null) { mOffLayout = makeLayout(mTextOff); } mTrackDrawable.getPadding(mTempRect); int maxTextWidth = System.Math.Max(mOnLayout.getWidth(), mOffLayout.getWidth()); int switchWidth = System.Math.Max(mSwitchMinWidth, maxTextWidth * 2 + mThumbTextPadding * 4 + mTempRect.left + mTempRect.right); int switchHeight = mTrackDrawable.getIntrinsicHeight(); mThumbWidth = maxTextWidth + mThumbTextPadding * 2; switch (widthMode) { case android.view.View.MeasureSpec.AT_MOST: { widthSize = System.Math.Min(widthSize, switchWidth); break; } case android.view.View.MeasureSpec.UNSPECIFIED: { widthSize = switchWidth; break; } case android.view.View.MeasureSpec.EXACTLY: { // Just use what we were given break; } } switch (heightMode) { case android.view.View.MeasureSpec.AT_MOST: { heightSize = System.Math.Min(heightSize, switchHeight); break; } case android.view.View.MeasureSpec.UNSPECIFIED: { heightSize = switchHeight; break; } case android.view.View.MeasureSpec.EXACTLY: { // Just use what we were given break; } } mSwitchWidth = switchWidth; mSwitchHeight = switchHeight; base.onMeasure(widthMeasureSpec, heightMeasureSpec); int measuredHeight = getMeasuredHeight(); if (measuredHeight < switchHeight) { setMeasuredDimension(getMeasuredWidthAndState(), switchHeight); } }