diff options
author | Alan Viverette <alanv@google.com> | 2014-02-12 13:50:34 -0800 |
---|---|---|
committer | Alan Viverette <alanv@google.com> | 2014-02-12 13:50:34 -0800 |
commit | 861621eb8a3bd8896bc00c4c5bc8af6816215e21 (patch) | |
tree | 67db8ff1a26254857dcfe089f4218d3a5610dca9 /graphics/java | |
parent | c65d26ee0e3792fc17c156c48e62b24f242e0c8d (diff) | |
download | frameworks_base-861621eb8a3bd8896bc00c4c5bc8af6816215e21.zip frameworks_base-861621eb8a3bd8896bc00c4c5bc8af6816215e21.tar.gz frameworks_base-861621eb8a3bd8896bc00c4c5bc8af6816215e21.tar.bz2 |
Fix gradient radius attribute to match documentation
BUG: 12191666
Change-Id: I62de3296500543797c595eeee15aa250e5b1511a
Diffstat (limited to 'graphics/java')
-rw-r--r-- | graphics/java/android/graphics/drawable/GradientDrawable.java | 60 |
1 files changed, 49 insertions, 11 deletions
diff --git a/graphics/java/android/graphics/drawable/GradientDrawable.java b/graphics/java/android/graphics/drawable/GradientDrawable.java index e654db1..a345e91 100644 --- a/graphics/java/android/graphics/drawable/GradientDrawable.java +++ b/graphics/java/android/graphics/drawable/GradientDrawable.java @@ -32,6 +32,7 @@ import android.graphics.Rect; import android.graphics.RectF; import android.graphics.Shader; import android.graphics.SweepGradient; +import android.os.Build; import android.util.AttributeSet; import android.util.Log; import android.util.TypedValue; @@ -130,6 +131,9 @@ public class GradientDrawable extends Drawable { private Path mRingPath; private boolean mPathIsDirty = true; + /** Current gradient radius, valid when {@link #mRectIsDirty} is false. */ + private float mGradientRadius; + /** * Controls how the gradient is oriented relative to the drawable's bounds */ @@ -401,12 +405,27 @@ public class GradientDrawable extends Drawable { * @see #setGradientType(int) */ public void setGradientRadius(float gradientRadius) { - mGradientState.setGradientRadius(gradientRadius); + mGradientState.setGradientRadius(gradientRadius, TypedValue.COMPLEX_UNIT_PX); mRectIsDirty = true; invalidateSelf(); } /** + * Returns the radius of the gradient in pixels. The radius is valid only + * when the gradient type is set to {@link #RADIAL_GRADIENT}. + * + * @return Radius in pixels. + */ + public float getGradientRadius() { + if (mGradientState.mGradient != RADIAL_GRADIENT) { + return 0; + } + + ensureValidRect(); + return mGradientRadius; + } + + /** * <p>Sets whether or not this drawable will honor its <code>level</code> * property.</p> * <p><strong>Note</strong>: changing this property will affect all instances @@ -872,11 +891,19 @@ public class GradientDrawable extends Drawable { x0 = r.left + (r.right - r.left) * st.mCenterX; y0 = r.top + (r.bottom - r.top) * st.mCenterY; - final float level = st.mUseLevel ? (float) getLevel() / 10000.0f : 1.0f; - - mFillPaint.setShader(new RadialGradient(x0, y0, - level * st.mGradientRadius, colors, null, - Shader.TileMode.CLAMP)); + float radius = st.mGradientRadius; + if (st.mGradientRadiusUnit == TypedValue.COMPLEX_UNIT_FRACTION) { + radius *= Math.min(st.mWidth, st.mHeight); + } else if (st.mGradientRadiusUnit == TypedValue.COMPLEX_UNIT_FRACTION_PARENT) { + radius *= Math.min(r.width(), r.height()); + } + + if (st.mUseLevel) { + radius *= getLevel() / 10000.0f; + } + mGradientRadius = radius; + mFillPaint.setShader(new RadialGradient( + x0, y0, mGradientRadius, colors, null, Shader.TileMode.CLAMP)); } else if (st.mGradient == SWEEP_GRADIENT) { x0 = r.left + (r.right - r.left) * st.mCenterX; y0 = r.top + (r.bottom - r.top) * st.mCenterY; @@ -1051,12 +1078,20 @@ public class GradientDrawable extends Drawable { break; } } else { - TypedValue tv = a.peekValue( + final TypedValue tv = a.peekValue( com.android.internal.R.styleable.GradientDrawableGradient_gradientRadius); if (tv != null) { - boolean radiusRel = tv.type == TypedValue.TYPE_FRACTION; - st.mGradientRadius = radiusRel ? - tv.getFraction(1.0f, 1.0f) : tv.getFloat(); + final float radius; + final int unit; + if (tv.type == TypedValue.TYPE_FRACTION) { + radius = tv.getFraction(1.0f, 1.0f); + unit = tv.data & TypedValue.COMPLEX_UNIT_MASK; + } else { + radius = tv.getDimension(r.getDisplayMetrics()); + unit = TypedValue.COMPLEX_UNIT_PX; + } + st.mGradientRadius = radius; + st.mGradientRadiusUnit = unit; } else if (gradientType == RADIAL_GRADIENT) { throw new XmlPullParserException( a.getPositionDescription() @@ -1218,6 +1253,7 @@ public class GradientDrawable extends Drawable { private float mCenterX = 0.5f; private float mCenterY = 0.5f; private float mGradientRadius = 0.5f; + private int mGradientRadiusUnit = TypedValue.COMPLEX_UNIT_PX; private boolean mUseLevel; private boolean mUseLevelForShape; private boolean mOpaque; @@ -1259,6 +1295,7 @@ public class GradientDrawable extends Drawable { mCenterX = state.mCenterX; mCenterY = state.mCenterY; mGradientRadius = state.mGradientRadius; + mGradientRadiusUnit = state.mGradientRadiusUnit; mUseLevel = state.mUseLevel; mUseLevelForShape = state.mUseLevelForShape; mOpaque = state.mOpaque; @@ -1375,8 +1412,9 @@ public class GradientDrawable extends Drawable { mHeight = height; } - public void setGradientRadius(float gradientRadius) { + public void setGradientRadius(float gradientRadius, int type) { mGradientRadius = gradientRadius; + mGradientRadiusUnit = type; } } |