Bitmap和Drawable的用法异同?


      可以简单地理解为 Bitmap 储存的是  像素信息 ,Drawable 储存的是  对 Canvas 的一系列操作

1.  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 {@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 只是一个抽象概念, 表示"something that can be drawn".

Drawable 的注释下面还有一段话:
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.
那么形式就比较明朗了, Drawable 是一个抽象的概念, 而 Bitmap 是其存在的实体之一.

2. 我们来看 Bitmap 类的定义:
public final class Bitmap implements Parcelable {
......
}
细心的同学会发现, Bitmap 并没有实现 Drawable,那么他们俩是如何联系起来的呢? 答案是 BitmapDrawable.:
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 的构造函数,使得 Bitmap 可以转换为 Drawable.

同样, BitmapDrawable 的 getBitmap 方法也会返回 bitmap对象:
    /**
     * Returns the bitmap used by this drawable to render. May be null.
     */
    public final Bitmap getBitmap() {
        return mBitmapState.mBitmap;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值