可以简单地理解为 Bitmap 储存的是
像素信息
,Drawable 储存的是
对 Canvas 的一系列操作
。
1. Drawable 的注释:
也就是说 Drawable 只是一个抽象概念, 表示"something that can be drawn".
Drawable 的注释下面还有一段话:
2. 我们来看 Bitmap 类的定义:
细心的同学会发现, Bitmap 并没有实现 Drawable,那么他们俩是如何联系起来的呢? 答案是 BitmapDrawable.:
Drawable 类中有一个方法:
通过 BitmapDrawable 的构造函数,使得 Bitmap 可以转换为 Drawable.
同样, BitmapDrawable 的 getBitmap 方法也会返回 bitmap对象:
再看看其类定义:* 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.
public abstract class Drawable {
......
}
Drawable 的注释下面还有一段话:
那么形式就比较明朗了, Drawable 是一个抽象的概念, 而 Bitmap 是其存在的实体之一.Though usually not visible to the application, Drawables may take a variety of forms: 1. Bitmap: the simplest Drawable, a PNG or JPEG image. 2. Nine Patch: an extension to the PNG format allows it to specify information about how to stretch it and place things inside of it. 3. Shape: contains simple drawing commands instead of a raw bitmap, allowing it to resize better in some cases. 4. Layers: a compound drawable, which draws multiple underlying drawables on top of each other. 5. States: a compound drawable that selects one of a set of drawables based on its state. 6. Levels: a compound drawable that selects one of a set of drawables based on its level. 7. Scale : a compound drawable with a single child drawable, whose overall size is modified based on the current level.
2. 我们来看 Bitmap 类的定义:
public final class Bitmap implements Parcelable {
......
}
public class BitmapDrawable extends Drawable {
......
}
Drawable 类中有一个方法:
private static Drawable drawableFromBitmap(Resources res, Bitmap bm, byte[] np,
Rect pad, Rect layoutBounds, String srcName) {
if (np != null) {
return new NinePatchDrawable(res, bm, np, pad, layoutBounds, srcName);
}
return new BitmapDrawable(res, bm);
}
同样, BitmapDrawable 的 getBitmap 方法也会返回 bitmap对象:
/**
* Returns the bitmap used by this drawable to render. May be null.
*/
public final Bitmap getBitmap() {
return mBitmapState.mBitmap;
}