public class ShapeDrawable extends Drawable
Shape
object and manages its
presence on the screen. If no Shape is given, then the ShapeDrawable will
default to a RectShape
.
This object can be defined in an XML file with the <shape>
element.
For more information about how to use ShapeDrawable, read the Canvas and Drawables document. For more information about defining a ShapeDrawable in XML, read the Drawable Resources document.
Modifier and Type | Class and Description |
---|---|
static class |
ShapeDrawable.ShaderFactory
Base class defines a factory object that is called each time the drawable
is resized (has a new width or height).
|
Drawable.Callback, Drawable.ConstantState
Constructor and Description |
---|
ShapeDrawable()
ShapeDrawable constructor.
|
ShapeDrawable(Shape s)
Creates a ShapeDrawable with a specified Shape.
|
Modifier and Type | Method and Description |
---|---|
void |
applyTheme(Resources.Theme t)
Applies the specified theme to this Drawable and its children.
|
void |
clearMutated()
Clears the mutated state, allowing this drawable to be cached and
mutated again.
|
void |
draw(Canvas canvas)
Draw in its bounds (set via setBounds) respecting optional effects such
as alpha (set via setAlpha) and color filter (set via setColorFilter).
|
int |
getAlpha()
Gets the current alpha value for the drawable. 0 means fully transparent,
255 means fully opaque.
|
int |
getChangingConfigurations()
Return a mask of the configuration parameters for which this drawable
may change, requiring that it be re-created.
|
Drawable.ConstantState |
getConstantState()
Return a
Drawable.ConstantState instance that holds the shared state of this Drawable. |
int |
getIntrinsicHeight()
Returns the drawable's intrinsic height.
|
int |
getIntrinsicWidth()
Returns the drawable's intrinsic width.
|
int |
getOpacity()
Return the opacity/transparency of this Drawable.
|
void |
getOutline(Outline outline)
Called to get the drawable to populate the Outline that defines its drawing area.
|
boolean |
getPadding(Rect padding)
Return in padding the insets suggested by this Drawable for placing
content inside the drawable's bounds.
|
Paint |
getPaint()
Returns the Paint used to draw the shape.
|
ShapeDrawable.ShaderFactory |
getShaderFactory()
Returns the ShaderFactory used by this ShapeDrawable for requesting a
Shader . |
Shape |
getShape()
Returns the Shape of this ShapeDrawable.
|
void |
inflate(Resources r,
XmlPullParser parser,
AttributeSet attrs,
Resources.Theme theme)
Inflate this Drawable from an XML resource optionally styled by a theme.
|
protected boolean |
inflateTag(String name,
Resources r,
XmlPullParser parser,
AttributeSet attrs)
Subclasses override this to parse custom subelements.
|
boolean |
isStateful()
Indicates whether this drawable will change its appearance based on
state.
|
Drawable |
mutate()
Make this drawable mutable.
|
protected void |
onBoundsChange(Rect bounds)
Override this in your subclass to change appearance if you vary based on
the bounds.
|
protected void |
onDraw(Shape shape,
Canvas canvas,
Paint paint)
Called from the drawable's draw() method after the canvas has been set to
draw the shape at (0,0).
|
protected boolean |
onStateChange(int[] stateSet)
Override this in your subclass to change appearance if you recognize the
specified state.
|
void |
setAlpha(int alpha)
Set the alpha level for this drawable [0..255].
|
void |
setColorFilter(ColorFilter colorFilter)
Specify an optional color filter for the drawable.
|
void |
setDither(boolean dither)
Set to true to have the drawable dither its colors when drawn to a
device with fewer than 8-bits per color component.
|
void |
setIntrinsicHeight(int height)
Sets the intrinsic (default) height for this shape.
|
void |
setIntrinsicWidth(int width)
Sets the intrinsic (default) width for this shape.
|
void |
setPadding(int left,
int top,
int right,
int bottom)
Sets padding for the shape.
|
void |
setPadding(Rect padding)
Sets padding for this shape, defined by a Rect object.
|
void |
setShaderFactory(ShapeDrawable.ShaderFactory fact)
Sets a ShaderFactory to which requests for a
Shader object will be made. |
void |
setShape(Shape s)
Sets the Shape of this ShapeDrawable.
|
void |
setTintList(ColorStateList tint)
Specifies tint color for this drawable as a color state list.
|
void |
setTintMode(PorterDuff.Mode tintMode)
Specifies a tint blending mode for this drawable.
|
canApplyTheme, clearColorFilter, copyBounds, copyBounds, createFromPath, createFromResourceStream, createFromResourceStream, createFromStream, createFromXml, createFromXml, createFromXmlInner, createFromXmlInner, getBounds, getCallback, getColorFilter, getCurrent, getDirtyBounds, getHotspotBounds, getLayoutDirection, getLevel, getMinimumHeight, getMinimumWidth, getOpticalInsets, getState, getTransparentRegion, inflate, invalidateSelf, isAutoMirrored, isFilterBitmap, isProjected, isVisible, jumpToCurrentState, obtainAttributes, onLayoutDirectionChanged, onLevelChange, parseTintMode, resolveOpacity, scheduleSelf, setAutoMirrored, setBounds, setBounds, setCallback, setChangingConfigurations, setColorFilter, setFilterBitmap, setHotspot, setHotspotBounds, setLayoutDirection, setLevel, setState, setTint, setVisible, setXfermode, unscheduleSelf
public ShapeDrawable()
public ShapeDrawable(Shape s)
s
- the Shape that this ShapeDrawable should bepublic Shape getShape()
public void setShape(Shape s)
public void setShaderFactory(ShapeDrawable.ShaderFactory fact)
Shader
object will be made.fact
- an instance of your ShaderFactory implementationpublic ShapeDrawable.ShaderFactory getShaderFactory()
Shader
.public Paint getPaint()
public void setPadding(int left, int top, int right, int bottom)
left
- padding for the left side (in pixels)top
- padding for the top (in pixels)right
- padding for the right side (in pixels)bottom
- padding for the bottom (in pixels)public void setPadding(Rect padding)
public void setIntrinsicWidth(int width)
width
- the intrinsic width (in pixels)public void setIntrinsicHeight(int height)
height
- the intrinsic height (in pixels)public int getIntrinsicWidth()
Drawable
Intrinsic width is the width at which the drawable would like to be laid out, including any inherent padding. If the drawable has no intrinsic width, such as a solid color, this method returns -1.
getIntrinsicWidth
in class Drawable
public int getIntrinsicHeight()
Drawable
Intrinsic height is the height at which the drawable would like to be laid out, including any inherent padding. If the drawable has no intrinsic height, such as a solid color, this method returns -1.
getIntrinsicHeight
in class Drawable
public boolean getPadding(Rect padding)
Drawable
getPadding
in class Drawable
protected void onDraw(Shape shape, Canvas canvas, Paint paint)
public void draw(Canvas canvas)
Drawable
public int getChangingConfigurations()
Drawable
Drawable.setChangingConfigurations(int)
or 0 by default. Subclasses
may extend this to or in the changing configurations of any other
drawables they hold.getChangingConfigurations
in class Drawable
ActivityInfo
.ActivityInfo
public void setAlpha(int alpha)
public int getAlpha()
Drawable
public void setTintList(ColorStateList tint)
Drawable
A Drawable's drawing content will be blended together with its tint
before it is drawn to the screen. This functions similarly to
Drawable.setColorFilter(int, PorterDuff.Mode)
.
Note: Setting a color filter via
Drawable.setColorFilter(ColorFilter)
or
Drawable.setColorFilter(int, PorterDuff.Mode)
overrides tint.
setTintList
in class Drawable
tint
- Color state list to use for tinting this drawable, or
null
to clear the tintDrawable.setTint(int)
,
Drawable.setTintMode(PorterDuff.Mode)
public void setTintMode(PorterDuff.Mode tintMode)
Drawable
Defines how this drawable's tint color should be blended into the drawable
before it is drawn to screen. Default tint mode is PorterDuff.Mode#SRC_IN
.
Note: Setting a color filter via
Drawable.setColorFilter(ColorFilter)
or
Drawable.setColorFilter(int, PorterDuff.Mode)
overrides tint.
setTintMode
in class Drawable
tintMode
- A Porter-Duff blending modeDrawable.setTint(int)
,
Drawable.setTintList(ColorStateList)
public void setColorFilter(ColorFilter colorFilter)
Drawable
If a Drawable has a ColorFilter, each output pixel of the Drawable's drawing contents will be modified by the color filter before it is blended onto the render target of a Canvas.
Pass null
to remove any existing color filter.
Note: Setting a non-null
color
filter disables tint
.
setColorFilter
in class Drawable
colorFilter
- The color filter to apply, or null
to remove the
existing color filterpublic int getOpacity()
Drawable
PixelFormat
:
PixelFormat.UNKNOWN
,
PixelFormat.TRANSLUCENT
,
PixelFormat.TRANSPARENT
, or
PixelFormat.OPAQUE
.
An OPAQUE drawable is one that draws all all content within its bounds, completely covering anything behind the drawable. A TRANSPARENT drawable is one that draws nothing within its bounds, allowing everything behind it to show through. A TRANSLUCENT drawable is a drawable in any other state, where the drawable will draw some, but not all, of the content within its bounds and at least some content behind the drawable will be visible. If the visibility of the drawable's contents cannot be determined, the safest/best return value is TRANSLUCENT.
Generally a Drawable should be as conservative as possible with the
value it returns. For example, if it contains multiple child drawables
and only shows one of them at a time, if only one of the children is
TRANSLUCENT and the others are OPAQUE then TRANSLUCENT should be
returned. You can use the method Drawable.resolveOpacity(int, int)
to perform a
standard reduction of two opacities to the appropriate single output.
Note that the returned value does not necessarily take into account a
custom alpha or color filter that has been applied by the client through
the Drawable.setAlpha(int)
or Drawable.setColorFilter(android.graphics.ColorFilter)
methods. Some subclasses,
such as BitmapDrawable
, ColorDrawable
, and GradientDrawable
,
do account for the value of Drawable.setAlpha(int)
, but the general behavior is dependent
upon the implementation of the subclass.
getOpacity
in class Drawable
PixelFormat
public void setDither(boolean dither)
Drawable
setDither
in class Drawable
Paint.setDither(boolean);
protected void onBoundsChange(Rect bounds)
Drawable
onBoundsChange
in class Drawable
protected boolean onStateChange(int[] stateSet)
Drawable
onStateChange
in class Drawable
public boolean isStateful()
Drawable
isStateful
in class Drawable
Drawable.setState(int[])
protected boolean inflateTag(String name, Resources r, XmlPullParser parser, AttributeSet attrs)
public void inflate(Resources r, XmlPullParser parser, AttributeSet attrs, Resources.Theme theme) throws XmlPullParserException, IOException
Drawable
inflate
in class Drawable
r
- Resources used to resolve attribute valuesparser
- XML parser from which to inflate this Drawableattrs
- Base set of attribute valuestheme
- Theme to apply, may be nullXmlPullParserException
IOException
public void applyTheme(Resources.Theme t)
Drawable
applyTheme
in class Drawable
t
- the theme to applypublic void getOutline(Outline outline)
Drawable
This method is called by the default ViewOutlineProvider
to define
the outline of the View.
The default behavior defines the outline to be the bounding rectangle of 0 alpha. Subclasses that wish to convey a different shape or alpha value must override this method.
getOutline
in class Drawable
View.setOutlineProvider(android.view.ViewOutlineProvider)
public Drawable.ConstantState getConstantState()
Drawable
Drawable.ConstantState
instance that holds the shared state of this Drawable.getConstantState
in class Drawable
Drawable.ConstantState
,
Drawable.mutate()
public Drawable mutate()
Drawable
mutate
in class Drawable
Drawable.ConstantState
,
Drawable.getConstantState()
public void clearMutated()
Drawable
This is hidden because only framework drawables can be cached, so custom drawables don't need to support constant state, mutate(), or clearMutated().
clearMutated
in class Drawable