Drawable

android.graphics.drawable. Drawable是个虚类。
它的直接子类有
BitmapDrawable,ClipDrawable,ColorDrawable,DrawableContainer,GradientDrawable,
InsetDrawable,LayerDrawable,NinePatchDrawable,PictureDrawable,RotateDrawable,
ScaleDrawable,ShapeDrawable
间接子类有
AnimationDrawable,LevelListDrawable,PaintDrawable,StateListDrawable,TransitionDrawable

Class Overview

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 aView, 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:

  • ThesetBounds(Rect)methodmustbe 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 thegetIntrinsicHeight()andgetIntrinsicWidth()methods.这个用来设置Drawable要画好大
  • ThegetPadding(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的padding
  • ThesetState(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.
  • ThesetLevel(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.
  • A Drawable can perform animations by calling back to its client through theDrawable.Callbackinterface. 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)andImageView.
Drawable定义了一个可画的对象一些接口。
它是抽象类。我们只能实例化它的实现类。Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable),我们根据画图的需求,创建相应的可画对象。
注意1:如果要手动调用 draw(Canvascanvas)来画Drawable,
一定要在之前通过
voidsetBounds(Rectbounds)
来设置其需要画到的区域。
注意2:可通过setCallback(Drawable.Callback)来实现动画。
构造函数
Public Constructors
<nobr style="line-height:21px"></nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html#Drawable()" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Drawable</a></span>()</nobr>
主要函数
Public Methods
<nobr style="line-height:21px">void</nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html#clearColorFilter()" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">clearColorFilter</a></span>()</nobr>
<nobr style="line-height:21px">final<a rel="nofollow" href="http://developer.android.com/reference/android/graphics/Rect.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Rect</a></nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html#copyBounds()" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">copyBounds</a></span>()</nobr>
Return a copy of the drawable's bounds in a new Rect.
<nobr style="line-height:21px">final void</nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html#copyBounds(android.graphics.Rect)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">copyBounds</a></span>(<a rel="nofollow" href="http://developer.android.com/reference/android/graphics/Rect.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Rect</a>bounds)</nobr>
Return a copy of the drawable's bounds in the specified Rect (allocated by the caller).
<nobr style="line-height:21px">static<a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Drawable</a></nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html#createFromPath(java.lang.String)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">createFromPath</a></span>(<a rel="nofollow" href="http://developer.android.com/reference/java/lang/String.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">String</a>pathName)</nobr>
Create a drawable from file path name.
<nobr style="line-height:21px">static<a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Drawable</a></nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html#createFromResourceStream(android.content.res.Resources,%20android.util.TypedValue,%20java.io.InputStream,%20java.lang.String,%20android.graphics.BitmapFactory.Options)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">createFromResourceStream</a></span>(<a rel="nofollow" href="http://developer.android.com/reference/android/content/res/Resources.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Resources</a>res,<a rel="nofollow" href="http://developer.android.com/reference/android/util/TypedValue.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">TypedValue</a>value,<a rel="nofollow" href="http://developer.android.com/reference/java/io/InputStream.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">InputStream</a>is,<a rel="nofollow" href="http://developer.android.com/reference/java/lang/String.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">String</a>srcName,<a rel="nofollow" href="http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">BitmapFactory.Options</a>opts)</nobr>
Create a drawable from an inputstream, using the given resources and value to determine density information.
<nobr style="line-height:21px">static<a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Drawable</a></nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html#createFromResourceStream(android.content.res.Resources,%20android.util.TypedValue,%20java.io.InputStream,%20java.lang.String)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">createFromResourceStream</a></span>(<a rel="nofollow" href="http://developer.android.com/reference/android/content/res/Resources.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Resources</a>res,<a rel="nofollow" href="http://developer.android.com/reference/android/util/TypedValue.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">TypedValue</a>value,<a rel="nofollow" href="http://developer.android.com/reference/java/io/InputStream.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">InputStream</a>is,<a rel="nofollow" href="http://developer.android.com/reference/java/lang/String.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">String</a>srcName)</nobr>
Create a drawable from an inputstream, using the given resources and value to determine density information.
<nobr style="line-height:21px">static<a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Drawable</a></nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html#createFromStream(java.io.InputStream,%20java.lang.String)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">createFromStream</a></span>(<a rel="nofollow" href="http://developer.android.com/reference/java/io/InputStream.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">InputStream</a>is,<a rel="nofollow" href="http://developer.android.com/reference/java/lang/String.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">String</a>srcName)</nobr>
Create a drawable from an inputstream
<nobr style="line-height:21px">static<a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Drawable</a></nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html#createFromXml(android.content.res.Resources,%20org.xmlpull.v1.XmlPullParser)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">createFromXml</a></span>(<a rel="nofollow" href="http://developer.android.com/reference/android/content/res/Resources.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Resources</a>r,<a rel="nofollow" href="http://developer.android.com/reference/org/xmlpull/v1/XmlPullParser.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">XmlPullParser</a>parser)</nobr>
Create a drawable from an XML document.
<nobr style="line-height:21px">static<a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Drawable</a></nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html#createFromXmlInner(android.content.res.Resources,%20org.xmlpull.v1.XmlPullParser,%20android.util.AttributeSet)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">createFromXmlInner</a></span>(<a rel="nofollow" href="http://developer.android.com/reference/android/content/res/Resources.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Resources</a>r,<a rel="nofollow" href="http://developer.android.com/reference/org/xmlpull/v1/XmlPullParser.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">XmlPullParser</a>parser,<a rel="nofollow" href="http://developer.android.com/reference/android/util/AttributeSet.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">AttributeSet</a>attrs)</nobr>
Create from inside an XML document.
<nobr style="line-height:21px">abstract void</nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html#draw(android.graphics.Canvas)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">draw</a></span>(<a rel="nofollow" href="http://developer.android.com/reference/android/graphics/Canvas.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Canvas</a>canvas)</nobr>
Draw in its bounds (set via setBounds) respecting optional effects such as alpha (set via setAlpha) and color filter (set via setColorFilter).
<nobr style="line-height:21px">final<a rel="nofollow" href="http://developer.android.com/reference/android/graphics/Rect.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Rect</a></nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html#getBounds()" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">getBounds</a></span>()</nobr>
Return the drawable's bounds Rect.
<nobr style="line-height:21px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.Callback.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Drawable.Callback</a></nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html#getCallback()" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">getCallback</a></span>()</nobr>
Return the current Drawable.Callbackimplementation attached to this Drawable.
<nobr style="line-height:21px">int</nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html#getChangingConfigurations()" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">getChangingConfigurations</a></span>()</nobr>
Return a mask of the configuration parameters for which this drawable may change, requiring that it be re-created.
<nobr style="line-height:21px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.ConstantState.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Drawable.ConstantState</a></nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html#getConstantState()" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">getConstantState</a></span>()</nobr>
Return a Drawable.ConstantStateinstance that holds the shared state of this Drawable.
<nobr style="line-height:21px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Drawable</a></nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html#getCurrent()" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">getCurrent</a></span>()</nobr>
<nobr style="line-height:21px">int</nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html#getIntrinsicHeight()" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">getIntrinsicHeight</a></span>()</nobr>
Return the intrinsic height of the underlying drawable object.
<nobr style="line-height:21px">int</nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html#getIntrinsicWidth()" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">getIntrinsicWidth</a></span>()</nobr>
Return the intrinsic width of the underlying drawable object.
<nobr style="line-height:21px">final int</nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html#getLevel()" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">getLevel</a></span>()</nobr>
Retrieve the current level.
<nobr style="line-height:21px">int</nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html#getMinimumHeight()" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">getMinimumHeight</a></span>()</nobr>
Returns the minimum height suggested by this Drawable.
<nobr style="line-height:21px">int</nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html#getMinimumWidth()" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">getMinimumWidth</a></span>()</nobr>
Returns the minimum width suggested by this Drawable.
<nobr style="line-height:21px">abstract int</nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html#getOpacity()" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">getOpacity</a></span>()</nobr>
Return the opacity/transparency of this Drawable.
<nobr style="line-height:21px">boolean</nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html#getPadding(android.graphics.Rect)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">getPadding</a></span>(<a rel="nofollow" href="http://developer.android.com/reference/android/graphics/Rect.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Rect</a>padding)</nobr>
Return in padding the insets suggested by this Drawable for placing content inside the drawable's bounds.
<nobr style="line-height:21px">int[]</nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html#getState()" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">getState</a></span>()</nobr>
Describes the current state, as a union of primitve states, such as state_focused, state_selected, etc.
<nobr style="line-height:21px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/Region.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Region</a></nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html#getTransparentRegion()" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">getTransparentRegion</a></span>()</nobr>
Returns a Region representing the part of the Drawable that is completely transparent.
<nobr style="line-height:21px">void</nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html#inflate(android.content.res.Resources,%20org.xmlpull.v1.XmlPullParser,%20android.util.AttributeSet)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">inflate</a></span>(<a rel="nofollow" href="http://developer.android.com/reference/android/content/res/Resources.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Resources</a>r,<a rel="nofollow" href="http://developer.android.com/reference/org/xmlpull/v1/XmlPullParser.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">XmlPullParser</a>parser,<a rel="nofollow" href="http://developer.android.com/reference/android/util/AttributeSet.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">AttributeSet</a>attrs)</nobr>
Inflate this Drawable from an XML resource.
<nobr style="line-height:21px">void</nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html#invalidateSelf()" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">invalidateSelf</a></span>()</nobr>
Use the current Drawable.Callbackimplementation to have this Drawable redrawn.
<nobr style="line-height:21px">boolean</nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html#isStateful()" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">isStateful</a></span>()</nobr>
Indicates whether this view will change its appearance based on state.
<nobr style="line-height:21px">final boolean</nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html#isVisible()" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">isVisible</a></span>()</nobr>
<nobr style="line-height:21px">void</nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html#jumpToCurrentState()" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">jumpToCurrentState</a></span>()</nobr>
If this Drawable does transition animations between states, ask that it immediately jump to the current state and skip any active animations.
<nobr style="line-height:21px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Drawable</a></nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html#mutate()" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">mutate</a></span>()</nobr>
Make this drawable mutable.
<nobr style="line-height:21px">static int</nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html#resolveOpacity(int,%20int)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">resolveOpacity</a></span>(int op1, int op2)</nobr>
Return the appropriate opacity value for two source opacities.
<nobr style="line-height:21px">void</nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html#scheduleSelf(java.lang.Runnable,%20long)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">scheduleSelf</a></span>(<a rel="nofollow" href="http://developer.android.com/reference/java/lang/Runnable.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Runnable</a>what, long when)</nobr>
Use the current Drawable.Callbackimplementation to have this Drawable scheduled.
<nobr style="line-height:21px">abstract void</nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html#setAlpha(int)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">setAlpha</a></span>(int alpha)</nobr>
Specify an alpha value for the drawable.
<nobr style="line-height:21px">void</nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html#setBounds(int,%20int,%20int,%20int)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">setBounds</a></span>(int left, int top, int right, int bottom)</nobr>
Specify a bounding rectangle for the Drawable.
<nobr style="line-height:21px">void</nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html#setBounds(android.graphics.Rect)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">setBounds</a></span>(<a rel="nofollow" href="http://developer.android.com/reference/android/graphics/Rect.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Rect</a>bounds)</nobr>
Specify a bounding rectangle for the Drawable.
<nobr style="line-height:21px">final void</nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html#setCallback(android.graphics.drawable.Drawable.Callback)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">setCallback</a></span>(<a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.Callback.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Drawable.Callback</a>cb)</nobr>
Bind a Drawable.Callbackobject to this Drawable.
<nobr style="line-height:21px">void</nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html#setChangingConfigurations(int)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">setChangingConfigurations</a></span>(int configs)</nobr>
Set a mask of the configuration parameters for which this drawable may change, requiring that it be re-created.
<nobr style="line-height:21px">abstract void</nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html#setColorFilter(android.graphics.ColorFilter)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">setColorFilter</a></span>(<a rel="nofollow" href="http://developer.android.com/reference/android/graphics/ColorFilter.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">ColorFilter</a>cf)</nobr>
Specify an optional colorFilter for the drawable.
<nobr style="line-height:21px">void</nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html#setColorFilter(int,%20android.graphics.PorterDuff.Mode)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">setColorFilter</a></span>(int color,<a rel="nofollow" href="http://developer.android.com/reference/android/graphics/PorterDuff.Mode.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">PorterDuff.Mode</a>mode)</nobr>
Specify a color and porterduff mode to be the colorfilter for this drawable.
<nobr style="line-height:21px">void</nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html#setDither(boolean)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">setDither</a></span>(boolean dither)</nobr>
Set to true to have the drawable dither its colors when drawn to a device with fewer than 8-bits per color component.
<nobr style="line-height:21px">void</nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html#setFilterBitmap(boolean)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">setFilterBitmap</a></span>(boolean filter)</nobr>
Set to true to have the drawable filter its bitmap when scaled or rotated (for drawables that use bitmaps).
<nobr style="line-height:21px">final boolean</nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html#setLevel(int)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">setLevel</a></span>(int level)</nobr>
Specify the level for the drawable.
<nobr style="line-height:21px">boolean</nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html#setState(int%5B%5D)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">setState</a></span>(int[] stateSet)</nobr>
Specify a set of states for the drawable.
<nobr style="line-height:21px">boolean</nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html#setVisible(boolean,%20boolean)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">setVisible</a></span>(boolean visible, boolean restart)</nobr>
Set whether this Drawable is visible.
<nobr style="line-height:21px">void</nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html#unscheduleSelf(java.lang.Runnable)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">unscheduleSelf</a></span>(<a rel="nofollow" href="http://developer.android.com/reference/java/lang/Runnable.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Runnable</a>what)</nobr>
Use the current Drawable.Callbackimplementation to have this Drawable unscheduled.
Protected Methods
<nobr style="line-height:21px">void</nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html#onBoundsChange(android.graphics.Rect)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">onBoundsChange</a></span>(<a rel="nofollow" href="http://developer.android.com/reference/android/graphics/Rect.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Rect</a>bounds)</nobr>
Override this in your subclass to change appearance if you recognize the specified state.
<nobr style="line-height:21px">boolean</nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html#onLevelChange(int)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">onLevelChange</a></span>(int level)</nobr>
Override this in your subclass to change appearance if you vary based on level.
<nobr style="line-height:21px">boolean</nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html#onStateChange(int%5B%5D)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">onStateChange</a></span>(int[] state)</nobr>
Override this in your subclass to change appearance if you recognize the specified state.
直接子类
BitmapDrawableADrawablethatwrapsabitmap.YoucancreateaBitmapDrawablefromafilepath,
aninputstream,throughXMLinflation,orfromaBitmapobject.
它的本质是个bitmap,它提供了针对bitmap的实现。
注意:Androidsupportsbitmapfilesinathreeformats: .png(preferred), .jpg(acceptable), .gif(discouraged).
也是说Android把.png,.jpg,.gif都是看为BitmapDrawable,Android应该是把这三种图片转为bitmap来处理。
其实任何图片绘画时最后都是转为bitmap来处理

BitmapDrawablebitmapDrawable=(BitmapDrawable)context.getResources().getDrawable(R.drawable.icon);
Bitmapbitmap=bitmapDrawable.getBitmap();

可以通过上面的形式把BitmapDrawable转化为Bitmap
ClipDrawableADrawablethatclipsanotherDrawablebasedonthisDrawable'scurrentlevelvalue.
具体参照《 ClipDrawable
ColorDrawableAspecializedDrawablethatfillstheCanvaswithaspecifiedcolor,withrespecttotheclipregion.
DrawableContainer
它应该是专门用来存放Drawable的。
如何直接使用它还不知道。它不能在XML文件中定义。
它的子类AnimationDrawable,LevelListDrawable,StateListDrawable
GradientDrawableADrawablewithacolorgradientforbuttons,backgrounds,etc.
ItcanbedefinedinanXMLfilewiththe<shape>element.Formoreinformation。
InsetDrawableADrawablethatinsetsanotherDrawablebyaspecifieddistance.
LayerDrawableADrawablethatmanagesanarrayofotherDrawables.
NinePatchDrawableAresizeablebitmap,withstretchableareasthatyoudefine.
它对应的是.9.png文件。关于此请参考《 个性缩放图片NinePatchDrawable
PictureDrawableDrawablesubclassthatwrapsaPicture,allowingthepicturetobeusedwhereeveraDrawableissupported.
RotateDrawable
ADrawablethatcanrotateanotherDrawablebasedonthecurrentlevelvalue.
ScaleDrawableADrawablethatchangesthesizeofanotherDrawablebasedonitscurrentlevelvalue.
ShapeDrawableADrawableobjectthatdrawsprimitiveshapes. 关于此请参考《 ShapeDrawable
ColorDrawable
AspecializedDrawablethatfillstheCanvaswithaspecifiedcolor,
withrespecttotheclipregion.NotethataColorDrawableignorestheColorFilter.
ItalsoignorestheBounds,meaningitwilldraweverywhereinthecurrentclip,
evenifsetBounds(...)wascalledwithasmallerarea.
ItcanbedefinedinanXMLfilewiththe<color>element.
注意1:它会忽略掉ColorFilter和Bounds,它会把整个clip区域都填充成指定的颜色。
在XML中定义和引用ColorDrawable
AcolorresourcecanalsobeusedasadrawableinXML.Forexample,
whencreatingastatelistdrawable,youcanreferenceacolorresourcefortheandroid:drawableattribute
(android:drawable="@color/green
在values下的 Colors.xml文件:
<?xmlversion="1.0"encoding="UTF-8"?>
<resources>
<
color name="red">#FFFF0000</color>
</resources>

像引用Drawable一样引用它
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:background="
@color/red "
/>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值