Merge "Fix gradient radius attribute to match documentation"
This commit is contained in:
committed by
Android (Google) Code Review
commit
cbdec7dd8c
@@ -10542,6 +10542,7 @@ package android.graphics.drawable {
|
|||||||
ctor public GradientDrawable();
|
ctor public GradientDrawable();
|
||||||
ctor public GradientDrawable(android.graphics.drawable.GradientDrawable.Orientation, int[]);
|
ctor public GradientDrawable(android.graphics.drawable.GradientDrawable.Orientation, int[]);
|
||||||
method public void draw(android.graphics.Canvas);
|
method public void draw(android.graphics.Canvas);
|
||||||
|
method public float getGradientRadius();
|
||||||
method public int getOpacity();
|
method public int getOpacity();
|
||||||
method public android.graphics.drawable.GradientDrawable.Orientation getOrientation();
|
method public android.graphics.drawable.GradientDrawable.Orientation getOrientation();
|
||||||
method public boolean onStateChange(int[]);
|
method public boolean onStateChange(int[]);
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ import android.graphics.Rect;
|
|||||||
import android.graphics.RectF;
|
import android.graphics.RectF;
|
||||||
import android.graphics.Shader;
|
import android.graphics.Shader;
|
||||||
import android.graphics.SweepGradient;
|
import android.graphics.SweepGradient;
|
||||||
|
import android.os.Build;
|
||||||
import android.util.AttributeSet;
|
import android.util.AttributeSet;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.util.TypedValue;
|
import android.util.TypedValue;
|
||||||
@@ -130,6 +131,9 @@ public class GradientDrawable extends Drawable {
|
|||||||
private Path mRingPath;
|
private Path mRingPath;
|
||||||
private boolean mPathIsDirty = true;
|
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
|
* Controls how the gradient is oriented relative to the drawable's bounds
|
||||||
*/
|
*/
|
||||||
@@ -401,11 +405,26 @@ public class GradientDrawable extends Drawable {
|
|||||||
* @see #setGradientType(int)
|
* @see #setGradientType(int)
|
||||||
*/
|
*/
|
||||||
public void setGradientRadius(float gradientRadius) {
|
public void setGradientRadius(float gradientRadius) {
|
||||||
mGradientState.setGradientRadius(gradientRadius);
|
mGradientState.setGradientRadius(gradientRadius, TypedValue.COMPLEX_UNIT_PX);
|
||||||
mRectIsDirty = true;
|
mRectIsDirty = true;
|
||||||
invalidateSelf();
|
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>
|
* <p>Sets whether or not this drawable will honor its <code>level</code>
|
||||||
* property.</p>
|
* property.</p>
|
||||||
@@ -872,11 +891,19 @@ public class GradientDrawable extends Drawable {
|
|||||||
x0 = r.left + (r.right - r.left) * st.mCenterX;
|
x0 = r.left + (r.right - r.left) * st.mCenterX;
|
||||||
y0 = r.top + (r.bottom - r.top) * st.mCenterY;
|
y0 = r.top + (r.bottom - r.top) * st.mCenterY;
|
||||||
|
|
||||||
final float level = st.mUseLevel ? (float) getLevel() / 10000.0f : 1.0f;
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
mFillPaint.setShader(new RadialGradient(x0, y0,
|
if (st.mUseLevel) {
|
||||||
level * st.mGradientRadius, colors, null,
|
radius *= getLevel() / 10000.0f;
|
||||||
Shader.TileMode.CLAMP));
|
}
|
||||||
|
mGradientRadius = radius;
|
||||||
|
mFillPaint.setShader(new RadialGradient(
|
||||||
|
x0, y0, mGradientRadius, colors, null, Shader.TileMode.CLAMP));
|
||||||
} else if (st.mGradient == SWEEP_GRADIENT) {
|
} else if (st.mGradient == SWEEP_GRADIENT) {
|
||||||
x0 = r.left + (r.right - r.left) * st.mCenterX;
|
x0 = r.left + (r.right - r.left) * st.mCenterX;
|
||||||
y0 = r.top + (r.bottom - r.top) * st.mCenterY;
|
y0 = r.top + (r.bottom - r.top) * st.mCenterY;
|
||||||
@@ -1051,12 +1078,20 @@ public class GradientDrawable extends Drawable {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
TypedValue tv = a.peekValue(
|
final TypedValue tv = a.peekValue(
|
||||||
com.android.internal.R.styleable.GradientDrawableGradient_gradientRadius);
|
com.android.internal.R.styleable.GradientDrawableGradient_gradientRadius);
|
||||||
if (tv != null) {
|
if (tv != null) {
|
||||||
boolean radiusRel = tv.type == TypedValue.TYPE_FRACTION;
|
final float radius;
|
||||||
st.mGradientRadius = radiusRel ?
|
final int unit;
|
||||||
tv.getFraction(1.0f, 1.0f) : tv.getFloat();
|
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) {
|
} else if (gradientType == RADIAL_GRADIENT) {
|
||||||
throw new XmlPullParserException(
|
throw new XmlPullParserException(
|
||||||
a.getPositionDescription()
|
a.getPositionDescription()
|
||||||
@@ -1218,6 +1253,7 @@ public class GradientDrawable extends Drawable {
|
|||||||
private float mCenterX = 0.5f;
|
private float mCenterX = 0.5f;
|
||||||
private float mCenterY = 0.5f;
|
private float mCenterY = 0.5f;
|
||||||
private float mGradientRadius = 0.5f;
|
private float mGradientRadius = 0.5f;
|
||||||
|
private int mGradientRadiusUnit = TypedValue.COMPLEX_UNIT_PX;
|
||||||
private boolean mUseLevel;
|
private boolean mUseLevel;
|
||||||
private boolean mUseLevelForShape;
|
private boolean mUseLevelForShape;
|
||||||
private boolean mOpaque;
|
private boolean mOpaque;
|
||||||
@@ -1259,6 +1295,7 @@ public class GradientDrawable extends Drawable {
|
|||||||
mCenterX = state.mCenterX;
|
mCenterX = state.mCenterX;
|
||||||
mCenterY = state.mCenterY;
|
mCenterY = state.mCenterY;
|
||||||
mGradientRadius = state.mGradientRadius;
|
mGradientRadius = state.mGradientRadius;
|
||||||
|
mGradientRadiusUnit = state.mGradientRadiusUnit;
|
||||||
mUseLevel = state.mUseLevel;
|
mUseLevel = state.mUseLevel;
|
||||||
mUseLevelForShape = state.mUseLevelForShape;
|
mUseLevelForShape = state.mUseLevelForShape;
|
||||||
mOpaque = state.mOpaque;
|
mOpaque = state.mOpaque;
|
||||||
@@ -1375,8 +1412,9 @@ public class GradientDrawable extends Drawable {
|
|||||||
mHeight = height;
|
mHeight = height;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setGradientRadius(float gradientRadius) {
|
public void setGradientRadius(float gradientRadius, int type) {
|
||||||
mGradientRadius = gradientRadius;
|
mGradientRadius = gradientRadius;
|
||||||
|
mGradientRadiusUnit = type;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user