这里有新鲜出炉的精品教程,程序狗速度看过来!
Android 是一种基于 Linux 的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由 Google 公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用 "安卓" 或 "安致"。
这篇文章主要介绍了 Android Bitmap 和 Drawable 的对比的相关资料, 需要的朋友可以参考下
Android Bitmap 和 Drawable 的对比
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.
Bitmap 是 Drawable . Drawable 不一定是 Bitmap . 就像拇指是指头, 但不是所有的指头都是拇指一样.
The API dictates: API 规定:
- Though usually not visible to the application, Drawables may take a variety of forms: 尽管通常情况下对于应用是不可见的,Drawables 可以采取很多形式:
- Bitmap: the simplest Drawable, a PNG or JPEG image. Bitmap: 简单化的Drawable, PNG 或JPEG图像.
- Nine Patch: an extension to the PNG format allows it to specify information about how to stretch it and place things inside of it.
- Shape: contains simple drawing commands instead of a raw bitmap, allowing it to resize better in some cases.
- Layers: a compound drawable, which draws multiple underlying drawables on top of each other.
- States: a compound drawable that selects one of a set of drawables based on its state.
- Levels: a compound drawable that selects one of a set of drawables based on its level.
- Scale: a compound drawable with a single child drawable, whose overall size is modified based on the current level.
对比项 | 显示清晰度 | 支持透明色 | 支持色相色差调整 | 支持像素操作 |
---|---|---|---|---|
Bitmap | 相同 | 是 | 是 | 是 |
Drawable | 相同 | 是 | 否 | 否 |
Drawable 在内存占用和绘制速度这两个非常关键的点上胜过 Bitmap
- Drawable 和 Bitmap 之间可以互相转换,Drawable 占用内存远小于 Bitmap。
- setImageDrawable 使用资源文件;setImageBitmap 使用 bitmap 图片,该图片可能是读取本地相册,或者从资源文件转换而来。
- setImageResource() 和 setImageBitmap()
- //setImageResource()
- public void setImageResource(int resId) //占用UI thread;
- // setImageBitmap()
- ImageView iv;
- String fileName = "/data/data/com.test/aa.png";
- Bitmap bm = BitmapFactory.decodeFile(fileName);
- iv.setImageBitmap(bm); //占用内存
- // setImageBitmap()
- Bitmap image = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
- imageView.setImageBitmap(image);
- // Bitmap转换成Drawable
- Bitmap image = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
- BitmapDrawable bitmapDrawable = new BitmapDrawable(image);
- imageView.setImageDrawable(bitmapDrawable);
- // 结论:Bitmap是Drawable . Drawable不一定是Bitmap
小结
Bitmap: 简单化的 Drawable, PNG 或 JPEG 图像.
Drawable 在内存占用和绘制速度这两个非常关键的点上胜过 Bitmap
来源: http://www.phperz.com/article/17/0819/337589.html