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
原创粉丝点击