Android Bitmap和Drawable的对比
AndroidBitmap和Drawable的对比
Bitmap-称作位图,一般位图的文件格式后缀为bmp,当然编码器也有很多如RGB565、RGB888。作为一种逐像素的显示对象执行效率高,但是缺点也很明显存储效率低。我们理解为一种存储对象比较好。
Drawable-作为Android平下通用的图形对象,它可以装载常用格式的图像,比如GIF、PNG、JPG,当然也支持BMP,当然还提供一些高级的可视化对象,比如渐变、图形等。
AbitmapisaDrawable.ADrawableisnotnecessarilyabitmap.Likeallthumbsarefingersbutnotallfingersarethumbs.
Bitmap是Drawable.Drawable不一定是Bitmap.就像拇指是指头,但不是所有的指头都是拇指一样.
TheAPIdictates:API规定:
Thoughusuallynotvisibletotheapplication,Drawablesmaytakeavarietyofforms:尽管通常情况下对于应用是不可见的,Drawables可以采取很多形式: Bitmap:thesimplestDrawable,aPNGorJPEGimage.Bitmap:简单化的Drawable,PNG或JPEG图像. NinePatch:anextensiontothePNGformatallowsittospecifyinformationabouthowtostretchitandplacethingsinsideofit. Shape:containssimpledrawingcommandsinsteadofarawbitmap,allowingittoresizebetterinsomecases. Layers:acompounddrawable,whichdrawsmultipleunderlyingdrawablesontopofeachother. States:acompounddrawablethatselectsoneofasetofdrawablesbasedonitsstate. Levels:acompounddrawablethatselectsoneofasetofdrawablesbasedonitslevel. Scale:acompounddrawablewithasinglechilddrawable,whoseoverallsizeismodifiedbasedonthecurrentlevel.
对比项 | 显示清晰度 | 支持透明色 | 支持色相色差调整 | 支持像素操作 |
---|---|---|---|---|
Bitmap | 相同 | 是 | 是 | 是 |
Drawable | 相同 | 是 | 否 | 否 |
Drawable在内存占用和绘制速度这两个非常关键的点上胜过Bitmap
-Drawable和Bitmap之间可以互相转换,Drawable占用内存远小于Bitmap。
-setImageDrawable使用资源文件;setImageBitmap使用bitmap图片,该图片可能是读取本地相册,或者从资源文件转换而来。
-setImageResource()和setImageBitmap()
//setImageResource() publicvoidsetImageResource(intresId)//占用UIthread; //setImageBitmap() ImageViewiv; StringfileName="/data/data/com.test/aa.png"; Bitmapbm=BitmapFactory.decodeFile(fileName); iv.setImageBitmap(bm);//占用内存 //setImageBitmap() Bitmapimage=BitmapFactory.decodeFile(imgFile.getAbsolutePath()); imageView.setImageBitmap(image); //Bitmap转换成Drawable Bitmapimage=BitmapFactory.decodeFile(imgFile.getAbsolutePath()); BitmapDrawablebitmapDrawable=newBitmapDrawable(image); imageView.setImageDrawable(bitmapDrawable); //结论:Bitmap是Drawable.Drawable不一定是Bitmap
小结
Bitmap:简单化的Drawable,PNG或JPEG图像.
Drawable在内存占用和绘制速度这两个非常关键的点上胜过Bitmap
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!