Bitmap和Drawable的关系区别
Posted free-thinker
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Bitmap和Drawable的关系区别相关的知识,希望对你有一定的参考价值。
Bitmap
- 称作位图,一般位图的文件格式后缀为bmp
Drawable
- 作为android平下通用的图形对象,它可以装载常用格式的图像
比如GIF、PNG、JPG,当然也支持BMP,当然还提供一些高级的可视化对象,比如渐变、图形等。
Bitmap是Drawable . Drawable不一定是Bitmap
Drawable在内存占用和绘制速度这两个非常关键的点上胜过Bitmap
1、Bitmap对象
Resources res = getResources();
Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.icon);
获取其宽高的方法:
bmp.getHeight()
bmp.getWidth()
2、Drawable对象
Drawable drawable = getResources().getDrawable(R.drawable.icon);
获取其宽高的方法:
drawable.getIntrinsicWidth();
drawable.getIntrinsicHeight();
3、Bitmap转换成Drawable
Bitmap bm=xxx; //xxx根据你的情况获取
BitmapDrawable bd= new BitmapDrawable(getResource(), bm);
//因为BtimapDrawable是Drawable的子类,最终直接使用bd对象即可。
4、Drawable转化为Bitmap
转化的方式是把Brawable通过画板画出来
- public static Bitmap drawableToBitmap(Drawable drawable) {
- // 取 drawable 的长宽
- int w = drawable.getIntrinsicWidth();
- int h = drawable.getIntrinsicHeight();
- // 取 drawable 的颜色格式
- Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
- : Bitmap.Config.RGB_565;
- // 建立对应 bitmap
- Bitmap bitmap = Bitmap.createBitmap(w, h, config);
- // 建立对应 bitmap 的画布
- Canvas canvas = new Canvas(bitmap);
- drawable.setBounds(0, 0, w, h);
- // 把 drawable 内容画到画布中
- drawable.draw(canvas);
- return bitmap;
- }
5、Bitmap → byte[]
- public byte[] BitmapToByte(Bitmap bmp) {
- ByteArrayOutputStream b = new ByteArrayOutputStream();
- bmp.compress(Bitmap.CompressFormat.PNG, 100, b);
- return b.toByteArray();
- }
6、byte[] → Bitmap
- public Bitmap Bytes2Bimap(byte[] b) {
- if (b.length != 0) {
- return BitmapFactory.decodeByteArray(b, 0, b.length);
- } else {
- return null;
- }
- }
Bitmap和Drawable的区别,为什么要用bitmap?
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;
-
}
以上是关于Bitmap和Drawable的关系区别的主要内容,如果未能解决你的问题,请参考以下文章
Android中 Bitmap和Drawable相互转换的方法
bitmap和drawable的相互转化以及setImageResource(),setImageDrawable(),setImageBitmap()