前言:”安得广厦千万间,大庇天下寒士俱欢颜“——杜甫。在帝都住的朋友们都可能会遇到租房子困难的问题(土豪请无视),找房子真是力气活,还耗费时间,占用我宝贵的写博客时间,没办法,谁让咱没钱还想住的好点,努力努力挣钱!!!以上发点牢骚,现在进入正题。上一篇博客《Bitmap那些事之内存占用计算和加载注意事项》,写了Bitmap基础知识和使用Bitmap需要知道的注意事项,这一片博客我会写在Android应用中Bitmap的创建和加载。
1、BitmapFactory使用:
说到图片的加载就必须说BitmapFactory,看名字就知道他的作用了,就是一个生产Bitmap的工厂,下图是它的一些工厂方法:
从上图可以看到BitmapFactory可以使用存储Bitmap数据的数组,Bitmap的资源ID,Bitmap文件等做为数据源来创建Bitmap对象,具体情况看你程序中提供的数据源是哪一种。这些方法中对每一种数据源都提供了两个方法,这里需要注意一下BitmapFacotry.Options参数,它是BitmapFactory的内部类,有一些成员变量含义需要记一下,下面就来说说。
2、BitmapFacotry.Options的inJustDecodeBounds 参数使用:
为了节省内存,很多情况下原图片都要经过缩放处理,根据控件的尺寸来处理成对应尺寸的图片,这时使用BitmapFactory创建Bitmap,很多情况下都会使用下面的代码:
BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds =true; BitmapFactory.decodeResource(getResources(), R.id.myimage, options); int imageHeight = options.outHeight; int imageWidth = options.outWidth; String imageType = options.outMimeType;
注意上面中的options.inJustDecodeBounds =true的inJustDecodeBounds参数,为了避免我翻译的不准确我这里先贴出来google的原文: If set to true, the decoder will return null (no bitmap), but the out... fields will still be set, allowing the caller to query the bitmap without having to allocate the memory for its pixels。用我的话来说就是在decode的时候不给这个bitmap的像素区分配内存,除了这个区别Bitmap的其他信息你都能获取到。这样就有很大的意义,你既没有消耗内存又拿到了图片的信息,为你下一步图片处理提供帮助。
3、BitmapFacotry.Options的inSampleSize参数使用:
上一步你已经获取到图片的原始尺寸了,下一步就是要把原图缩放到你需要的大小,可以通过inSampleSize参数来设置,google原文的解释是:If set to a value > 1, requests the decoder to subsample the original image, returning a smaller image to save memory. The sample size is the number of pixels in either dimension that correspond to a single pixel in the decoded bitmap. For example, inSampleSize == 4 returns an image that is 1/4 the width/height of the original, and 1/16 the number of pixels. Any value <= 1 is treated the same as 1. Note: the decoder will try to fulfill this request, but the resulting bitmap may have different dimensions that precisely what has been requested. Also, powers of 2 are often faster/easier for the decoder to honor.(不管你看不看英文文档我还是要把google原文贴出来,我英文比较烂,翻译的不一定准确),大概意思就是说这个参数可以调节你在decode原图时所需要的内存,有点像采样率,会丢掉一些像素,值是大于1的数,为2的幂时更利于运算。举个例子:当 inSampleSize == 4 时会返回一个尺寸(长和宽)是原始尺寸1/4,像素是原来1/16的图片。这个值怎么计算呢?
public static int calculateInSampleSize( BitmapFactory.Options options,int reqWidth,int reqHeight){ // Raw height and width of image finalint height = options.outHeight; finalint width = options.outWidth; int inSampleSize =1; if(height > reqHeight || width > reqWidth){ finalint halfHeight = height /2; finalint halfWidth = width /2; // Calculate the largest inSampleSize value that is a power of 2 and keeps both // height and width larger than the requested height and width. while((halfHeight / inSampleSize)> reqHeight &&(halfWidth / inSampleSize)> reqWidth){ inSampleSize *=2; } } return inSampleSize; }
在decode的时候先设置options.inJustDecodeBounds =true,获取到图片参数后再设置为false,这就是decode时的技巧,下面就把完整代码贴出来,可以作为工具方法来使用:
public static Bitmap decodeSampledBitmapFromResource(Resources res,int resId, int reqWidth,int reqHeight){ // First decode with inJustDecodeBounds=true to check dimensions finalBitmapFactory.Options options =newBitmapFactory.Options(); options.inJustDecodeBounds =true; BitmapFactory.decodeResource(res, resId, options); // Calculate inSampleSize options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // Decode bitmap with inSampleSize set options.inJustDecodeBounds =false; returnBitmapFactory.decodeResource(res, resId, options); }
上面的方法来自于google官网,没必要进行修改,这就是程序员的拿来主义吧,关键在于要知道为什么这么写。下面是我自己写的一个方法可以直接拿来当工具用。
/** * 对图片进行压缩,主要是为了解决控件显示过大图片占用内存造成OOM问题,一般压缩后的图片大小应该和用来展示它的控件大小相近. * * @param context 上下文 * @param resId 图片资源Id * @param reqWidth 期望压缩的宽度 * @param reqHeight 期望压缩的高度 * @return 压缩后的图片 */ public static Bitmap compressBitmapFromResourse(Context context, int resId, int reqWidth, int reqHeight) { final BitmapFactory.Options options = new BitmapFactory.Options(); /* * 第一次解析时,inJustDecodeBounds设置为true, * 禁止为bitmap分配内存,虽然bitmap返回值为空,但可以获取图片大小 */ options.inJustDecodeBounds = true; BitmapFactory.decodeResource(context.getResources(), resId, options); final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { final int heightRatio = Math.round((float) height / (float) reqHeight); final int widthRatio = Math.round((float) width / (float) reqWidth); inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; } options.inSampleSize = inSampleSize; // 使用计算得到的inSampleSize值再次解析图片 options.inJustDecodeBounds = false; return BitmapFactory.decodeResource(context.getResources(), resId, options); }
以上就是Bitmap在Android中加载到内存中的一些小技巧,大家是不是以后就能很好的应用起来,避免因为加载图片引起OOM这样的问题呢?如果您有更好更棒的方法可以给我留言或者添加我的微信公众号:coder_online。大家共同学习,共同进步,赚钱不再为房子发愁。你可以方便的扫描下面的二维码进行添加:
相关推荐
android bitmap outofMemory 用来解决android中常见的bitmap outOfMemory
Android Bitmap详解及Bitmap的内存优化 一、Bitmap: Bitmap是Android系统中的图像处理的最重要类之一。用它可以获取图像文件信息,进行图像剪切、旋转、缩放等操作,并可以指定格式保存图像文件。 常用方法: ...
处理bitmap内存溢出问题
Bitmap bitmap = BitmapFactory.decodeFile(imageFile, opts); 设置inJustDecodeBounds为true后,decodeFile并不分配空间,但可计算出原始图片的长度和宽度,即opts.width和opts.height。有了这两个参数,再通过...
Android 内存 OOM 优化和治理,Android 内存泄漏原因详解与应对措施,Android 中的 Bitmap 使用详解,Android 中的 Bitmap 内存治理和优化详解,Android 内存分析命令详解,Java内存模型。Android 内存 OOM 优化和...
ANDROIDBITMAP内存限制OOM,OUTOFMEMORY.pdf
这里就有疑问了,Android系统有自己的垃圾回收机制,可以不定期的回收掉不使用的内存空间,当然也包括Bitmap的空间。那为什么还需要这个方法呢? Bitmap类的构造方法都是私有的,所以开发者不能直接new出一个Bitmap...
android Bitmap用法总结 Bitmap用法总结 1、Drawable → Bitmap public static Bitmap drawableToBitmap(Drawable drawable) { Bitmap bitmap = Bitmap .createBitmap( drawable.getIntrinsicWidth(), drawable....
在Android开发过程中,Bitmap往往会给开发者带来一些困扰,因为对Bitmap操作不慎,就容易造成OOM(Java.lang.OutofMemoryError – 内存溢出),本篇博客,我们将一起探讨Bitmap的性能优化。 为什么Bitmap会导致OOM? 1...
Android性能优化(七)Bitmap内存压缩示例源码
ANDROIDBITMAP内存限制OOM,OUTOFMEMORY[文].pdf
若是从内存中读取到的Bitmap为空,则在磁盘缓存中读取。 若是从磁盘中读取到的Bitmap为空,则从起始资源中读取 2.开启工作线程,执行从起始资源中读取bitmap的任务 3.计算合适比例,读取适屏的bitmap 4.检查...
双缓存机制下,Android应用会对大内存的图片做二级缓存,对于溢出缓存的bitmap实行recycle,但是有些bitmap正在显示,会造成概率性的花屏等, 该方案提供了一套完整的防止recycle正在显示的bitmap的方案
可以很好的出来android开发过程中,由bitmap导致的内存溢出的问题。
Android中bitmap引起内存溢出的解决方案的对比分析.pdf
Android中图片是以bitmap形式存在的,那么bitmap所占内存,直接影响到了应用所占内存大小,首先要知道bitmap所占内存大小计算方式: 图片长度 x 图片宽度 x 一个像素点占用的字节数 以下是图片的压缩格式: 其中,A...
缩小尺寸可不是指在layout中设置一下宽高使图片缩小(其实质还是一张占内存大图),而是实实在在的将图片本身缩小,减小内存占用。