android优化图片内存

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值