diff --git a/api/current.txt b/api/current.txt index 1eb37b3a45c47..d1cfb62c60070 100644 --- a/api/current.txt +++ b/api/current.txt @@ -12302,6 +12302,7 @@ package android.graphics.drawable { method public int getIntrinsicWidth(); method public int getLayoutDirection(); method public final int getLevel(); + method public final float getLevelFloat(); method public int getMinimumHeight(); method public int getMinimumWidth(); method public abstract int getOpacity(); @@ -12321,6 +12322,7 @@ package android.graphics.drawable { method protected void onBoundsChange(android.graphics.Rect); method public boolean onLayoutDirectionChanged(int); method protected boolean onLevelChange(int); + method protected boolean onLevelChange(float); method protected boolean onStateChange(int[]); method public static int resolveOpacity(int, int); method public void scheduleSelf(java.lang.Runnable, long); @@ -12338,12 +12340,15 @@ package android.graphics.drawable { method public void setHotspotBounds(int, int, int, int); method public final boolean setLayoutDirection(int); method public final boolean setLevel(int); + method public final boolean setLevel(float); method public boolean setState(int[]); method public void setTint(int); method public void setTintList(android.content.res.ColorStateList); method public void setTintMode(android.graphics.PorterDuff.Mode); method public boolean setVisible(boolean, boolean); method public void unscheduleSelf(java.lang.Runnable); + field public static final int MAX_LEVEL = 10000; // 0x2710 + field public static final float MAX_LEVEL_FLOAT = 10000.0f; } public static abstract interface Drawable.Callback { diff --git a/api/system-current.txt b/api/system-current.txt index f913e6c99b2cc..8bd0181804768 100644 --- a/api/system-current.txt +++ b/api/system-current.txt @@ -12639,6 +12639,7 @@ package android.graphics.drawable { method public int getIntrinsicWidth(); method public int getLayoutDirection(); method public final int getLevel(); + method public final float getLevelFloat(); method public int getMinimumHeight(); method public int getMinimumWidth(); method public abstract int getOpacity(); @@ -12658,6 +12659,7 @@ package android.graphics.drawable { method protected void onBoundsChange(android.graphics.Rect); method public boolean onLayoutDirectionChanged(int); method protected boolean onLevelChange(int); + method protected boolean onLevelChange(float); method protected boolean onStateChange(int[]); method public static int resolveOpacity(int, int); method public void scheduleSelf(java.lang.Runnable, long); @@ -12675,12 +12677,15 @@ package android.graphics.drawable { method public void setHotspotBounds(int, int, int, int); method public final boolean setLayoutDirection(int); method public final boolean setLevel(int); + method public final boolean setLevel(float); method public boolean setState(int[]); method public void setTint(int); method public void setTintList(android.content.res.ColorStateList); method public void setTintMode(android.graphics.PorterDuff.Mode); method public boolean setVisible(boolean, boolean); method public void unscheduleSelf(java.lang.Runnable); + field public static final int MAX_LEVEL = 10000; // 0x2710 + field public static final float MAX_LEVEL_FLOAT = 10000.0f; } public static abstract interface Drawable.Callback { diff --git a/graphics/java/android/graphics/drawable/AnimatedVectorDrawable.java b/graphics/java/android/graphics/drawable/AnimatedVectorDrawable.java index 1857345968fd7..abb51dbac1b3f 100644 --- a/graphics/java/android/graphics/drawable/AnimatedVectorDrawable.java +++ b/graphics/java/android/graphics/drawable/AnimatedVectorDrawable.java @@ -217,7 +217,7 @@ public class AnimatedVectorDrawable extends Drawable implements Animatable2 { } @Override - protected boolean onLevelChange(int level) { + protected boolean onLevelChange(float level) { return mAnimatedVectorState.mVectorDrawable.setLevel(level); } diff --git a/graphics/java/android/graphics/drawable/ClipDrawable.java b/graphics/java/android/graphics/drawable/ClipDrawable.java index 31fccd0880c9b..3b9250795992a 100644 --- a/graphics/java/android/graphics/drawable/ClipDrawable.java +++ b/graphics/java/android/graphics/drawable/ClipDrawable.java @@ -52,8 +52,6 @@ public class ClipDrawable extends DrawableWrapper { public static final int HORIZONTAL = 1; public static final int VERTICAL = 2; - private static final int MAX_LEVEL = 10000; - private final Rect mTmpRect = new Rect(); private ClipState mState; @@ -143,7 +141,7 @@ public class ClipDrawable extends DrawableWrapper { } @Override - protected boolean onLevelChange(int level) { + protected boolean onLevelChange(float level) { super.onLevelChange(level); invalidateSelf(); return true; @@ -153,12 +151,12 @@ public class ClipDrawable extends DrawableWrapper { public int getOpacity() { final Drawable dr = getDrawable(); final int opacity = dr.getOpacity(); - if (opacity == PixelFormat.TRANSPARENT || dr.getLevel() == 0) { + if (opacity == PixelFormat.TRANSPARENT || dr.getLevelFloat() == 0) { return PixelFormat.TRANSPARENT; } - final int level = getLevel(); - if (level >= MAX_LEVEL) { + final float level = getLevelFloat(); + if (level >= MAX_LEVEL_FLOAT) { return dr.getOpacity(); } @@ -169,24 +167,24 @@ public class ClipDrawable extends DrawableWrapper { @Override public void draw(Canvas canvas) { final Drawable dr = getDrawable(); - if (dr.getLevel() == 0) { + if (dr.getLevelFloat() == 0) { return; } final Rect r = mTmpRect; final Rect bounds = getBounds(); - final int level = getLevel(); + final float level = getLevelFloat(); int w = bounds.width(); - final int iw = 0; //mState.mDrawable.getIntrinsicWidth(); + final int iw = 0; if ((mState.mOrientation & HORIZONTAL) != 0) { - w -= (w - iw) * (MAX_LEVEL - level) / MAX_LEVEL; + w -= Math.round((w - iw) * (MAX_LEVEL_FLOAT - level) / MAX_LEVEL_FLOAT); } int h = bounds.height(); - final int ih = 0; //mState.mDrawable.getIntrinsicHeight(); + final int ih = 0; if ((mState.mOrientation & VERTICAL) != 0) { - h -= (h - ih) * (MAX_LEVEL - level) / MAX_LEVEL; + h -= Math.round((h - ih) * (MAX_LEVEL_FLOAT - level) / MAX_LEVEL_FLOAT); } final int layoutDirection = getLayoutDirection(); diff --git a/graphics/java/android/graphics/drawable/Drawable.java b/graphics/java/android/graphics/drawable/Drawable.java index b95c183642ca2..fb7715507103c 100644 --- a/graphics/java/android/graphics/drawable/Drawable.java +++ b/graphics/java/android/graphics/drawable/Drawable.java @@ -41,6 +41,7 @@ import android.graphics.Xfermode; import android.os.Trace; import android.util.AttributeSet; import android.util.DisplayMetrics; +import android.util.FloatProperty; import android.util.StateSet; import android.util.TypedValue; import android.util.Xml; @@ -126,12 +127,19 @@ import java.util.Collection; * document.

*/ public abstract class Drawable { + private static final Rect ZERO_BOUNDS_RECT = new Rect(); static final PorterDuff.Mode DEFAULT_TINT_MODE = PorterDuff.Mode.SRC_IN; + /** The standard maximum value for calls to {@link #setLevel(int)}. */ + public static final int MAX_LEVEL = 10000; + + /** The standard maximum value for calls to {@link #setLevel(float)}. */ + public static final float MAX_LEVEL_FLOAT = 10000.0f; + private int[] mStateSet = StateSet.WILD_CARD; - private int mLevel = 0; + private float mLevel = 0.0f; private int mChangingConfigurations = 0; private Rect mBounds = ZERO_BOUNDS_RECT; // lazily becomes a new Rect() private WeakReference mCallback = null; @@ -711,22 +719,63 @@ public abstract class Drawable { } /** - * Specify the level for the drawable. This allows a drawable to vary its - * imagery based on a continuous controller, for example to show progress - * or volume level. + * Sets the level for the drawable as an integer value where typically the + * minimum level is 0 and the maximum is 10000 {@link #MAX_LEVEL}; however, + * the range may vary based on the Drawable implementation and is not + * clamped. + *

+ * This allows a drawable to vary its imagery based on a continuous + * controller. For example, it may be used to show progress or volume + * level. + *

+ * Use #setLevelFloat(float) to set the level as a high-precision + * floating-point value. * - *

If the new level you are supplying causes the appearance of the - * Drawable to change, then it is responsible for calling - * {@link #invalidateSelf} in order to have itself redrawn, and - * true will be returned from this function. - * - * @param level The new level, from 0 (minimum) to 10000 (maximum). - * - * @return Returns true if this change in level has caused the appearance - * of the Drawable to change (hence requiring an invalidate), otherwise - * returns false. + * @param level the new level, typically between 0 and 10000 + * @return {@code true} if this change in level has caused the appearance + * of the drawable to change and will require a subsequent call to + * invalidate, {@code false} otherwise + * @see #getLevel() + * @see #setLevel(float) + * @see #onLevelChange(int) */ public final boolean setLevel(int level) { + return setLevel((float) level); + } + + /** + * Returns the current level as a rounded integer value. + *

+ * Use #getLevelFloat() to return the level as a high-precision + * floating-point value. + * + * @return the current level, typically between 0 and 10000 + * @see #setLevel(int) + * @see #getLevelFloat() + */ + public final int getLevel() { + return Math.round(mLevel); + } + + /** + * Sets the level for the drawable as a floating-point value where + * typically the minimum level is 0.0 and the maximum is 10000.0 + * {@link #MAX_LEVEL_FLOAT}; however, the range may vary based on the + * Drawable implementation and is not clamped. + *

+ * This allows a drawable to vary its imagery based on a continuous + * controller. For example, it may be used to show progress or volume + * level. + * + * @param level the new level, typically between 0.0 and 10000.0 + * ({@link #MAX_LEVEL_FLOAT}) + * @return {@code true} if this change in level has caused the appearance + * of the drawable to change and will require a subsequent call to + * invalidate, {@code false} otherwise + * @see #getLevelFloat() + * @see #onLevelChange(float) + */ + public final boolean setLevel(float level) { if (mLevel != level) { mLevel = level; return onLevelChange(level); @@ -735,11 +784,13 @@ public abstract class Drawable { } /** - * Retrieve the current level. + * Returns the current level as a floating-point value. * - * @return int Current level, from 0 (minimum) to 10000 (maximum). + * @return the current level, typically between 0.0 and 10000.0 + * ({@link #MAX_LEVEL_FLOAT}) + * @see #setLevel(float) */ - public final int getLevel() { + public final float getLevelFloat() { return mLevel; } @@ -894,14 +945,47 @@ public abstract class Drawable { * last state. */ protected boolean onStateChange(int[] state) { return false; } - /** Override this in your subclass to change appearance if you vary based - * on level. - * @return Returns true if the level change has caused the appearance of - * the Drawable to change (that is, it needs to be drawn), else false - * if it looks the same and there is no need to redraw it since its - * last level. + + /** + * Called when the drawable level changes. + *

+ * Override this in your subclass to change appearance if you vary based on + * level and do not need floating-point accuracy. To handle changes with + * higher accuracy, override {@link #onLevelChange(float)} instead. + *

+ * Note: Do not override both this method and + * {@link #onLevelChange(float)}. Only override one method. + * + * @param level the level as an integer value, typically between 0 + * (minimum) and 10000 ({@link #MAX_LEVEL}) + * @return {@code true} if the level change has caused the appearance of + * the drawable to change such that it needs to be redrawn, or + * {@code false} if there is no need to redraw */ protected boolean onLevelChange(int level) { return false; } + + /** + * Called when the drawable level changes. + *

+ * Override this in your subclass to change appearance if you vary based on + * level and need floating-point accuracy. + *

+ * Note: Do not override both this method and + * {@link #onLevelChange(int)}. Only override one method. If your app + * targets SDK <= 23 ({@link android.os.Build.VERSION_CODES#M M}), you + * will need to override {@link #onLevelChange(int)} to receive callbacks + * on devices running Android M and below. + * + * @param level the level as a floating-point value, typically between 0.0 + * and 10000.0 ({@link #MAX_LEVEL_FLOAT}) + * @return {@code true} if the level change has caused the appearance of + * the drawable to change such that it needs to be redrawn, or + * {@code false} if there is no need to redraw + */ + protected boolean onLevelChange(float level) { + return onLevelChange(Math.round(level)); + } + /** * Override this in your subclass to change appearance if you vary based on * the bounds. @@ -1327,6 +1411,23 @@ public abstract class Drawable { return tintFilter; } + /** + * Animatable property for Drawable level. + * + * @hide Until Drawable animations have been cleaned up. + */ + public static final FloatProperty LEVEL = new FloatProperty("levelFloat") { + @Override + public Float get(Drawable object) { + return object.getLevelFloat(); + } + + @Override + public void setValue(Drawable object, float value) { + object.setLevel(value); + } + }; + /** * Obtains styled attributes from the theme, if available, or unstyled * resources if the theme is null. diff --git a/graphics/java/android/graphics/drawable/DrawableContainer.java b/graphics/java/android/graphics/drawable/DrawableContainer.java index 1915dd74ea459..0210ddb191ce5 100644 --- a/graphics/java/android/graphics/drawable/DrawableContainer.java +++ b/graphics/java/android/graphics/drawable/DrawableContainer.java @@ -322,7 +322,7 @@ public class DrawableContainer extends Drawable implements Drawable.Callback { } @Override - protected boolean onLevelChange(int level) { + protected boolean onLevelChange(float level) { if (mLastDrawable != null) { return mLastDrawable.setLevel(level); } @@ -510,7 +510,7 @@ public class DrawableContainer extends Drawable implements Drawable.Callback { d.setVisible(isVisible(), true); d.setDither(mDrawableContainerState.mDither); d.setState(getState()); - d.setLevel(getLevel()); + d.setLevel(getLevelFloat()); d.setBounds(getBounds()); d.setLayoutDirection(getLayoutDirection()); d.setAutoMirrored(mDrawableContainerState.mAutoMirrored); diff --git a/graphics/java/android/graphics/drawable/DrawableWrapper.java b/graphics/java/android/graphics/drawable/DrawableWrapper.java index 9185e1a064662..57b4db244c564 100644 --- a/graphics/java/android/graphics/drawable/DrawableWrapper.java +++ b/graphics/java/android/graphics/drawable/DrawableWrapper.java @@ -92,7 +92,7 @@ public abstract class DrawableWrapper extends Drawable implements Drawable.Callb // Only call setters for data that's stored in the base Drawable. dr.setVisible(isVisible(), true); dr.setState(getState()); - dr.setLevel(getLevel()); + dr.setLevel(getLevelFloat()); dr.setBounds(getBounds()); dr.setLayoutDirection(getLayoutDirection()); @@ -286,7 +286,7 @@ public abstract class DrawableWrapper extends Drawable implements Drawable.Callb } @Override - protected boolean onLevelChange(int level) { + protected boolean onLevelChange(float level) { return mDrawable != null && mDrawable.setLevel(level); } diff --git a/graphics/java/android/graphics/drawable/GradientDrawable.java b/graphics/java/android/graphics/drawable/GradientDrawable.java index d7fd8a55d4045..15295a03abd79 100644 --- a/graphics/java/android/graphics/drawable/GradientDrawable.java +++ b/graphics/java/android/graphics/drawable/GradientDrawable.java @@ -530,8 +530,8 @@ public class GradientDrawable extends Drawable { * {@code false} otherwise * * @see #mutate() - * @see #setLevel(int) - * @see #getLevel() + * @see #setLevel(float) + * @see #getLevelFloat() * @see #isUseLevel() */ public void setUseLevel(boolean useLevel) { @@ -764,7 +764,7 @@ public class GradientDrawable extends Drawable { if (mRingPath != null && (!st.mUseLevelForShape || !mPathIsDirty)) return mRingPath; mPathIsDirty = false; - float sweep = st.mUseLevelForShape ? (360.0f * getLevel() / 10000.0f) : 360f; + float sweep = st.mUseLevelForShape ? (360.0f * getLevelFloat() / MAX_LEVEL_FLOAT) : 360f; RectF bounds = new RectF(mRect); @@ -990,7 +990,7 @@ public class GradientDrawable extends Drawable { } @Override - protected boolean onLevelChange(int level) { + protected boolean onLevelChange(float level) { super.onLevelChange(level); mGradientIsDirty = true; mPathIsDirty = true; @@ -1026,7 +1026,7 @@ public class GradientDrawable extends Drawable { final float x0, x1, y0, y1; if (st.mGradient == LINEAR_GRADIENT) { - final float level = st.mUseLevel ? getLevel() / 10000.0f : 1.0f; + final float level = st.mUseLevel ? getLevelFloat() / MAX_LEVEL_FLOAT : 1.0f; switch (st.mOrientation) { case TOP_BOTTOM: x0 = r.left; y0 = r.top; @@ -1080,7 +1080,7 @@ public class GradientDrawable extends Drawable { } if (st.mUseLevel) { - radius *= getLevel() / 10000.0f; + radius *= getLevelFloat() / MAX_LEVEL_FLOAT; } mGradientRadius = radius; @@ -1115,7 +1115,7 @@ public class GradientDrawable extends Drawable { tempPositions = st.mTempPositions = new float[length + 1]; } - final float level = getLevel() / 10000.0f; + final float level = getLevelFloat() / MAX_LEVEL_FLOAT; for (int i = 0; i < length; i++) { tempPositions[i] = i * fraction * level; } diff --git a/graphics/java/android/graphics/drawable/LayerDrawable.java b/graphics/java/android/graphics/drawable/LayerDrawable.java index 1a0ba6f2eabc6..c9e38b9841ebb 100644 --- a/graphics/java/android/graphics/drawable/LayerDrawable.java +++ b/graphics/java/android/graphics/drawable/LayerDrawable.java @@ -1400,7 +1400,7 @@ public class LayerDrawable extends Drawable implements Drawable.Callback { } @Override - protected boolean onLevelChange(int level) { + protected boolean onLevelChange(float level) { boolean changed = false; final ChildDrawable[] array = mLayerState.mChildren; @@ -1733,7 +1733,7 @@ public class LayerDrawable extends Drawable implements Drawable.Callback { clone.setCallback(owner); clone.setLayoutDirection(dr.getLayoutDirection()); clone.setBounds(dr.getBounds()); - clone.setLevel(dr.getLevel()); + clone.setLevel(dr.getLevelFloat()); } else { clone = null; } diff --git a/graphics/java/android/graphics/drawable/LevelListDrawable.java b/graphics/java/android/graphics/drawable/LevelListDrawable.java index b01c643a2a017..09d8b6fdb1e62 100644 --- a/graphics/java/android/graphics/drawable/LevelListDrawable.java +++ b/graphics/java/android/graphics/drawable/LevelListDrawable.java @@ -69,15 +69,16 @@ public class LevelListDrawable extends DrawableContainer { if (drawable != null) { mLevelListState.addLevel(low, high, drawable); // in case the new state matches our current state... - onLevelChange(getLevel()); + onLevelChange(getLevelFloat()); } } // overrides from Drawable @Override - protected boolean onLevelChange(int level) { - int idx = mLevelListState.indexOfLevel(level); + protected boolean onLevelChange(float level) { + final int nearestLevel = Math.round(level); + final int idx = mLevelListState.indexOfLevel(nearestLevel); if (selectDrawable(idx)) { return true; } @@ -141,7 +142,7 @@ public class LevelListDrawable extends DrawableContainer { mLevelListState.addLevel(low, high, dr); } - onLevelChange(getLevel()); + onLevelChange(getLevelFloat()); } @Override @@ -240,7 +241,7 @@ public class LevelListDrawable extends DrawableContainer { private LevelListDrawable(LevelListState state, Resources res) { final LevelListState as = new LevelListState(state, this, res); setConstantState(as); - onLevelChange(getLevel()); + onLevelChange(getLevelFloat()); } } diff --git a/graphics/java/android/graphics/drawable/RotateDrawable.java b/graphics/java/android/graphics/drawable/RotateDrawable.java index 036a078eb00d5..71c9977ba190b 100644 --- a/graphics/java/android/graphics/drawable/RotateDrawable.java +++ b/graphics/java/android/graphics/drawable/RotateDrawable.java @@ -303,10 +303,10 @@ public class RotateDrawable extends DrawableWrapper { } @Override - protected boolean onLevelChange(int level) { + protected boolean onLevelChange(float level) { super.onLevelChange(level); - final float value = level / (float) MAX_LEVEL; + final float value = level / (float) MAX_LEVEL_FLOAT; final float degrees = MathUtils.lerp(mState.mFromDegrees, mState.mToDegrees, value); mState.mCurrentDegrees = degrees; diff --git a/graphics/java/android/graphics/drawable/ScaleDrawable.java b/graphics/java/android/graphics/drawable/ScaleDrawable.java index 0acbeda1979e0..38c6b8093c1a8 100644 --- a/graphics/java/android/graphics/drawable/ScaleDrawable.java +++ b/graphics/java/android/graphics/drawable/ScaleDrawable.java @@ -50,8 +50,6 @@ import java.io.IOException; * @attr ref android.R.styleable#ScaleDrawable_drawable */ public class ScaleDrawable extends DrawableWrapper { - private static final int MAX_LEVEL = 10000; - private final Rect mTmpRect = new Rect(); private ScaleState mState; @@ -170,7 +168,7 @@ public class ScaleDrawable extends DrawableWrapper { @Override public void draw(Canvas canvas) { final Drawable d = getDrawable(); - if (d != null && d.getLevel() != 0) { + if (d != null && d.getLevelFloat() != 0) { d.draw(canvas); } } @@ -178,12 +176,12 @@ public class ScaleDrawable extends DrawableWrapper { @Override public int getOpacity() { final Drawable d = getDrawable(); - if (d.getLevel() == 0) { + if (d.getLevelFloat() == 0) { return PixelFormat.TRANSPARENT; } final int opacity = d.getOpacity(); - if (opacity == PixelFormat.OPAQUE && d.getLevel() < MAX_LEVEL) { + if (opacity == PixelFormat.OPAQUE && d.getLevelFloat() < MAX_LEVEL_FLOAT) { return PixelFormat.TRANSLUCENT; } @@ -191,7 +189,7 @@ public class ScaleDrawable extends DrawableWrapper { } @Override - protected boolean onLevelChange(int level) { + protected boolean onLevelChange(float level) { super.onLevelChange(level); onBoundsChange(getBounds()); invalidateSelf(); @@ -203,18 +201,20 @@ public class ScaleDrawable extends DrawableWrapper { final Drawable d = getDrawable(); final Rect r = mTmpRect; final boolean min = mState.mUseIntrinsicSizeAsMin; - final int level = getLevel(); + final float level = getLevelFloat(); int w = bounds.width(); if (mState.mScaleWidth > 0) { final int iw = min ? d.getIntrinsicWidth() : 0; - w -= (int) ((w - iw) * (MAX_LEVEL - level) * mState.mScaleWidth / MAX_LEVEL); + w -= (int) ((w - iw) * (MAX_LEVEL_FLOAT - level) + * mState.mScaleWidth / MAX_LEVEL_FLOAT); } int h = bounds.height(); if (mState.mScaleHeight > 0) { final int ih = min ? d.getIntrinsicHeight() : 0; - h -= (int) ((h - ih) * (MAX_LEVEL - level) * mState.mScaleHeight / MAX_LEVEL); + h -= (int) ((h - ih) * (MAX_LEVEL_FLOAT - level) + * mState.mScaleHeight / MAX_LEVEL_FLOAT); } final int layoutDirection = getLayoutDirection();