Android Bitmap和Drawable的对比

作者:灿岩 时间:2021-11-16 06:03:41 

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://blog.csdn.net/zhangcanyan/article/details/72811985

标签:Android,Bitmap,Drawable
0
投稿

猜你喜欢

  • 深入了解c# 迭代器和列举器

    2022-04-24 16:32:37
  • Spring Boot + Mybatis多数据源和动态数据源配置方法

    2023-02-16 17:15:31
  • SpringBoot FreeWorker模板技术解析

    2023-08-09 09:14:39
  • 深入了解Java ServletContext

    2023-11-08 22:36:27
  • Java8中CompletableFuture的用法全解

    2023-09-08 15:08:55
  • React-Native之Android(6.0及以上)权限申请详解

    2023-07-28 01:56:55
  • spring boot整合netty的实现方法

    2021-11-17 04:28:03
  • Java常用数字工具类 数字转汉字(1)

    2022-06-25 11:01:45
  • 详解Java实现批量压缩图片裁剪压缩多种尺寸缩略图一键批量上传图片

    2022-12-07 15:56:04
  • 详细总结Java堆栈内存、堆外内存、零拷贝浅析与代码实现

    2023-11-21 01:52:29
  • C#实现打字小游戏

    2023-11-08 01:08:04
  • Java实现窗体程序显示日历

    2022-09-14 11:01:59
  • Java常见数据结构面试题(带答案)

    2023-11-24 19:44:05
  • Flyway详解及Springboot集成Flyway的详细教程

    2022-09-21 10:37:49
  • 一篇文章带你搞定JAVA注解

    2023-03-15 05:22:23
  • java封装前端查询条件通用版

    2023-06-24 12:06:15
  • Java如何处理延迟任务过程解析

    2022-04-29 15:11:37
  • C#中@的用法总结

    2023-03-11 10:02:51
  • Java基础之面向对象机制(多态、继承)底层实现

    2023-11-12 02:59:59
  • springboot+chatgpt+chatUI Pro开发智能聊天工具的实践

    2023-10-30 05:38:03
  • asp之家 软件编程 m.aspxhome.com