您的位置:首页 > 其它

Drawable简单使用

2010-08-20 17:28 267 查看
Android文档这样定义Drawable的:

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.

Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是
一个图层(LayerDrawable),我们根据画图的需求,创建相应的可画对象,就可以将这个可画对象当作一块“画布(Canvas)”,在其上面操作可画对象,并最终将这种可画对象显示在画布上,有点类似于“内存画布“。

下面的一个自定义View显示了ShapeDrawable的一些简单使用方式:

public class TestView extends View {
private ShapeDrawable mShapeDrawable;
public TestView(Context context) {
super(context);

int x = 10, y = 10;
int width = 300, height = 50;

mShapeDrawable = new ShapeDrawable(new OvalShape());
//ShapeDrawable.getPaint():Returns the Paint used to draw the shape
mShapeDrawable.getPaint().setColor(Color.YELLOW);
mShapeDrawable.setBounds(x, y, x+width, y+height);
}

@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);

canvas.drawColor(Color.WHITE);
mShapeDrawable.draw(canvas);
}
}


程序运行效果如下图:

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