当前位置: 首页 > 编程笔记 >

Android Bitmap和Drawable的对比

颛孙正谊
2023-03-14
本文向大家介绍Android Bitmap和Drawable的对比,包括了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

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

 类似资料:
  • Views and Drawable for animated GIFs in Android. Overview Bundled GIFLib via JNI is used to render frames. This way should be more efficient than WebView or Movie classes. Javadoc Setup Gradle (Androi

  • [免责声明:首先,我知道有很多关于drawable和mipmap文件夹的帖子,我读了很多] 在《Android Programming-The Big Nerd Ranch Guide》一书中有一个示例,其中我必须在上的一些文本旁边插入一个图标。对于更高分辨率的屏幕,包含多个大小的图标是有意义的。在他们的解决方案中,他们的所有图像都在许多不同的可绘制文件夹中,具有不同的图像分辨率大小。 然而,我对

  • 本文向大家介绍Android对图片Drawable实现变色示例代码,包括了Android对图片Drawable实现变色示例代码的使用技巧和注意事项,需要的朋友参考一下 前言 本文主要给大家介绍了Android中图片DrawableCompat利用setTint()对图片Drawable进行变色的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍: 1.利用color资源对Dra

  • 本文向大家介绍详解Android中Drawable方法,包括了详解Android中Drawable方法的使用技巧和注意事项,需要的朋友参考一下 本文为大家分享了Android中Drawable方法的详细使用方法,供大家参考,具体内容如下 1. BitmapDrawable相关方法: 新建在drawable目录下面,示例如下: 具体每个属性的解释如下: 上述的排版方式只是为了方便查看,不能直接复制到

  • 主要内容:本节引言:,1.LayerDrawable,2.TransitionDrawable,3.LevelListDrawable,4.StateListDrawable,本节小结:本节引言: 本节我们来把剩下的四种Drawable也学完,他们分别是: LayerDrawable,TransitionDrawable,LevelListDrawable和StateListDrawable, 依旧贴下13种Drawable的导图: 1.LayerDrawable 层图形对象,包含一个Drawa

  • 主要内容:本节引言:,1.BitmapDrawable,2.InsetDrawable,3.ClipDrawable,4.RotateDrawable,5.AnimationDrawable,本节小结:本节引言: 本节我们继续来学习Android中的Drawable资源,上一节我们学习了: ColorDrawable; NinePatchDrawable; ShapeDrawable; GradientDrawable! 这四个Drawable~ 而本节我们继续来学习接下来的五个Drawable