繁体   English   中英

通过调整位图的大小来耗尽内存

[英]out of memory by resizing bitmap

我试图将位图的大小调整为自定义X和Y,并且我使用的代码有时会运行到OutOfMemory异常中。 我到处查看并找到了一些解决方案,该解决方案如何使用InputStream调整位图的大小,但是找不到任何方法来将其调整为自定义X和Y尺寸。 有人可以给我个建议吗?

这是我的代码:

try {

            Point scr = Sys.screenSize(getActivity());

                    // MY CUSTOM X and Y    
            int newWidth = (int) Sys.convertDpToPixel(linheight, getActivity())
                    * lines;
            int newHeight = scr.x;

            Bitmap bitmap = BitmapFactory.decodeResource(getActivity()
                    .getResources(), R.drawable.field);

            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            float scaleWidth = ((float) newWidth) / width;
            float scaleHeight = ((float) newHeight) / height;
            // CREATE A MATRIX FOR THE MANIPULATION
            Matrix matrix = new Matrix();
            // RESIZE THE BIT MAP
            matrix.postScale(scaleWidth, scaleHeight);

            // "RECREATE" THE NEW BITMAP
            bm = Bitmap
                    .createBitmap(bitmap, 0, 0, width, height, matrix, false);
            bitmap.recycle();
            matrix = null;
            iv.setImageBitmap(bm);
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
        }

我认为您想要的是createScaledBitmap:

Bitmap resizedBitmap = 
       Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, false);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM