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通过画板画出来

[java] view plain copy
 
  1. public static Bitmap drawableToBitmap(Drawable drawable) {  
  2.         // 取 drawable 的长宽  
  3.         int w = drawable.getIntrinsicWidth();  
  4.         int h = drawable.getIntrinsicHeight();  
  5.   
  6.         // 取 drawable 的颜色格式  
  7.         Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888  
  8.                 : Bitmap.Config.RGB_565;  
  9.         // 建立对应 bitmap  
  10.         Bitmap bitmap = Bitmap.createBitmap(w, h, config);  
  11.         // 建立对应 bitmap 的画布  
  12.         Canvas canvas = new Canvas(bitmap);  
  13.         drawable.setBounds(00, w, h);  
  14.         // 把 drawable 内容画到画布中  
  15.         drawable.draw(canvas);  
  16.         return bitmap;  
  17. }  

5、Bitmap → byte[]

 

[java] view plain copy
 
  1. public byte[] BitmapToByte(Bitmap bmp) {  
  2.     ByteArrayOutputStream b = new ByteArrayOutputStream();  
  3.     bmp.compress(Bitmap.CompressFormat.PNG, 100, b);  
  4.     return b.toByteArray();  
  5. }  

6、byte[] → Bitmap 

 

[java] view plain copy
 
  1. public Bitmap Bytes2Bimap(byte[] b) {  
  2.         if (b.length != 0) {  
  3.             return BitmapFactory.decodeByteArray(b, 0, b.length);  
  4.         } else {  
  5.             return null;  
  6.         }  
  7.     }  

 

Bitmap和Drawable的区别,为什么要用bitmap?

 

1. 注意看 Drawable 的注释:

  1. * A Drawable is a general abstraction for "something that can be drawn." Most
  2. * often you will deal with Drawable as the type of resource retrieved for
  3. * drawing things to the screen; the Drawable class provides a generic API for
  4. * dealing with an underlying visual resource that may take a variety of forms.
  5. * Unlike a {@link android.view.View}, a Drawable does not have any facility to
  6. * receive events or otherwise interact with the user.

再看看其类定义:

  1. public abstract class Drawable {
  2. ......
  3. }

也就是说 Drawable 只是一个抽象概念, 表示"something that can be drawn".

Drawable 的注释下面还有一段话:

  1. Though usually not visible to the application, Drawables may take a variety of forms:
  2. 1. Bitmap: the simplest Drawable, a PNG or JPEG image.
  3. 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.
  4. 3. Shape: contains simple drawing commands instead of a raw bitmap, allowing it to resize better in some cases.
  5. 4. Layers: a compound drawable, which draws multiple underlying drawables on top of each other.
  6. 5. States: a compound drawable that selects one of a set of drawables based on its state.
  7. 6. Levels: a compound drawable that selects one of a set of drawables based on its level.
  8. 7. Scale : a compound drawable with a single child drawable, whose overall size is modified based on the current level.

那么形式就比较明朗了, Drawable 是一个抽象的概念, 而 Bitmap 是其存在的实体之一.

2. 我们来看 Bitmap 类的定义:

  1. public final class Bitmap implements Parcelable {
  2. ......
  3. }

细心的同学会发现, Bitmap 并没有实现 Drawable,那么他们俩是如何联系起来的呢? 答案是 BitmapDrawable.:

  1. public class BitmapDrawable extends Drawable {
  2. ......
  3. }


Drawable 类中有一个方法:

  1. private static Drawable drawableFromBitmap(Resources res, Bitmap bm, byte[] np,
  2. Rect pad, Rect layoutBounds, String srcName) {
  3.  
  4. if (np != null) {
  5. return new NinePatchDrawable(res, bm, np, pad, layoutBounds, srcName);
  6. }
  7.  
  8. return new BitmapDrawable(res, bm);
  9. }

通过 BitmapDrawable 的构造函数,使得 Bitmap 可以转换为 Drawable.

同样, BitmapDrawable 的 getBitmap 方法也会返回 bitmap对象:

    1. /**
    2. * Returns the bitmap used by this drawable to render. May be null.
    3. */
    4. public final Bitmap getBitmap() {
    5. return mBitmapState.mBitmap;
    6. }
原文地址:https://www.cnblogs.com/Free-Thinker/p/12157906.html