diff options
| author | Alan Viverette <alanv@google.com> | 2014-07-29 17:22:30 -0700 |
|---|---|---|
| committer | Alan Viverette <alanv@google.com> | 2014-07-30 00:28:08 +0000 |
| commit | 8bb399069da4e46b231333cff6880a0cf35b9417 (patch) | |
| tree | 83ed48124cfb034062a87fd3b11ea60829abe368 /core/java/android/widget/Switch.java | |
| parent | 8ef360f34893771ee22a8ec7a1dc9c9aedc4b38c (diff) | |
Update switch assets, fix SearchView asset 9-patch areas
BUG: 16606037
BUG: 16467249
Change-Id: Iabdf1634f4d8f87031ab3e5579140cdd428173de
Diffstat (limited to 'core/java/android/widget/Switch.java')
| -rw-r--r-- | core/java/android/widget/Switch.java | 169 |
1 files changed, 129 insertions, 40 deletions
diff --git a/core/java/android/widget/Switch.java b/core/java/android/widget/Switch.java index 8ea1090e792b..682f2ae1f311 100644 --- a/core/java/android/widget/Switch.java +++ b/core/java/android/widget/Switch.java @@ -100,13 +100,35 @@ public class Switch extends CompoundButton { private int mMinFlingVelocity; private float mThumbPosition; + + /** + * Width required to draw the switch track and thumb. Includes padding and + * optical bounds for both the track and thumb. + */ private int mSwitchWidth; + + /** + * Height required to draw the switch track and thumb. Includes padding and + * optical bounds for both the track and thumb. + */ private int mSwitchHeight; - private int mThumbWidth; // Does not include padding + /** + * Width of the thumb's content region. Does not include padding or + * optical bounds. + */ + private int mThumbWidth; + + /** Left bound for drawing the switch track and thumb. */ private int mSwitchLeft; + + /** Top bound for drawing the switch track and thumb. */ private int mSwitchTop; + + /** Right bound for drawing the switch track and thumb. */ private int mSwitchRight; + + /** Bottom bound for drawing the switch track and thumb. */ private int mSwitchBottom; private TextPaint mTextPaint; @@ -539,7 +561,7 @@ public class Switch extends CompoundButton { * Sets whether the on/off text should be displayed. * * @param showText {@code true} to display on/off text - * @hide + * @attr ref android.R.styleable#Switch_showText */ public void setShowText(boolean showText) { if (mShowText != showText) { @@ -550,7 +572,7 @@ public class Switch extends CompoundButton { /** * @return whether the on/off text should be displayed - * @hide + * @attr ref android.R.styleable#Switch_showText */ public boolean getShowText() { return mShowText; @@ -568,32 +590,50 @@ public class Switch extends CompoundButton { } } - final int trackHeight; final Rect padding = mTempRect; - if (mTrackDrawable != null) { - mTrackDrawable.getPadding(padding); - trackHeight = mTrackDrawable.getIntrinsicHeight(); - } else { - padding.setEmpty(); - trackHeight = 0; - } - final int thumbWidth; final int thumbHeight; if (mThumbDrawable != null) { - thumbWidth = mThumbDrawable.getIntrinsicWidth(); + // Cached thumb width does not include padding. + mThumbDrawable.getPadding(padding); + thumbWidth = mThumbDrawable.getIntrinsicWidth() - padding.left - padding.right; thumbHeight = mThumbDrawable.getIntrinsicHeight(); } else { thumbWidth = 0; thumbHeight = 0; } - final int maxTextWidth = mShowText ? Math.max(mOnLayout.getWidth(), mOffLayout.getWidth()) - + mThumbTextPadding * 2 : 0; + final int maxTextWidth; + if (mShowText) { + maxTextWidth = Math.max(mOnLayout.getWidth(), mOffLayout.getWidth()) + + mThumbTextPadding * 2; + } else { + maxTextWidth = 0; + } + mThumbWidth = Math.max(maxTextWidth, thumbWidth); + final int trackHeight; + if (mTrackDrawable != null) { + mTrackDrawable.getPadding(padding); + trackHeight = mTrackDrawable.getIntrinsicHeight(); + } else { + padding.setEmpty(); + trackHeight = 0; + } + + // Adjust left and right padding to ensure there's enough room for the + // thumb's padding (when present). + int paddingLeft = padding.left; + int paddingRight = padding.right; + if (mThumbDrawable != null) { + final Insets inset = mThumbDrawable.getOpticalInsets(); + paddingLeft = Math.max(paddingLeft, inset.left); + paddingRight = Math.max(paddingRight, inset.right); + } + final int switchWidth = Math.max(mSwitchMinWidth, - 2 * mThumbWidth + padding.left + padding.right); + 2 * mThumbWidth + paddingLeft + paddingRight); final int switchHeight = Math.max(trackHeight, thumbHeight); mSwitchWidth = switchWidth; mSwitchHeight = switchHeight; @@ -806,19 +846,33 @@ public class Switch extends CompoundButton { protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); - int switchRight; - int switchLeft; + int opticalInsetLeft = 0; + int opticalInsetRight = 0; + if (mThumbDrawable != null) { + final Rect trackPadding = mTempRect; + if (mTrackDrawable != null) { + mTrackDrawable.getPadding(trackPadding); + } else { + trackPadding.setEmpty(); + } + + final Insets insets = mThumbDrawable.getOpticalInsets(); + opticalInsetLeft = Math.max(0, insets.left - trackPadding.left); + opticalInsetRight = Math.max(0, insets.right - trackPadding.right); + } + final int switchRight; + final int switchLeft; if (isLayoutRtl()) { - switchLeft = getPaddingLeft(); - switchRight = switchLeft + mSwitchWidth; + switchLeft = getPaddingLeft() + opticalInsetLeft; + switchRight = switchLeft + mSwitchWidth - opticalInsetLeft - opticalInsetRight; } else { - switchRight = getWidth() - getPaddingRight(); - switchLeft = switchRight - mSwitchWidth; + switchRight = getWidth() - getPaddingRight() - opticalInsetRight; + switchLeft = switchRight - mSwitchWidth + opticalInsetLeft + opticalInsetRight; } - int switchTop = 0; - int switchBottom = 0; + final int switchTop; + final int switchBottom; switch (getGravity() & Gravity.VERTICAL_GRAVITY_MASK) { default: case Gravity.TOP: @@ -847,27 +901,55 @@ public class Switch extends CompoundButton { @Override public void draw(Canvas c) { final Rect padding = mTempRect; - - // Layout the track. final int switchLeft = mSwitchLeft; final int switchTop = mSwitchTop; final int switchRight = mSwitchRight; final int switchBottom = mSwitchBottom; - if (mTrackDrawable != null) { - mTrackDrawable.setBounds(switchLeft, switchTop, switchRight, switchBottom); - mTrackDrawable.getPadding(padding); + + int thumbInitialLeft = switchLeft + getThumbOffset(); + + final Insets thumbInsets; + if (mThumbDrawable != null) { + thumbInsets = mThumbDrawable.getOpticalInsets(); + } else { + thumbInsets = Insets.NONE; } - final int switchInnerLeft = switchLeft + padding.left; + // Layout the track. + if (mTrackDrawable != null) { + mTrackDrawable.getPadding(padding); - // Relies on mTempRect, MUST be called first! - final int thumbPos = getThumbOffset(); + // Adjust thumb position for track padding. + thumbInitialLeft += padding.left; + + // If necessary, offset by the optical insets of the thumb asset. + int trackLeft = switchLeft; + int trackTop = switchTop; + int trackRight = switchRight; + int trackBottom = switchBottom; + if (thumbInsets != Insets.NONE) { + if (thumbInsets.left > padding.left) { + trackLeft += thumbInsets.left - padding.left; + } + if (thumbInsets.top > padding.top) { + trackTop += thumbInsets.top - padding.top; + } + if (thumbInsets.right > padding.right) { + trackRight -= thumbInsets.right - padding.right; + } + if (thumbInsets.bottom > padding.bottom) { + trackBottom -= thumbInsets.bottom - padding.bottom; + } + } + mTrackDrawable.setBounds(trackLeft, trackTop, trackRight, trackBottom); + } // Layout the thumb. if (mThumbDrawable != null) { mThumbDrawable.getPadding(padding); - final int thumbLeft = switchInnerLeft - padding.left + thumbPos; - final int thumbRight = switchInnerLeft + thumbPos + mThumbWidth + padding.right; + + final int thumbLeft = thumbInitialLeft - padding.left; + final int thumbRight = thumbInitialLeft + mThumbWidth + padding.right; mThumbDrawable.setBounds(thumbLeft, switchTop, thumbRight, switchBottom); final Drawable background = getBackground(); @@ -894,9 +976,7 @@ public class Switch extends CompoundButton { final int switchTop = mSwitchTop; final int switchBottom = mSwitchBottom; - final int switchInnerLeft = mSwitchLeft + padding.left; final int switchInnerTop = switchTop + padding.top; - final int switchInnerRight = mSwitchRight - padding.right; final int switchInnerBottom = switchBottom - padding.bottom; final Drawable thumbDrawable = mThumbDrawable; @@ -919,7 +999,6 @@ public class Switch extends CompoundButton { final int saveCount = canvas.save(); if (thumbDrawable != null) { - canvas.clipRect(switchInnerLeft, switchTop, switchInnerRight, switchBottom); thumbDrawable.draw(canvas); } @@ -974,7 +1053,7 @@ public class Switch extends CompoundButton { /** * Translates thumb position to offset according to current RTL setting and - * thumb scroll range. + * thumb scroll range. Accounts for both track and thumb padding. * * @return thumb offset */ @@ -990,8 +1069,18 @@ public class Switch extends CompoundButton { private int getThumbScrollRange() { if (mTrackDrawable != null) { - mTrackDrawable.getPadding(mTempRect); - return mSwitchWidth - mThumbWidth - mTempRect.left - mTempRect.right; + final Rect padding = mTempRect; + mTrackDrawable.getPadding(padding); + + final Insets insets; + if (mThumbDrawable != null) { + insets = mThumbDrawable.getOpticalInsets(); + } else { + insets = Insets.NONE; + } + + return mSwitchWidth - mThumbWidth - padding.left - padding.right + - insets.left - insets.right; } else { return 0; } |
