零碎的记录一些话以便理解。
* 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 {@link android.view.View}, a Drawable does not have any facility to
* receive events or otherwise interact with the user.
Drawable 只是一个抽象概念, 表示"something that can be drawn".
Though usually not visible to the application, Drawables may take a variety
* of forms:
*
* <ul>
* <li> <b>Bitmap</b>: the simplest Drawable, a PNG or JPEG image.
* <li> <b>Nine Patch</b>: an extension to the PNG format allows it to
* specify information about how to stretch it and place things inside of
* it.
* <li><b>Vector</b>: a drawable defined in an XML file as a set of points,
* lines, and curves along with its associated color information. This type
* of drawable can be scaled without loss of display quality.
* <li> <b>Shape</b>: contains simple drawing commands instead of a raw
* bitmap, allowing it to resize better in some cases.
* <li> <b>Layers</b>: a compound drawable, which draws multiple underlying
* drawables on top of each other.
* <li> <b>States</b>: a compound drawable that selects one of a set of
* drawables based on its state.
* <li> <b>Levels</b>: a compound drawable that selects one of a set of
* drawables based on its level.
* <li> <b>Scale</b>: a compound drawable with a single child drawable,
* whose overall size is modified based on the current level.
* </ul>
Drawable 是一个抽象的存在,是一个可画的对象。可能是一个Bitmap/Nine Patch/Vector/Shape/Layer/States/Levels/Scales Drawable。(我)
Drawable 是一个抽象的概念, 而 Bitmap 是其存在的实体之一.
“PNG和JPEG都是图片格式,Bitmap是Java中的一个类, 类本身也是一个抽象的概念, Bitmap中有一个属性 byte[] mBuffer; 这个存储的是图片的二进制信息,PNG 和 JPEG本身也是由二进制数据构成的.”——高爷
“可以简单地理解为 Bitmap 储存的是 像素信息,Drawable 储存的是 对 Canvas 的一系列操作。而 BitmapDrawable 储存的是「把 Bitmap 渲染到 Canvas 上」这个操作。”——nekocode
在实际开发中,Drawable常被用作View的背景或者作为ImageView中的图像显示。Drawable一般都是通过XML来定义的,也可以通过代码构造。
android系统不允许直接修改原图,类似Photoshop中的锁定,必须通过原图创建一个同样大小的Bitmap,并将原图绘制到该Bitmap中,以一个副本的形式来修改图像。代码如下,bm为原图,bmp为创建的副本。
Bitmap bmp = Bitmap.createBitmap(bm.getWidth(),bm.getHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);
Paint paint = new Paint();
canvas.drawBitmap(bm,0,0,paint);
每个Drawable都会有一个draw的方法
参考:
网友评论