diff --git a/api/current.txt b/api/current.txt index 00fd83a0a02ae..0394d56eda5bf 100644 --- a/api/current.txt +++ b/api/current.txt @@ -12399,9 +12399,17 @@ package android.graphics.drawable { ctor public GradientDrawable(); ctor public GradientDrawable(android.graphics.drawable.GradientDrawable.Orientation, int[]); method public void draw(android.graphics.Canvas); + method public android.content.res.ColorStateList getColor(); + method public int[] getColors(); + method public float[] getCornerRadii(); + method public float getCornerRadius(); + method public float getGradientCenterX(); + method public float getGradientCenterY(); method public float getGradientRadius(); + method public int getGradientType(); method public int getOpacity(); method public android.graphics.drawable.GradientDrawable.Orientation getOrientation(); + method public boolean isUseLevel(); method public void setAlpha(int); method public void setColor(int); method public void setColor(android.content.res.ColorStateList); @@ -12535,10 +12543,12 @@ package android.graphics.drawable { ctor public deprecated NinePatchDrawable(android.graphics.NinePatch); ctor public NinePatchDrawable(android.content.res.Resources, android.graphics.NinePatch); method public void draw(android.graphics.Canvas); + method public android.graphics.NinePatch getNinePatch(); method public int getOpacity(); method public android.graphics.Paint getPaint(); method public void setAlpha(int); method public void setColorFilter(android.graphics.ColorFilter); + method public void setNinePatch(android.graphics.NinePatch); method public void setTargetDensity(android.graphics.Canvas); method public void setTargetDensity(android.util.DisplayMetrics); method public void setTargetDensity(int); diff --git a/api/system-current.txt b/api/system-current.txt index 2db8c2fe6320b..a1aa0424bf6d7 100644 --- a/api/system-current.txt +++ b/api/system-current.txt @@ -12736,9 +12736,17 @@ package android.graphics.drawable { ctor public GradientDrawable(); ctor public GradientDrawable(android.graphics.drawable.GradientDrawable.Orientation, int[]); method public void draw(android.graphics.Canvas); + method public android.content.res.ColorStateList getColor(); + method public int[] getColors(); + method public float[] getCornerRadii(); + method public float getCornerRadius(); + method public float getGradientCenterX(); + method public float getGradientCenterY(); method public float getGradientRadius(); + method public int getGradientType(); method public int getOpacity(); method public android.graphics.drawable.GradientDrawable.Orientation getOrientation(); + method public boolean isUseLevel(); method public void setAlpha(int); method public void setColor(int); method public void setColor(android.content.res.ColorStateList); @@ -12872,10 +12880,12 @@ package android.graphics.drawable { ctor public deprecated NinePatchDrawable(android.graphics.NinePatch); ctor public NinePatchDrawable(android.content.res.Resources, android.graphics.NinePatch); method public void draw(android.graphics.Canvas); + method public android.graphics.NinePatch getNinePatch(); method public int getOpacity(); method public android.graphics.Paint getPaint(); method public void setAlpha(int); method public void setColorFilter(android.graphics.ColorFilter); + method public void setNinePatch(android.graphics.NinePatch); method public void setTargetDensity(android.graphics.Canvas); method public void setTargetDensity(android.util.DisplayMetrics); method public void setTargetDensity(int); diff --git a/graphics/java/android/graphics/drawable/GradientDrawable.java b/graphics/java/android/graphics/drawable/GradientDrawable.java index a11b2cd3f0087..d7fd8a55d4045 100644 --- a/graphics/java/android/graphics/drawable/GradientDrawable.java +++ b/graphics/java/android/graphics/drawable/GradientDrawable.java @@ -17,10 +17,11 @@ package android.graphics.drawable; import android.annotation.ColorInt; +import android.annotation.Nullable; import android.content.res.ColorStateList; import android.content.res.Resources; -import android.content.res.TypedArray; import android.content.res.Resources.Theme; +import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.ColorFilter; @@ -198,34 +199,54 @@ public class GradientDrawable extends Drawable { } /** - *

Specify radii for each of the 4 corners. For each corner, the array - * contains 2 values, [X_radius, Y_radius]. The corners are ordered - * top-left, top-right, bottom-right, bottom-left. This property - * is honored only when the shape is of type {@link #RECTANGLE}.

- *

Note: changing this property will affect all instances + * Specifies radii for each of the 4 corners. For each corner, the array + * contains 2 values, [X_radius, Y_radius]. The corners are + * ordered top-left, top-right, bottom-right, bottom-left. This property + * is honored only when the shape is of type {@link #RECTANGLE}. + *

+ * Note: changing this property will affect all instances * of a drawable loaded from a resource. It is recommended to invoke - * {@link #mutate()} before changing this property.

+ * {@link #mutate()} before changing this property. * - * @param radii 4 pairs of X and Y radius for each corner, specified in pixels. - * The length of this array must be >= 8 + * @param radii an array of length >= 8 containing 4 pairs of X and Y + * radius for each corner, specified in pixels * * @see #mutate() - * @see #setCornerRadii(float[]) * @see #setShape(int) + * @see #setCornerRadius(float) */ - public void setCornerRadii(float[] radii) { + public void setCornerRadii(@Nullable float[] radii) { mGradientState.setCornerRadii(radii); mPathIsDirty = true; invalidateSelf(); } /** - *

Specify radius for the corners of the gradient. If this is > 0, then the - * drawable is drawn in a round-rectangle, rather than a rectangle. This property - * is honored only when the shape is of type {@link #RECTANGLE}.

- *

Note: changing this property will affect all instances + * Returns the radii for each of the 4 corners. For each corner, the array + * contains 2 values, [X_radius, Y_radius]. The corners are + * ordered top-left, top-right, bottom-right, bottom-left. + *

+ * If the radius was previously set with {@link #setCornerRadius(float)}, + * or if the corners are not rounded, this method will return {@code null}. + * + * @return an array containing the radii for each of the 4 corners, or + * {@code null} + * @see #setCornerRadii(float[]) + */ + @Nullable + public float[] getCornerRadii() { + return mGradientState.mRadiusArray.clone(); + } + + /** + * Specifies the radius for the corners of the gradient. If this is > 0, + * then the drawable is drawn in a round-rectangle, rather than a + * rectangle. This property is honored only when the shape is of type + * {@link #RECTANGLE}. + *

+ * Note: changing this property will affect all instances * of a drawable loaded from a resource. It is recommended to invoke - * {@link #mutate()} before changing this property.

+ * {@link #mutate()} before changing this property. * * @param radius The radius in pixels of the corners of the rectangle shape * @@ -239,6 +260,19 @@ public class GradientDrawable extends Drawable { invalidateSelf(); } + /** + * Returns the radius for the corners of the gradient. + *

+ * If the radius was previously set with {@link #setCornerRadii(float[])}, + * or if the corners are not rounded, this method will return {@code null}. + * + * @return the radius in pixels of the corners of the rectangle shape, or 0 + * @see #setCornerRadius + */ + public float getCornerRadius() { + return mGradientState.mRadius; + } + /** *

Set the stroke width and color for the drawable. If width is zero, * then no stroke is drawn.

@@ -376,15 +410,17 @@ public class GradientDrawable extends Drawable { } /** - *

Sets the type of gradient used by this drawable..

- *

Note: changing this property will affect all instances + * Sets the type of gradient used by this drawable. + *

+ * Note: changing this property will affect all instances * of a drawable loaded from a resource. It is recommended to invoke - * {@link #mutate()} before changing this property.

+ * {@link #mutate()} before changing this property. * * @param gradient The type of the gradient: {@link #LINEAR_GRADIENT}, * {@link #RADIAL_GRADIENT} or {@link #SWEEP_GRADIENT} * * @see #mutate() + * @see #getGradientType() */ public void setGradientType(int gradient) { mGradientState.setGradientType(gradient); @@ -393,17 +429,33 @@ public class GradientDrawable extends Drawable { } /** - *

Sets the center location of the gradient. The radius is honored only when - * the gradient type is set to {@link #RADIAL_GRADIENT} or {@link #SWEEP_GRADIENT}.

- *

Note: changing this property will affect all instances - * of a drawable loaded from a resource. It is recommended to invoke - * {@link #mutate()} before changing this property.

+ * Returns the type of gradient used by this drawable, one of + * {@link #LINEAR_GRADIENT}, {@link #RADIAL_GRADIENT}, or + * {@link #SWEEP_GRADIENT}. * - * @param x The x coordinate of the gradient's center - * @param y The y coordinate of the gradient's center + * @return the type of gradient used by this drawable + * @see #setGradientType(int) + */ + public int getGradientType() { + return mGradientState.mGradient; + } + + /** + * Sets the center location in pixels of the gradient. The radius is + * honored only when the gradient type is set to {@link #RADIAL_GRADIENT} + * or {@link #SWEEP_GRADIENT}. + *

+ * Note: changing this property will affect all instances + * of a drawable loaded from a resource. It is recommended to invoke + * {@link #mutate()} before changing this property. + * + * @param x the x coordinate of the gradient's center in pixels + * @param y the y coordinate of the gradient's center in pixels * * @see #mutate() * @see #setGradientType(int) + * @see #getGradientCenterX() + * @see #getGradientCenterY() */ public void setGradientCenter(float x, float y) { mGradientState.setGradientCenter(x, y); @@ -412,16 +464,38 @@ public class GradientDrawable extends Drawable { } /** - *

Sets the radius of the gradient. The radius is honored only when the - * gradient type is set to {@link #RADIAL_GRADIENT}.

- *

Note: changing this property will affect all instances - * of a drawable loaded from a resource. It is recommended to invoke - * {@link #mutate()} before changing this property.

+ * Returns the center X location of this gradient in pixels. * - * @param gradientRadius The radius of the gradient in pixels + * @return the center X location of this gradient in pixels + * @see #setGradientCenter(float, float) + */ + public float getGradientCenterX() { + return mGradientState.mCenterX; + } + + /** + * Returns the center Y location of this gradient in pixels. + * + * @return the center Y location of this gradient in pixels + * @see #setGradientCenter(float, float) + */ + public float getGradientCenterY() { + return mGradientState.mCenterY; + } + + /** + * Sets the radius of the gradient. The radius is honored only when the + * gradient type is set to {@link #RADIAL_GRADIENT}. + *

+ * Note: changing this property will affect all instances + * of a drawable loaded from a resource. It is recommended to invoke + * {@link #mutate()} before changing this property. + * + * @param gradientRadius the radius of the gradient in pixels * * @see #mutate() * @see #setGradientType(int) + * @see #getGradientRadius() */ public void setGradientRadius(float gradientRadius) { mGradientState.setGradientRadius(gradientRadius, TypedValue.COMPLEX_UNIT_PX); @@ -433,7 +507,8 @@ public class GradientDrawable extends Drawable { * 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. + * @return the radius of the gradient in pixels + * @see #setGradientRadius(float) */ public float getGradientRadius() { if (mGradientState.mGradient != RADIAL_GRADIENT) { @@ -445,17 +520,19 @@ public class GradientDrawable extends Drawable { } /** - *

Sets whether or not this drawable will honor its level - * property.

- *

Note: changing this property will affect all instances + * Sets whether or not this drawable will honor its {@code level} property. + *

+ * Note: changing this property will affect all instances * of a drawable loaded from a resource. It is recommended to invoke - * {@link #mutate()} before changing this property.

+ * {@link #mutate()} before changing this property. * - * @param useLevel True if this drawable should honor its level, false otherwise + * @param useLevel {@code true} if this drawable should honor its level, + * {@code false} otherwise * * @see #mutate() * @see #setLevel(int) * @see #getLevel() + * @see #isUseLevel() */ public void setUseLevel(boolean useLevel) { mGradientState.mUseLevel = useLevel; @@ -463,6 +540,18 @@ public class GradientDrawable extends Drawable { invalidateSelf(); } + /** + * Returns whether or not this drawable will honor its {@code level} + * property. + * + * @return {@code true} if this drawable should honor its level, + * {@code false} otherwise + * @see #setUseLevel(boolean) + */ + public boolean isUseLevel() { + return mGradientState.mUseLevel; + } + private int modulateAlpha(int alpha) { int scale = mAlpha + (mAlpha >> 7); return alpha * scale >> 8; @@ -470,20 +559,25 @@ public class GradientDrawable extends Drawable { /** * Returns the orientation of the gradient defined in this drawable. + * + * @return the orientation of the gradient defined in this drawable + * @see #setOrientation(Orientation) */ public Orientation getOrientation() { return mGradientState.mOrientation; } /** - *

Changes the orientation of the gradient defined in this drawable.

- *

Note: changing orientation will affect all instances + * Sets the orientation of the gradient defined in this drawable. + *

+ * Note: changing orientation will affect all instances * of a drawable loaded from a resource. It is recommended to invoke - * {@link #mutate()} before changing the orientation.

+ * {@link #mutate()} before changing the orientation. * - * @param orientation The desired orientation (angle) of the gradient + * @param orientation the desired orientation (angle) of the gradient * * @see #mutate() + * @see #getOrientation() */ public void setOrientation(Orientation orientation) { mGradientState.mOrientation = orientation; @@ -511,6 +605,18 @@ public class GradientDrawable extends Drawable { invalidateSelf(); } + /** + * Returns the colors used to draw the gradient, or {@code null} if the + * gradient is drawn using a single color or no colors. + * + * @return the colors used to draw the gradient, or {@code null} + * @see #setColors(int[] colors) + */ + @Nullable + public int[] getColors() { + return mGradientState.mGradientColors.clone(); + } + @Override public void draw(Canvas canvas) { if (!ensureValidRect()) { @@ -707,15 +813,17 @@ public class GradientDrawable extends Drawable { } /** - *

Changes this drawable to use a single color instead of a gradient.

- *

Note: changing color will affect all instances - * of a drawable loaded from a resource. It is recommended to invoke - * {@link #mutate()} before changing the color.

+ * Changes this drawable to use a single color instead of a gradient. + *

+ * Note: changing color will affect all instances of a + * drawable loaded from a resource. It is recommended to invoke + * {@link #mutate()} before changing the color. * * @param argb The color used to fill the shape * * @see #mutate() * @see #setColors(int[]) + * @see #getColor */ public void setColor(@ColorInt int argb) { mGradientState.setSolidColors(ColorStateList.valueOf(argb)); @@ -734,7 +842,9 @@ public class GradientDrawable extends Drawable { * {@link #mutate()} before changing the color.

* * @param colorStateList The color state list used to fill the shape + * * @see #mutate() + * @see #getColor */ public void setColor(ColorStateList colorStateList) { mGradientState.setSolidColors(colorStateList); @@ -749,6 +859,19 @@ public class GradientDrawable extends Drawable { invalidateSelf(); } + /** + * Returns the color state list used to fill the shape, or {@code null} if + * the shape is filled with a gradient or has no fill color. + * + * @return the color state list used to fill this gradient, or {@code null} + * + * @see #setColor(int) + * @see #setColor(ColorStateList) + */ + public ColorStateList getColor() { + return mGradientState.mSolidColors; + } + @Override protected boolean onStateChange(int[] stateSet) { boolean invalidateSelf = false; diff --git a/graphics/java/android/graphics/drawable/NinePatchDrawable.java b/graphics/java/android/graphics/drawable/NinePatchDrawable.java index 152fe6a97fddb..231405d4ba125 100644 --- a/graphics/java/android/graphics/drawable/NinePatchDrawable.java +++ b/graphics/java/android/graphics/drawable/NinePatchDrawable.java @@ -213,7 +213,12 @@ public class NinePatchDrawable extends Drawable { } } - private void setNinePatch(NinePatch ninePatch) { + /** + * Sets the nine patch used by this drawable. + * + * @param ninePatch the nine patch for this drawable + */ + public void setNinePatch(NinePatch ninePatch) { if (mNinePatch != ninePatch) { mNinePatch = ninePatch; if (ninePatch != null) { @@ -226,6 +231,13 @@ public class NinePatchDrawable extends Drawable { } } + /** + * @return the nine patch used by this drawable + */ + public NinePatch getNinePatch() { + return mNinePatch; + } + @Override public void draw(Canvas canvas) { final Rect bounds = getBounds();