在了解Bitmap.Config之前,首先应该了解一下一种颜色的组成。简单的说,世界上任何一种颜色的"颜色空间"都可定义成一个固定的数字或变量。RGB(红、绿、蓝)只是众多颜色空间的一种。采用这种编码方法,每种颜色都可用三个变量来表示-红色绿色以及蓝色的强度。
Bitmap.Config在官网中解释为:Bitmap的配置,用于描述和保存图片相素的保存方式。在android中其有四种:
<1>ALPHA_8 像素以半透明的形式保存,且在内存中一个像素占8位,即一个字节。透明度为半透明,无法设置
<2>ARGB_4444 引入了透明度的设置,A透明度 R 红色 G绿色 B蓝色,每种占4位,一个相素总共点16位,2个字节,此种方式占用内存较小,但图片会造成模糊现象。在API_leavel 13中已过时,被ARGB_8888代替。
<3>ARGB_8888 解释同上,但一个相素点32位,4个字节。这种方式占用内存较多,但最清楚。
<4>RGB_565 R 占5位,G 占6位 ,B 占5位,无法设置透明度,一个相素占16位,两个字节。
对于上述几种配置,视情况使用,占位越多,内存越大,但图像颜色越多。如RGB565最多只能表示2^16=65536种图像,这对于RGB888所能表示的2^24=16777216种颜色来说显然在表现力上要略逊一筹。这集中表现在显示某些带有渐变效果的图片时,出现了一条条的颜色带,而不是原始的平滑的渐变效果。后来得知android使用了Dither(抖动)这种技术,以欺骗人类眼球的方式加以补偿。
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; }
另外可参考资料
http://wangxuefang.iteye.com/admin/blogs/1956708 关于位图颜色模式的一些android处理机制和说明。
相关推荐
Android Bitmap.getPixels的正确理解演示源码,参考文章《Android Bitmap入门:getPixels的正确理解》
android bitmap outofMemory 用来解决android中常见的bitmap outOfMemory
android Bitmap用法总结 Bitmap用法总结 1、Drawable → Bitmap public static Bitmap drawableToBitmap(Drawable drawable) { Bitmap bitmap = Bitmap .createBitmap( drawable.getIntrinsicWidth(), drawable....
处理bitmap内存溢出问题
NULL 博文链接:https://buerkai.iteye.com/blog/1240797
初中级Android开发社招面试之Bitmap.pdf后续还有一系列资料,欢迎大家下载
android Bitmap相关知识介绍~~~
主要介绍了 Android canvas drawBitmap方法详解及实例的相关资料,需要的朋友可以参考下
androidbitmap的用法.pdf
Android开发者学习笔记-View、Canvas、bitmap.pdf
RoaringBitmap.zip
钱萌萌作品集.bitmap.pdf
赠送jar包:RoaringBitmap-0.7.45.jar;...使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。
基于Javascript的bitmap处理,并且将位图输出为base64编码以便于浏览器进行显示。 一、Bitmap.create(width, height, bgcolor) 创建一个width x height像素大小的位图,底色为bgcolor所代表的颜色。 如:bitmap....
开源项目-boljen-go-bitmap.zip,go-bitmap - Some simple bitmap utilities - Could someone please review SetAtomic?
基于android 人脸识别 class MyImageView extends ImageView { private Bitmap mBitmap; private Canvas mCanvas; private int mBitmapWidth = 200; private int mBitmapHeight = 200; private Paint mPaint =...
开源项目-seiflotfy-s-bitmap.zip,S-Bitmap: Distinct Counting with a Self-Learning Bitmap (an equivalent to HyperLogLog) implemented in Go
Android bitmap Android Bitmap相关