Update progress bar and seek bar backgrounds, fix seek bar thumb

Ensures LayerDrawable copies out the correct layer properties during
"tileification". This really needs to be deprecated or replaced in a
future CL, though, because it is potentially lossy (e.g. for
RippleDrawable, which is an instanceof LayerDrawable).

Bug: 19448441
Change-Id: I483e9c34fd781e280c13eb5951f96bc71a252819
This commit is contained in:
Alan Viverette
2015-02-23 12:49:47 -08:00
parent b3f3762ee0
commit 6a8253fdc9
14 changed files with 280 additions and 59 deletions

View File

@@ -12018,6 +12018,12 @@ package android.graphics.drawable {
method public int getId(int);
method public int getLayerGravity(int);
method public int getLayerHeight(int);
method public int getLayerInsetBottom(int);
method public int getLayerInsetEnd(int);
method public int getLayerInsetLeft(int);
method public int getLayerInsetRight(int);
method public int getLayerInsetStart(int);
method public int getLayerInsetTop(int);
method public int getLayerWidth(int);
method public int getNumberOfLayers();
method public int getOpacity();
@@ -12030,9 +12036,17 @@ package android.graphics.drawable {
method public boolean setDrawableByLayerId(int, android.graphics.drawable.Drawable);
method public void setId(int, int);
method public void setLayerGravity(int, int);
method public void setLayerHeight(int, int);
method public void setLayerInset(int, int, int, int, int);
method public void setLayerInsetBottom(int, int);
method public void setLayerInsetEnd(int, int);
method public void setLayerInsetLeft(int, int);
method public void setLayerInsetRelative(int, int, int, int, int);
method public void setLayerInsetRight(int, int);
method public void setLayerInsetStart(int, int);
method public void setLayerInsetTop(int, int);
method public void setLayerSize(int, int, int);
method public void setLayerWidth(int, int);
method public void setOpacity(int);
method public void setPaddingMode(int);
method public void unscheduleDrawable(android.graphics.drawable.Drawable, java.lang.Runnable);

View File

@@ -12292,6 +12292,12 @@ package android.graphics.drawable {
method public int getId(int);
method public int getLayerGravity(int);
method public int getLayerHeight(int);
method public int getLayerInsetBottom(int);
method public int getLayerInsetEnd(int);
method public int getLayerInsetLeft(int);
method public int getLayerInsetRight(int);
method public int getLayerInsetStart(int);
method public int getLayerInsetTop(int);
method public int getLayerWidth(int);
method public int getNumberOfLayers();
method public int getOpacity();
@@ -12304,9 +12310,17 @@ package android.graphics.drawable {
method public boolean setDrawableByLayerId(int, android.graphics.drawable.Drawable);
method public void setId(int, int);
method public void setLayerGravity(int, int);
method public void setLayerHeight(int, int);
method public void setLayerInset(int, int, int, int, int);
method public void setLayerInsetBottom(int, int);
method public void setLayerInsetEnd(int, int);
method public void setLayerInsetLeft(int, int);
method public void setLayerInsetRelative(int, int, int, int, int);
method public void setLayerInsetRight(int, int);
method public void setLayerInsetStart(int, int);
method public void setLayerInsetTop(int, int);
method public void setLayerSize(int, int, int);
method public void setLayerWidth(int, int);
method public void setOpacity(int);
method public void setPaddingMode(int);
method public void unscheduleDrawable(android.graphics.drawable.Drawable, java.lang.Runnable);

View File

@@ -423,8 +423,8 @@ public abstract class AbsSeekBar extends ProgressBar {
}
if (track != null) {
track.setBounds(0, trackOffset, w - mPaddingRight - mPaddingLeft,
h - mPaddingBottom - trackOffset - mPaddingTop);
final int trackWidth = w - mPaddingRight - mPaddingLeft;
track.setBounds(0, trackOffset, trackWidth, trackOffset + trackHeight);
}
if (thumb != null) {
@@ -472,7 +472,6 @@ public abstract class AbsSeekBar extends ProgressBar {
final Drawable background = getBackground();
if (background != null) {
final Rect bounds = thumb.getBounds();
final int offsetX = mPaddingLeft - mThumbOffset;
final int offsetY = mPaddingTop;
background.setHotspotBounds(left + offsetX, top + offsetY,

View File

@@ -401,36 +401,49 @@ public class ProgressBar extends View {
* traverse layer and state list drawables.
*/
private Drawable tileify(Drawable drawable, boolean clip) {
// TODO: This is a terrible idea that potentially destroys any drawable
// that extends any of these classes. We *really* need to remove this.
if (drawable instanceof LayerDrawable) {
LayerDrawable background = (LayerDrawable) drawable;
final int N = background.getNumberOfLayers();
Drawable[] outDrawables = new Drawable[N];
final LayerDrawable orig = (LayerDrawable) drawable;
final int N = orig.getNumberOfLayers();
final Drawable[] outDrawables = new Drawable[N];
for (int i = 0; i < N; i++) {
int id = background.getId(i);
outDrawables[i] = tileify(background.getDrawable(i),
final int id = orig.getId(i);
outDrawables[i] = tileify(orig.getDrawable(i),
(id == R.id.progress || id == R.id.secondaryProgress));
}
LayerDrawable newBg = new LayerDrawable(outDrawables);
final LayerDrawable clone = new LayerDrawable(outDrawables);
for (int i = 0; i < N; i++) {
newBg.setId(i, background.getId(i));
clone.setId(i, orig.getId(i));
clone.setLayerGravity(i, orig.getLayerGravity(i));
clone.setLayerWidth(i, orig.getLayerWidth(i));
clone.setLayerHeight(i, orig.getLayerHeight(i));
clone.setLayerInsetLeft(i, orig.getLayerInsetLeft(i));
clone.setLayerInsetRight(i, orig.getLayerInsetRight(i));
clone.setLayerInsetTop(i, orig.getLayerInsetTop(i));
clone.setLayerInsetBottom(i, orig.getLayerInsetBottom(i));
clone.setLayerInsetStart(i, orig.getLayerInsetStart(i));
clone.setLayerInsetEnd(i, orig.getLayerInsetEnd(i));
}
return newBg;
return clone;
}
} else if (drawable instanceof StateListDrawable) {
StateListDrawable in = (StateListDrawable) drawable;
StateListDrawable out = new StateListDrawable();
int numStates = in.getStateCount();
for (int i = 0; i < numStates; i++) {
if (drawable instanceof StateListDrawable) {
final StateListDrawable in = (StateListDrawable) drawable;
final StateListDrawable out = new StateListDrawable();
final int N = in.getStateCount();
for (int i = 0; i < N; i++) {
out.addState(in.getStateSet(i), tileify(in.getStateDrawable(i), clip));
}
return out;
} else if (drawable instanceof BitmapDrawable) {
return out;
}
if (drawable instanceof BitmapDrawable) {
final BitmapDrawable bitmap = (BitmapDrawable) drawable;
final Bitmap tileBitmap = bitmap.getBitmap();
if (mSampleTile == null) {
@@ -1648,7 +1661,7 @@ public class ProgressBar extends View {
// rotates properly in its animation
final int saveCount = canvas.save();
if(isLayoutRtl() && mMirrorForRtl) {
if (isLayoutRtl() && mMirrorForRtl) {
canvas.translate(getWidth() - mPaddingRight, mPaddingTop);
canvas.scale(-1.0f, 1.0f);
} else {
@@ -1680,20 +1693,23 @@ public class ProgressBar extends View {
@Override
protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Drawable d = mCurrentDrawable;
int dw = 0;
int dh = 0;
final Drawable d = mCurrentDrawable;
if (d != null) {
dw = Math.max(mMinWidth, Math.min(mMaxWidth, d.getIntrinsicWidth()));
dh = Math.max(mMinHeight, Math.min(mMaxHeight, d.getIntrinsicHeight()));
}
updateDrawableState();
dw += mPaddingLeft + mPaddingRight;
dh += mPaddingTop + mPaddingBottom;
setMeasuredDimension(resolveSizeAndState(dw, widthMeasureSpec, 0),
resolveSizeAndState(dh, heightMeasureSpec, 0));
final int measuredWidth = resolveSizeAndState(dw, widthMeasureSpec, 0);
final int measuredHeight = resolveSizeAndState(dh, heightMeasureSpec, 0);
setMeasuredDimension(measuredWidth, measuredHeight);
}
@Override
@@ -1703,7 +1719,7 @@ public class ProgressBar extends View {
}
private void updateDrawableState() {
int[] state = getDrawableState();
final int[] state = getDrawableState();
if (mProgressDrawable != null && mProgressDrawable.isStateful()) {
mProgressDrawable.setState(state);

View File

@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2015 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/white"
android:alpha="?attr/disabledAlpha" />
</selector>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 184 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 178 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 185 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 177 B

View File

@@ -15,22 +15,32 @@
-->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@id/background">
<nine-patch android:src="@drawable/progress_mtrl_alpha"
android:tint="?attr/colorControlNormal"
android:alpha="?attr/disabledAlpha" />
<item android:id="@id/background"
android:gravity="center_vertical|fill_horizontal">
<shape android:shape="rectangle"
android:tint="?attr/colorControlNormal">
<size android:height="@dimen/progress_bar_height_material" />
<solid android:color="@color/white_disabled_material" />
</shape>
</item>
<item android:id="@id/secondaryProgress">
<item android:id="@id/secondaryProgress"
android:gravity="center_vertical|fill_horizontal">
<scale android:scaleWidth="100%">
<nine-patch android:src="@drawable/progress_mtrl_alpha"
android:tint="?attr/colorControlActivated"
android:alpha="?attr/disabledAlpha" />
<shape android:shape="rectangle"
android:tint="?attr/colorControlActivated">
<size android:height="@dimen/progress_bar_height_material" />
<solid android:color="@color/white_disabled_material" />
</shape>
</scale>
</item>
<item android:id="@id/progress">
<item android:id="@id/progress"
android:gravity="center_vertical|fill_horizontal">
<scale android:scaleWidth="100%">
<nine-patch android:src="@drawable/progress_mtrl_alpha"
android:tint="?attr/colorControlActivated" />
<shape android:shape="rectangle"
android:tint="?attr/colorControlActivated">
<size android:height="@dimen/progress_bar_height_material" />
<solid android:color="@color/white" />
</shape>
</scale>
</item>
</layer-list>

View File

@@ -15,32 +15,42 @@
-->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@id/background">
<nine-patch android:src="@drawable/scrubber_track_mtrl_alpha"
android:tint="?attr/colorControlNormal" />
<item android:id="@id/background"
android:gravity="center_vertical|fill_horizontal">
<shape android:shape="rectangle"
android:tint="?attr/colorControlNormal">
<size android:height="@dimen/scrubber_track_height_material" />
<solid android:color="@color/white_disabled_material" />
</shape>
</item>
<item android:id="@id/secondaryProgress">
<item android:id="@id/secondaryProgress"
android:gravity="center_vertical|fill_horizontal">
<scale android:scaleWidth="100%">
<selector>
<item android:state_enabled="false">
<color android:color="@color/transparent" />
</item>
<item android:state_enabled="false"
android:drawable="@color/transparent" />
<item>
<nine-patch android:src="@drawable/scrubber_primary_mtrl_alpha"
android:tint="?attr/colorControlNormal" />
<shape android:shape="rectangle"
android:tint="?attr/colorControlActivated">
<size android:height="@dimen/scrubber_track_height_material" />
<solid android:color="@color/white_disabled_material" />
</shape>
</item>
</selector>
</scale>
</item>
<item android:id="@id/progress">
<item android:id="@id/progress"
android:gravity="center_vertical|fill_horizontal">
<scale android:scaleWidth="100%">
<selector>
<item android:state_enabled="false">
<color android:color="@color/transparent" />
</item>
<item android:state_enabled="false"
android:drawable="@color/transparent" />
<item>
<nine-patch android:src="@drawable/scrubber_primary_mtrl_alpha"
android:tint="?attr/colorControlActivated" />
<shape android:shape="rectangle"
android:tint="?attr/colorControlActivated">
<size android:height="@dimen/progress_bar_height_material" />
<solid android:color="@color/white" />
</shape>
</item>
</selector>
</scale>

View File

@@ -16,12 +16,12 @@
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:gravity="center_vertical|fill_horizontal"
android:start="2dp"
android:end="2dp">
android:start="4dp"
android:end="4dp">
<shape android:shape="rectangle"
android:tint="@color/switch_track_material">
<corners android:radius="7dp" />
<solid android:color="#4dffffff" />
<solid android:color="@color/white_disabled_material" />
<size android:height="14dp" />
</shape>
</item>

View File

@@ -115,5 +115,6 @@
<!-- Padding above and below selection dialog lists. -->
<dimen name="dialog_list_padding_vertical_material">8dp</dimen>
<dimen name="scrubber_track_height_material">2dp</dimen>
<dimen name="progress_bar_height_material">4dp</dimen>
</resources>

View File

@@ -558,10 +558,9 @@ public class LayerDrawable extends Drawable implements Drawable.Callback {
* default layer gravity behavior. See {@link #setLayerGravity(int, int)}
* for more information.
*
* @param index the index of the drawable to adjust
* @param index the index of the layer to adjust
* @param w width in pixels, or -1 to use the intrinsic width
* @param h height in pixels, or -1 to use the intrinsic height
*
* @see #getLayerWidth(int)
* @see #getLayerHeight(int)
* @attr ref android.R.styleable#LayerDrawableItem_width
@@ -573,10 +572,19 @@ public class LayerDrawable extends Drawable implements Drawable.Callback {
childDrawable.mHeight = h;
}
/**
* @param index the index of the layer to adjust
* @param w width in pixels, or -1 to use the intrinsic width
* @attr ref android.R.styleable#LayerDrawableItem_width
*/
public void setLayerWidth(int index, int w) {
final ChildDrawable childDrawable = mLayerState.mChildren[index];
childDrawable.mWidth = w;
}
/**
* @param index the index of the drawable to adjust
* @return the explicit width of the layer, or -1 if not specified
*
* @see #setLayerSize(int, int, int)
* @attr ref android.R.styleable#LayerDrawableItem_width
*/
@@ -585,10 +593,19 @@ public class LayerDrawable extends Drawable implements Drawable.Callback {
return childDrawable.mWidth;
}
/**
* @param index the index of the layer to adjust
* @param h height in pixels, or -1 to use the intrinsic height
* @attr ref android.R.styleable#LayerDrawableItem_height
*/
public void setLayerHeight(int index, int h) {
final ChildDrawable childDrawable = mLayerState.mChildren[index];
childDrawable.mHeight = h;
}
/**
* @param index the index of the drawable to adjust
* @return the explicit height of the layer, or -1 if not specified
*
* @see #setLayerSize(int, int, int)
* @attr ref android.R.styleable#LayerDrawableItem_height
*/
@@ -656,7 +673,7 @@ public class LayerDrawable extends Drawable implements Drawable.Callback {
* Specifies the relative insets in pixels for the drawable at the
* specified index.
*
* @param index the index of the drawable to adjust
* @param index the index of the layer to adjust
* @param s number of pixels to inset from the start bound
* @param t number of pixels to inset from the top bound
* @param e number of pixels to inset from the end bound
@@ -671,6 +688,126 @@ public class LayerDrawable extends Drawable implements Drawable.Callback {
setLayerInsetInternal(index, 0, t, 0, b, s, e);
}
/**
* @param index the index of the layer to adjust
* @param l number of pixels to inset from the left bound
* @attr ref android.R.styleable#LayerDrawableItem_left
*/
public void setLayerInsetLeft(int index, int l) {
final ChildDrawable childDrawable = mLayerState.mChildren[index];
childDrawable.mInsetL = l;
}
/**
* @param index the index of the layer
* @return number of pixels to inset from the left bound
* @attr ref android.R.styleable#LayerDrawableItem_left
*/
public int getLayerInsetLeft(int index) {
final ChildDrawable childDrawable = mLayerState.mChildren[index];
return childDrawable.mInsetL;
}
/**
* @param index the index of the layer to adjust
* @param r number of pixels to inset from the right bound
* @attr ref android.R.styleable#LayerDrawableItem_right
*/
public void setLayerInsetRight(int index, int r) {
final ChildDrawable childDrawable = mLayerState.mChildren[index];
childDrawable.mInsetR = r;
}
/**
* @param index the index of the layer
* @return number of pixels to inset from the right bound
* @attr ref android.R.styleable#LayerDrawableItem_right
*/
public int getLayerInsetRight(int index) {
final ChildDrawable childDrawable = mLayerState.mChildren[index];
return childDrawable.mInsetR;
}
/**
* @param index the index of the layer to adjust
* @param t number of pixels to inset from the top bound
* @attr ref android.R.styleable#LayerDrawableItem_top
*/
public void setLayerInsetTop(int index, int t) {
final ChildDrawable childDrawable = mLayerState.mChildren[index];
childDrawable.mInsetT = t;
}
/**
* @param index the index of the layer
* @return number of pixels to inset from the top bound
* @attr ref android.R.styleable#LayerDrawableItem_top
*/
public int getLayerInsetTop(int index) {
final ChildDrawable childDrawable = mLayerState.mChildren[index];
return childDrawable.mInsetT;
}
/**
* @param index the index of the layer to adjust
* @param b number of pixels to inset from the bottom bound
* @attr ref android.R.styleable#LayerDrawableItem_bottom
*/
public void setLayerInsetBottom(int index, int b) {
final ChildDrawable childDrawable = mLayerState.mChildren[index];
childDrawable.mInsetB = b;
}
/**
* @param index the index of the layer
* @return number of pixels to inset from the bottom bound
* @attr ref android.R.styleable#LayerDrawableItem_bottom
*/
public int getLayerInsetBottom(int index) {
final ChildDrawable childDrawable = mLayerState.mChildren[index];
return childDrawable.mInsetB;
}
/**
* @param index the index of the layer to adjust
* @param s number of pixels to inset from the start bound
* @attr ref android.R.styleable#LayerDrawableItem_start
*/
public void setLayerInsetStart(int index, int s) {
final ChildDrawable childDrawable = mLayerState.mChildren[index];
childDrawable.mInsetS = s;
}
/**
* @param index the index of the layer
* @return number of pixels to inset from the start bound
* @attr ref android.R.styleable#LayerDrawableItem_start
*/
public int getLayerInsetStart(int index) {
final ChildDrawable childDrawable = mLayerState.mChildren[index];
return childDrawable.mInsetS;
}
/**
* @param index the index of the layer to adjust
* @param e number of pixels to inset from the end bound
* @attr ref android.R.styleable#LayerDrawableItem_end
*/
public void setLayerInsetEnd(int index, int e) {
final ChildDrawable childDrawable = mLayerState.mChildren[index];
childDrawable.mInsetE = e;
}
/**
* @param index the index of the layer
* @return number of pixels to inset from the end bound
* @attr ref android.R.styleable#LayerDrawableItem_end
*/
public int getLayerInsetEnd(int index) {
final ChildDrawable childDrawable = mLayerState.mChildren[index];
return childDrawable.mInsetE;
}
private void setLayerInsetInternal(int index, int l, int t, int r, int b, int s, int e) {
final ChildDrawable childDrawable = mLayerState.mChildren[index];
childDrawable.mInsetL = l;