二次采样 OOM的克星

来源:互联网 发布:python idle打开闪退 编辑:程序博客网 时间:2024/04/30 12:57
//找控件        ImageView iv = (ImageView) findViewById(R.id.iv);        //加载图片 没有对图片进行处理        /*Bitmap bitmap = BitmapFactory.decodeFile("/mnt/shared/Image/big.JPG");        iv.setImageBitmap(bitmap);*/        //使用二次采样对图片进行压缩        BitmapFactory.Options options = new BitmapFactory.Options();        //只加载图片的宽高 不加载图片像素  第一次采样        options.inJustDecodeBounds = true;        Bitmap bitmap = BitmapFactory.decodeFile("/mnt/shared/Image/big.JPG",options);        //获取图片宽高        int outWidth = options.outWidth;        int outHeight = options.outHeight;        //获取手机屏幕的宽高        Display display = getWindowManager().getDefaultDisplay();        int width = display.getWidth();        int height = display.getHeight();        int scaleX = outWidth/width;        int scaleY = outHeight/height;        int scale = 0;        //对比大的作为缩放比例        scale = scaleX>scaleY?scaleX:scaleY;         //第二次采样 加载图片的像素        options.inJustDecodeBounds  = false;        //设置图片缩放比例        options.inSampleSize = scale;        Bitmap bitmap1 = BitmapFactory.decodeFile("/mnt/shared/Image/big.JPG",options);        iv.setImageBitmap(bitmap1);