android.graphics.drawable.
Drawable
是个虚类。
它的直接子类有
BitmapDrawable, ClipDrawable, ColorDrawable, DrawableContainer, GradientDrawable,
InsetDrawable, LayerDrawable, NinePatchDrawable, PictureDrawable, RotateDrawable,
ScaleDrawable, ShapeDrawable
间接子类有
AnimationDrawable, LevelListDrawable, PaintDrawable, StateListDrawable, TransitionDrawable
Class Overview
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:
- The
- 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
- and
- methods.这个用来设置Drawable要画好大
- The
- 这个接口用于返回padding,这个padding其实表示的是控件的content的padding
setState(int[])
setLevel(int)
Drawable.Callback
- interface. All clients should support this interface (via
setCallback(Drawable.Callback)
- ) so that animations will work. A simple way to do this is through the system facilities such as
setBackgroundDrawable(Drawable)
- and
ImageView
- .
Drawable定义了一个可画的对象一些接口。
它是抽象类。我们只能实例化它的实现类。Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable),我们根据画图的需求,创建相应的可画对象。
draw(Canvas canvas)来画Drawable,
一定要在之前通过
void setBounds(Rect bounds)
来设置其需要画到的区域。
注意2: 可通过setCallback(Drawable.Callback)来实现动画。
构造函数
直接子类
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 Drawable's 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
"
/>