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;
-
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
2016-01-06 Android清理内存
2016-01-06 Swift继承的用法
2016-01-06 swift通过摄像头读取每一帧的图片,并且做识别做人脸识别
2016-01-06 IOS用CGContextRef画各种图形(文字、圆、直线、弧线、矩形、扇形、椭圆、三角形、圆角矩形、贝塞尔曲线、图片)
2016-01-06 IOS将UIView转化为UIImage
2016-01-06 HexColor
2016-01-06 UIColor-Hex-Swift