A Drawable is a general abstraction for "something that can be drawn." Most often you will deal with Drawable as the type of resource retrieved for drawing things to the screen; the Drawable class provides a generic API for dealing with an underlying visual resource that may take a variety of forms. Unlike a View
, a Drawable does not have any facility to receive events or otherwise interact with the user.
In addition to simple drawing, Drawable provides a number of generic mechanisms for its client to interact with what is being drawn:
setBounds(Rect)
method must be called to tell the Drawable where it is drawn and how large it should be. All Drawables should respect the requested size, often simply by scaling their imagery. A client can find the preferred size for some Drawables with the getIntrinsicHeight()
and getIntrinsicWidth()
methods.这个用来设置Drawable要画好大getPadding(Rect)
method can return from some Drawables information about how to frame content that is placed inside of them. For example, a Drawable that is intended to be the frame for a button widget would need to return padding that correctly places the label inside of itself.这个接口用于返回padding,这个padding其实表示的是控件的content的paddingsetState(int[])
method allows the client to tell the Drawable in which state it is to be drawn, such as "focused", "selected", etc. Some drawables may modify their imagery based on the selected state.setLevel(int)
method allows the client to supply a single continuous controller that can modify the Drawable is displayed, such as a battery level or progress level. Some drawables may modify their imagery based on the current level.Drawable.Callback
interface. All clients should support this interface (viasetCallback(Drawable.Callback)
) so that animations will work. A simple way to do this is through the system facilities such assetBackgroundDrawable(Drawable)
and ImageView
.Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Drawable() |
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
void | clearColorFilter() | ||||||||||
final Rect | copyBounds()
Return a copy of the drawable's bounds in a new Rect.
|
||||||||||
final void | copyBounds( Rect bounds)
Return a copy of the drawable's bounds in the specified Rect (allocated by the caller).
|
||||||||||
static Drawable | createFromPath( String pathName)
Create a drawable from file path name.
|
||||||||||
static Drawable | createFromResourceStream( Resources res, TypedValue value, InputStream is, String srcName, BitmapFactory.Options opts)
Create a drawable from an inputstream, using the given resources and value to determine density information.
|
||||||||||
static Drawable | createFromResourceStream( Resources res, TypedValue value, InputStream is, String srcName)
Create a drawable from an inputstream, using the given resources and value to determine density information.
|
||||||||||
static Drawable | createFromStream( InputStream is, String srcName)
Create a drawable from an inputstream
|
||||||||||
static Drawable | createFromXml( Resources r, XmlPullParser parser)
Create a drawable from an XML document.
|
||||||||||
static Drawable | createFromXmlInner( Resources r, XmlPullParser parser, AttributeSet attrs)
Create from inside an XML document.
|
||||||||||
abstract 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).
|
||||||||||
final Rect | getBounds()
Return the drawable's bounds Rect.
|
||||||||||
Drawable.Callback | getCallback()
Return the current
Drawable.Callback implementation attached to this Drawable.
|
||||||||||
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.
|
||||||||||
Drawable | getCurrent() | ||||||||||
int | getIntrinsicHeight()
Return the intrinsic height of the underlying drawable object.
|
||||||||||
int | getIntrinsicWidth()
Return the intrinsic width of the underlying drawable object.
|
||||||||||
final int | getLevel()
Retrieve the current level.
|
||||||||||
int | getMinimumHeight()
Returns the minimum height suggested by this Drawable.
|
||||||||||
int | getMinimumWidth()
Returns the minimum width suggested by this Drawable.
|
||||||||||
abstract int | getOpacity()
Return the opacity/transparency of this Drawable.
|
||||||||||
boolean | getPadding( Rect padding)
Return in padding the insets suggested by this Drawable for placing content inside the drawable's bounds.
|
||||||||||
int[] | getState()
Describes the current state, as a union of primitve states, such as
state_focused ,
state_selected , etc.
|
||||||||||
Region | getTransparentRegion()
Returns a Region representing the part of the Drawable that is completely transparent.
|
||||||||||
void | inflate( Resources r, XmlPullParser parser, AttributeSet attrs)
Inflate this Drawable from an XML resource.
|
||||||||||
void | invalidateSelf()
Use the current
Drawable.Callback implementation to have this Drawable redrawn.
|
||||||||||
boolean | isStateful()
Indicates whether this view will change its appearance based on state.
|
||||||||||
final boolean | isVisible() | ||||||||||
void | jumpToCurrentState()
If this Drawable does transition animations between states, ask that it immediately jump to the current state and skip any active animations.
|
||||||||||
Drawable | mutate()
Make this drawable mutable.
|
||||||||||
static int | resolveOpacity(int op1, int op2)
Return the appropriate opacity value for two source opacities.
|
||||||||||
void | scheduleSelf( Runnable what, long when)
Use the current
Drawable.Callback implementation to have this Drawable scheduled.
|
||||||||||
abstract void | setAlpha(int alpha)
Specify an alpha value for the drawable.
|
||||||||||
void | setBounds(int left, int top, int right, int bottom)
Specify a bounding rectangle for the Drawable.
|
||||||||||
void | setBounds( Rect bounds)
Specify a bounding rectangle for the Drawable.
|
||||||||||
final void | setCallback( Drawable.Callback cb)
Bind a
Drawable.Callback object to this Drawable.
|
||||||||||
void | setChangingConfigurations(int configs)
Set a mask of the configuration parameters for which this drawable may change, requiring that it be re-created.
|
||||||||||
abstract void | setColorFilter( ColorFilter cf)
Specify an optional colorFilter for the drawable.
|
||||||||||
void | setColorFilter(int color, PorterDuff.Mode mode)
Specify a color and porterduff mode to be the colorfilter for this 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 | setFilterBitmap(boolean filter)
Set to true to have the drawable filter its bitmap when scaled or rotated (for drawables that use bitmaps).
|
||||||||||
final boolean | setLevel(int level)
Specify the level for the drawable.
|
||||||||||
boolean | setState(int[] stateSet)
Specify a set of states for the drawable.
|
||||||||||
boolean | setVisible(boolean visible, boolean restart)
Set whether this Drawable is visible.
|
||||||||||
void | unscheduleSelf( Runnable what)
Use the current
Drawable.Callback implementation to have this Drawable unscheduled.
|
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
void | onBoundsChange( Rect bounds)
Override this in your subclass to change appearance if you recognize the specified state.
|
||||||||||
boolean | onLevelChange(int level)
Override this in your subclass to change appearance if you vary based on level.
|
||||||||||
boolean | onStateChange(int[] state)
Override this in your subclass to change appearance if you recognize the specified state.
|