zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

android drawable

Android Drawable
2023-09-14 08:58:19 时间
span >它的直接子类有
BitmapDrawable, ClipDrawable, ColorDrawable, DrawableContainer, GradientDrawable, 
InsetDrawable, LayerDrawable, NinePatchDrawable, PictureDrawable, RotateDrawable, 
ScaleDrawable, ShapeDrawable
间接子类有
AnimationDrawable, LevelListDrawable, PaintDrawable, StateListDrawable, TransitionDrawable


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:


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).
getChangingConfigurations()Return a mask of the configuration parameters for which this drawable may change, requiring that it be re-created.
getTransparentRegion()Returns a Region representing the part of the Drawable that is completely transparent.
jumpToCurrentState()If this Drawable does transition animations between states, ask that it immediately jump to the current state and skip any active animations.
setBounds(int left, int top, int right, int bottom)Specify a bounding rectangle for the Drawable.
setChangingConfigurations(int configs)Set a mask of the configuration parameters for which this drawable may change, requiring that it be re-created.
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.
setFilterBitmap(boolean filter)Set to true to have the drawable filter its bitmap when scaled or rotated (for drawables that use bitmaps).
onLevelChange(int level)Override this in your subclass to change appearance if you vary based on level.
onStateChange(int[] state)Override this in your subclass to change appearance if you recognize the specified state.
直接子类
BitmapDrawable     A Drawable that wraps a bitmap. You can create a BitmapDrawable from a file path, 
                                an input stream, through XML inflation, or from a Bitmap object. 
                            它的本质是个bitmap,它提供了针对bitmap的实现。
注意:Android supports bitmap files in a three formats: .png (preferred), .jpg (acceptable), .gif (discouraged).
也是说Android把.png,.jpg,.gif都是看为BitmapDrawable,Android应该是把这三种图片转为bitmap来处理。
其实任何图片绘画时最后都是转为bitmap来处理
        BitmapDrawable bitmapDrawable=(BitmapDrawable)context.getResources().getDrawable(R.drawable.icon);
        Bitmap bitmap=bitmapDrawable.getBitmap();
可以通过上面的形式把BitmapDrawable转化为Bitmap
ClipDrawable     A Drawable that clips another Drawable based on this Drawables current level value. 
具体参照《ClipDrawable
ColorDrawable     A specialized Drawable that fills the Canvas with a specified color, with respect to the clip region. 
DrawableContainer
    它应该是专门用来存放Drawable的。
    如何直接使用它还不知道。它不能在XML文件中定义。
    它的子类AnimationDrawable, LevelListDrawable, StateListDrawable        
GradientDrawable     A Drawable with a color gradient for buttons, backgrounds, etc.
    It can be defined in an XML file with the  shape  element. For more information。 
InsetDrawable     A Drawable that insets another Drawable by a specified distance. 
LayerDrawable     A Drawable that manages an array of other Drawables. 
NinePatchDrawable     A resizeable bitmap, with stretchable areas that you define. 
它对应的是.9.png文件。关于此请参考《个性缩放图片NinePatchDrawable》 PictureDrawable     Drawable subclass that wraps a Picture, allowing the picture to be used whereever a Drawable is supported. 
RotateDrawable     
A Drawable that can rotate another Drawable based on the current level value. 
ScaleDrawable     A Drawable that changes the size of another Drawable based on its current level value. 
ShapeDrawable     A Drawable object that draws primitive shapes. 关于此请参考《ShapeDrawable》 ColorDrawable
    A specialized Drawable that fills the Canvas with a specified color, 
    with respect to the clip region. Note that a ColorDrawable ignores the ColorFilter. 
    It also ignores the Bounds, meaning it will draw everywhere in the current clip, 
    even if setBounds(...) was called with a smaller area.
    It can be defined in an XML file with the  color  element.
注意1:它会忽略掉ColorFilter和Bounds,它会把整个clip区域都填充成指定的颜色。
在XML中定义和引用ColorDrawable
A color resource can also be used as a drawable in XML. For example,
when creating a state list drawable, you can reference a color resource for the android:drawable attribute 
(android:drawable="@color/green
在values下的Colors.xml文件:
?xml version="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"
    /
你都知道么?Android中21种drawable标签大全(下) 我们在drawable目录下可以创建很多自定义的资源,其中用的最多的应该就是selector和shape。目前在Android中有21种drawable标签,了解和利用这些标签对我们的开发有很大的帮助。这个文章我们对这21种标签做一个介绍,让大家有一个印象。 本文中有些资料取自网上,当时记录在笔记中,但是由于时间久远,忘记出处了。
你都知道么?Android中21种drawable标签大全(上) 我们在drawable目录下可以创建很多自定义的资源,其中用的最多的应该就是selector和shape。目前在Android中有21种drawable标签,了解和利用这些标签对我们的开发有很大的帮助。这个文章我们对这21种标签做一个介绍,让大家有一个印象。 本文中有些资料取自网上,当时记录在笔记中,但是由于时间久远,忘记出处了。