Android Bitmap 与 Drawable之间的区别和转换
2016-01-28 09:28
148 查看
Bitmap - 称作位图,一般位图的文件格式后缀为bmp,当然编码器也有很多如RGB565、RGB888。作为一种逐像素的显示对象执行效率高,但是缺点也很明显存储效率低。我们理解为一种存储对象比较好。
Drawable - 作为Android平下通用的图形对象,它可以装载常用格式的图像,比如GIF、PNG、JPG,当然也支持BMP,当然还提供一些高级的可视化对象,比如渐变、图形等。
A bitmap is a Drawable. A Drawable is not necessarily a bitmap. Like all thumbs are fingers but not all fingers are thumbs.
The API dictates:
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
Drawable - 作为Android平下通用的图形对象,它可以装载常用格式的图像,比如GIF、PNG、JPG,当然也支持BMP,当然还提供一些高级的可视化对象,比如渐变、图形等。
A bitmap is a Drawable. A Drawable is not necessarily a bitmap. Like all thumbs are fingers but not all fingers are thumbs.
The API dictates:
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.
对比项 | 显示清晰度 | 占用内存 | 支持缩放 | 支持色相色差调整 | 支持旋转 | 支持透明色 | 绘制速度 | 支持像素操作 |
---|---|---|---|---|---|---|---|---|
Bitmap | 相同 | 大 | 是 | 是 | 是 | 是 | 慢 | 是 |
Drawable | 相同 | 小 | 是 | 否 | 是 | 是 | 快 | 否 |
//转换Bitmap to Drawable Bitmap bitmap = new Bitmap (...); Drawable drawable = new BitmapDrawable(bitmap); //转换Drawable to Bitmap Drawable d = ImagesList.get(0); Bitmap bitmap = ((BitmapDrawable)d).getBitmap(); //1、Drawable → Bitmap public static Bitmap drawableToBitmap(Drawable drawable) { Bitmap bitmap = Bitmap .createBitmap( drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565); Canvas canvas = new Canvas(bitmap); //canvas.setBitmap(bitmap); drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); drawable.draw(canvas); return bitmap; } //2、从资源中获取Bitmap Resources res=getResources(); Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic); //3、Bitmap → byte[] private byte[] Bitmap2Bytes(Bitmap bm){ ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.PNG, 100, baos); return baos.toByteArray(); } //4、 byte[] → Bitmap private Bitmap Bytes2Bimap(byte[] b){ if(b.length!=0){ return BitmapFactory.decodeByteArray(b, 0, b.length); } else { return null; } }
相关文章推荐
- Android 测试时的server重定向
- Android中BaseAdapter的理解
- 转【Android】Paint的效果研究
- Android图片下载缓存库picasso解析
- layout_weight/weightSum详解
- AndroidManifest.xml文件详解(application标签)
- android wifi状态3种广播
- 最快速的Android开发环境搭建ADT-Bundle及Hello World
- 初探android应用性能分析
- Android智能手机屏蔽电话与屏蔽安装软件功能
- Android如何使用API
- Android开发学习笔记:Intent的简介以及属性的详解
- facebook freso使用的时候的drawable的问题
- android151 笔记 3
- Android的编译过程 & Android dex 方法限制的一些总结
- android onNewIntent方法
- Android RxJava 的详解(转)
- android152 笔记 2
- 侧滑菜单(三)
- Android启动流程