android优化图片内存
来源:互联网 发布:php 使用aes加密 编辑:程序博客网 时间:2024/05/16 06:29
android中图片非常消耗内存,现在不同的手机分配给应用的内存不近相同,有的几十M,有的有100M,如果应用中有非常多的图片,则需要做处理,不然很容易出现OOM现象。
android分配个图片的内存是按照图片分辨率乘以每个像素点所占字节决定的,公式为HightPix*WithPix*Bitmap.Config.ARGB?
Color.Rgb指的是色彩编码的种类
Bitmap.Config.ARGB_4444 每个像素占2个字节
Bitmap.Config.ARGB_8888 每个像素占4个字节
Bitmap.Config.RGB_565 每个像素占2个字节
获取网络图片时通过BitmapFactory.Option对图片处理
public Bitmap getHttpBitmap(final String url) { try { myFileURL = new URL(url); HttpURLConnection conn = (HttpURLConnection) myFileURL .openConnection(); conn.setConnectTimeout(6000); conn.setDoInput(true); conn.setUseCaches(false); InputStream is = conn.getInputStream(); BitmapFactory.Options opts = new BitmapFactory.Options(); //设置该opts.inJustDecodeBounds=true属性后BitmapFactory.decodeByteArray不会返回bitmap,opts中会返回 //图片的像素尺寸 opts.inJustDecodeBounds=true; byte[] data = getBytes(is); BitmapFactory.decodeByteArray(data, 0,data.length,opts); Log.i("ImageLoader","width="+opts.outWidth+",height="+opts.outHeight); opts.inJustDecodeBounds=false; opts.inSampleSize=calculateInSampleSize(opts,150,300); opts.inPreferredConfig = Bitmap.Config.ARGB_8888; bitmap = BitmapFactory.decodeByteArray(data, 0,data.length,opts); Log.i("ImageLoader","houwidth="+bitmap.getWidth()+",houheight="+bitmap.getHeight()); } catch (Exception e) { e.printStackTrace(); Log.i("HttpUtils","getHttpBitmap异常"); }finally{ if(is!=null){ try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } return bitmap; } public static byte[] getBytes(InputStream is) throws IOException { ByteArrayOutputStream outstream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; // 用数据装 int len = -1; while ((len = is.read(buffer)) != -1) { outstream.write(buffer, 0, len); } outstream.close(); // 关闭流一定要记得。 return outstream.toByteArray(); } // 传入需要的像素尺寸后得到压缩比例 public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { // 源图片的高度和宽度 final int height = options.outHeight; final int width = options.outWidth; Log.i("ImageLoader","width="+width+",height="+height); 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; } return inSampleSize; }
这样就完成了图片的处理
0 0
- android图片内存优化
- Android图片内存优化
- android图片内存优化
- Android图片内存优化
- Android图片内存优化
- android优化图片内存
- android图片内存优化
- android图片的内存优化
- Android图片的内存优化
- android图片的内存优化
- android图片的内存优化
- android图片的内存优化
- android图片的内存优化
- android图片的内存优化
- 读取图片 优化android内存
- android图片的内存优化
- Android图片与内存优化
- Android图片与内存优化
- NSURLSession 学习笔记
- linux screen 命令详解
- 最大连续乘积子数组u
- NSURLSession学习笔记(三)Download Task
- JAVA——对象所占内存空间大小的计算
- android优化图片内存
- UVa 11221 - Magic square palindromes.
- 成员变量命名规范,类方法,self
- Shader学习笔记_Vertex Shader_几何变换_波
- CSU 1541- There is No Alternative(克鲁斯卡尔)
- Android打包APK遇到的错误]conversion to dalvik format failed with error 1的解决办法
- 信号量与互斥锁
- Javascript基本语法,与html,C 语言比较
- poj 1200