您的位置:首页 > 移动开发 > Android开发

android绘制图形:android.graphics.drawable.Drawable

2015-01-23 10:16 295 查看
android.graphics.drawable
类 Drawable
java.lang.Object
android.graphics.drawable.Drawable
直接已知子类:
BitmapDrawable,ClipDrawable,ColorDrawable,DrawableContainer,GradientDrawable,InsetDrawable,LayerDrawable,NinePatchDrawable,PictureDrawable,RotateDrawable,ScaleDrawable,ShapeDrawable

public abstract class
Drawable
extends
Object
A Drawable is a general abstraction for "something that can bedrawn." Most often you will deal with Drawable as the type of resourceretrieved for drawing things to the screen; the Drawable class provides ageneric API for dealing with an underlying
visual resource that may take avariety of forms. Unlike a
View, a Drawable does not have any facility toreceive events or otherwise interact with the user.
Drawable是对“可绘制对象”的抽象。通常需要对不同类型的可绘制对象进行不同的绘制操作,Drawable为不同的绘制对象提供了一组通用的API接口。和View不同,Drawable不具备接收处理事件和与用户交互的功能。
In addition to simple drawing, Drawable provides a number of genericmechanisms for its client to interact with what is being drawn:
除了简单的绘制操作,Drawable为用户提供一些初始化Drawable对象的接口:

The setBounds(int, int, int, int) 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.

setBounds(int, int, int, int)是初始化必须使用的接口,用来设置Drawable对象的位置和大小,所有的Drawable对象都需要有大小,用户可以使用getIntrinsicHeight()和getIntrinsicWidth()来获取【首选】值。

The getPadding(android.graphics.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.

getPadding(android.graphics.Rect)返回Drawable对象内部图像摆放位置的一些数据,例如,一个表示按钮控件的Drawable对象需要返回规定其内部文字标签位置的数据。

The setState(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.

setState(int[])让用户选择在什么状态下让Drawable对象完成绘制操作,例如:“获取焦点”或者“被选中”的状态。一些Drawable对象需要在不同状态下展示出不同的形态。

The 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.

setLevel(int)为Drawable的状态提供一个连续的控制,例如电池电量和长操作进度,Drawable对象需要根据level值展示相对应的形态。

A Drawable can perform animations by calling back to its client through the
Drawable.Callback interface. All clients should support this interface (via
setCallback(android.graphics.drawable.Drawable.Callback)) so that animations will work. A simple way to do this is through the system facilities such asView.setBackgroundDrawable(Drawable) and
ImageView.

Drawable对象可以通过回调方法实现动画,所有的用户都应该调用setCallback(Drawable.Callback)接口提供回调方法,实现动画展示。一个简单的方式是通过使用系统提供的装置如View.setBackgroundDrawable(Drawable)和ImageView。
Though usually not visible to the application, Drawablesmay take a variety of forms:

Bitmap: the simplest Drawable, a PNG or JPEG image.
Nine Patch: an extension to the PNG format allows it to specify information about how to stretch it and place things inside of it.
Shape: contains simple drawing commands instead of a raw bitmap, allowing it to resize better in some cases.
Layers: a compound drawable, which draws multiple underlying drawables on top of each other.
States: a compound drawable that selects one of a set of drawables based on its state.
Levels: a compound drawable that selects one of a set of drawables based on its level.
Scale: a compound drawable with a single child drawable, whose overall size is modified based on the current level.

嵌套类摘要
static interface
Drawable.Callback
Implement this interface if you want to create an animated drawable that extendsDrawable.
在需要实现动画时,实现此接口。
static class
Drawable.ConstantState
构造方法详细信息
Drawable
public Drawable()
方法详细信息
draw
public abstract void draw(Canvas canvas)
Draw in its bounds(set via setBounds) respecting optional effects such as alpha (set viasetAlpha) and color filter (set via setColorFilter).
参数:
canvas - Thecanvas to draw into

setBounds:长方形区域边线
public void setBounds(int left,int top,int right,int bottom)
Specify a boundingrectangle for the Drawable. This is where the drawable will draw when itsdraw() method is called.

public voidsetBounds(Rect bounds)
Specify a boundingrectangle for the Drawable. This is where the drawable will draw when itsdraw() method is called.

publicfinal voidcopyBounds(Rect bounds)
Return a copy ofthe drawable's bounds in the specified Rect (allocated by the caller). Thebounds specify where this will draw when its draw() method is called.
参数:
bounds - Rect toreceive the drawable's bounds (allocated by the caller).

public final
Rect
copyBounds()
Return a copy ofthe drawable's bounds in a new Rect. This returns the same values asgetBounds(), but the returned object is guaranteed to not be changed later bythe drawable (i.e. it retains no reference to this rect). If the caller alreadyhas
a Rect allocated, call copyBounds(rect)
返回:
A copy of thedrawable's bounds

public final
Rect
getBounds()
Return thedrawable's bounds Rect. Note: for efficiency, the returned object may be thesame object stored in the drawable (though this is not guaranteed), so if apersistent copy of the bounds is needed, call copyBounds(rect) instead.
返回:
The bounds of thedrawable (which may change later, so caller beware).

public voidsetChangingConfigurations(int configs)
Set a mask of theconfiguration parameters for which this drawable may change, requiring that itbe re-created.
参数:
configs - A maskof the changing configuration parameters, as defined byConfiguration.

另请参见:
Configuration

public intgetChangingConfigurations()
Return a mask ofthe configuration parameters for which this drawable mau change, requiring thatit be re-created. The default implementation returns whatever was providedthroughsetChangingConfigurations(int)or
0 by default. Subclasses may extend this to or in the changingconfigurations of any other drawables they hold.
返回:
Returns a mask ofthe changing configuration parameters, as defined byConfiguration.

另请参见:
Configuration

setDither:抖动
public void setDither(boolean dither)
Set to true tohave the drawable dither its colors when drawn to a device with fewer than8-bits per color component. This can improve the look on those devices, but canalso slow down the drawing a little.

public voidsetFilterBitmap(boolean filter)
Set to true tohave the drawable filter its bitmap when scaled or rotated (for drawables thatuse bitmaps). If the drawable does not use bitmaps, this call is ignored. Thiscan improve the look when scaled or rotated, but also slows down the drawing.

public final voidsetCallback(Drawable.Callback cb)
Bind a
Drawable.Callback object to thisDrawable. Required for clients that want to support animated drawables.
参数:
cb - The client'sCallback implementation.

public void
invalidateSelf()
Use the current
Drawable.Callback implementation tohave this Drawable redrawn. Does nothing if there is no Callback attached tothe Drawable.
另请参见:
Drawable.Callback.invalidateDrawable(android.graphics.drawable.Drawable)

public voidscheduleSelf(Runnable what,
long when)
Use the current
Drawable.Callback implementation tohave this Drawable scheduled. Does nothing if there is no Callback attached tothe Drawable.
参数:
what - The actionbeing scheduled.
when - The time(in milliseconds) to run.
另请参见:
Drawable.Callback.scheduleDrawable(android.graphics.drawable.Drawable,java.lang.Runnable, long)

public voidunscheduleSelf(Runnable what)
Use the current
Drawable.Callback implementation tohave this Drawable unscheduled. Does nothing if there is no Callback attachedto the Drawable.
参数:
what - Therunnable that you no longer want called.
另请参见:
Drawable.Callback.unscheduleDrawable(android.graphics.drawable.Drawable,java.lang.Runnable)

public abstract void
setAlpha(int alpha)
Specify an alphavalue for the drawable. 0 means fully transparent, and 255 means fully opaque.

public abstract voidsetColorFilter(ColorFilter cf)
Specify anoptional colorFilter for the drawable. Pass null to remove any filters.

public voidsetColorFilter(int color,
PorterDuff.Mode mode)
Specify a colorand porterduff mode to be the colorfilter for this drawable.

public void
clearColorFilter()

public boolean
isStateful()
Indicates whetherthis view will change its appearance based on state. Clients can use this todetermine whether it is necessary to calculate their state and call setState.
返回:
True if this viewchanges its appearance based on state, false otherwise.
另请参见:
setState(int[])

public boolean
setState(int[] stateSet)
Specify a set ofstates for the drawable. These are use-case specific, so see the relevantdocumentation. As an example, the background for widgets like Button understandthe following states: [android.R.attr#state_focused, android.R.attr#state_pressed].
If the new state you are supplying causes the appearanceof the Drawable to change, then it is responsible for callinginvalidateSelf()in order to have itself redrawn,
and true will be returned from thisfunction.
Note: The Drawable holds a reference on to stateSetuntil a new state array is given to it, so you must not modify this arrayduring that time.
参数:
stateSet - The newset of states to be displayed.
返回:
Returns true ifthis change in state has caused the appearance of the Drawable to change (hencerequiring an invalidate), otherwise returns false.

public int[]
getState()
Describes thecurrent state, as a union of primitve states, such as android.R.attr#state_focused,android.R.attr#state_selected, etc. Some drawables may modify their imagerybased on the selected state.
返回:
An array ofresource Ids describing the current state.

public

Drawable getCurrent()
返回:
The currentdrawable that will be used by this drawable. For simple drawables, this is justthe drawable itself. For drawables that change state likeStateListDrawable
andLevelListDrawable this will be thechild drawable currently in use.

public final boolean
setLevel(int level)
Specify the levelfor the drawable. This allows a drawable to vary its imagery based on acontinuous controller, for example to show progress or volume level.
If the new level you are supplying causes the appearanceof the Drawable to change, then it is responsible for callinginvalidateSelf()in order to have itself redrawn,
and true will be returned from thisfunction.
参数:
level - The newlevel, from 0 (minimum) to 10000 (maximum).
返回:
Returns true ifthis change in level has caused the appearance of the Drawable to change (hencerequiring an invalidate), otherwise returns false.

public final int
getLevel()
Retrieve thecurrent level.
返回:
int Current level,from 0 (minimum) to 10000 (maximum).

public booleansetVisible(boolean visible,
boolean restart)
Set whether thisDrawable is visible. This generally does not impact the Drawable's behavior,but is a hint that can be used by some Drawables, for example, to decidewhether run animations.
参数:
visible - Set totrue if visible, false if not.
restart - You cansupply true here to force the drawable to behave as if it has just becomevisible, even if it had last been set visible. Used for example to forceanimations to restart.
返回:
boolean Returnstrue if the new visibility is different than its previous state.

public final boolean
isVisible()

getOpacity:不透明度
public abstract int getOpacity()
Return theopacity/transparency of this Drawable. The returned value is one of theabstract format constants inPixelFormat:
PixelFormat.UNKNOWN,PixelFormat.TRANSLUCENT,PixelFormat.TRANSPARENT,orPixelFormat.OPAQUE.
Generally a Drawable should be as conservative aspossible with the value it returns. For example, if it contains multiple childdrawables and only shows one of them at a time, if only one of the children isTRANSLUCENT and the others are OPAQUE
then TRANSLUCENT should be returned. Youcan use the method
resolveOpacity(int,int) to perform a standard reduction of two opacities to the appropriatesingle output.
Note that the returned value does not take intoaccount a custom alpha or color filter that has been applied by the clientthrough thesetAlpha(int)or
setColorFilter(android.graphics.ColorFilter)methods.
返回:
int The opacityclass of the Drawable.
另请参见:
PixelFormat

public static intresolveOpacity(int op1,
int op2)
Return theappropriate opacity value for two source opacities. If either is UNKNOWN, thatis returned; else, if either is TRANSLUCENT, that is returned; else, if eitheris TRANSPARENT, that is returned; else, OPAQUE is returned.
This is to help in implementing
getOpacity().
参数:
op1 - One opacityvalue.
op2 - Anotheropacity value.
返回:
int The combinedopacity value.
另请参见:
getOpacity()

public
Region
getTransparentRegion()
Returns a Regionrepresenting the part of the Drawable that is completely transparent. This canbe used to perform drawing operations, identifying which parts of the targetwill not change when rendering the Drawable. The default implementation
returnsnull, indicating no transparent region; subclasses can optionally override thisto return an actual Region if they want to supply this optimizationinformation, but it is not required that they do so.
返回:
Returns null ifthe Drawables has no transparent region to report, else a Region holding theparts of the Drawable's bounds that are transparent.

getIntrinsicWidth:内在宽度
public int getIntrinsicWidth()
Return theintrinsic width of the underlying drawable object. Returns -1 if it has nointrinsic width, such as with a solid color.

getIntrinsicHeight:内在高度
public int getIntrinsicHeight()
Return theintrinsic height of the underlying drawable object. Returns -1 if it has nointrinsic height, such as with a solid color.

getMinimumWidth:最小宽度
public int getMinimumWidth()
Returns theminimum width suggested by this Drawable. If a View uses this Drawable as abackground, it is suggested that the View use at least this value for itswidth. (There will be some scenarios where this will not be possible.) Thisvalue should
INCLUDE any padding.
返回:
The minimum widthsuggested by this Drawable. If this Drawable doesn't have a suggested minimumwidth, 0 is returned.

getMinimumHeight:最小高度
public int getMinimumHeight()
Returns theminimum height suggested by this Drawable. If a View uses this Drawable as abackground, it is suggested that the View use at least this value for itsheight. (There will be some scenarios where this will not be possible.) Thisvalue
should INCLUDE any padding.
返回:
The minimum heightsuggested by this Drawable. If this Drawable doesn't have a suggested minimumheight, 0 is returned.

getPadding:与边界距离
public boolean getPadding(Rect padding)
Return in paddingthe insets suggested by this Drawable for placing content inside the drawable'sbounds. Positive values move toward the center of the Drawable (setRect.inset). Returns true if this drawable actually has a padding, else false.When
false is returned, the padding is always set to 0.

mutate:变形
public
Drawable mutate()
Make this drawablemutable. This operation cannot be reversed. A mutable drawable is guaranteed tonot share its state with any other drawable. This is especially useful when youneed to modify properties of drawables loaded from resources. By
default, alldrawables instances loaded from the same resource share a common state; if youmodify the state of one instance, all the other instances will receive the samemodification. Calling this method on a mutable Drawable will have no effect.
返回:
This drawable.

createFromStream
public static
Drawable createFromStream(InputStream is,

String srcName)
Create a drawablefrom an inputstream

createFromResourceStream
public static
Drawable createFromResourceStream(Resources res,
TypedValue value,
InputStream is,
String srcName)
Create a drawablefrom an inputstream

createFromXml
public static
Drawable createFromXml(Resources r,
XmlPullParser parser)
throws
XmlPullParserException,

IOException
Create a drawablefrom an XML document. For more information on how to create resources in XML,seeResourcesand Internationalization.

抛出:
XmlPullParserException
IOException

createFromXmlInner
public static
Drawable createFromXmlInner(Resources r,
XmlPullParser parser,

AttributeSet attrs)
throws
XmlPullParserException,

IOException
Create from insidean XML document. Called on a parser positioned at a tag in an XML document,tries to create a Drawable from that tag. Returns null if the tag is not avalid drawable.
抛出:
XmlPullParserException
IOException

createFromPath
public static
Drawable createFromPath(String pathName)
Create a drawablefrom file path name.

inflate
public void inflate(Resources r,
XmlPullParser parser,
AttributeSet attrs)
throwsXmlPullParserException,
IOException
抛出:
XmlPullParserException
IOException

getConstantState
public
Drawable.ConstantState getConstantState()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: