调整位图的大小而无需创建新的位图 [英] Resize bitmap without creating new bitmap

查看:126
本文介绍了调整位图的大小而无需创建新的位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否仍然可以在不创建新位图的情况下调整位图的大小或缩放比例?假设我下载的图片高度或宽度大于2048px.在我可以显示它之前,我必须调整它的大小,因为ImageView不支持大于2048px的位图.如果我使用Bitmap.createScaledBitmap(Bitmap src,int dstWidth,int dstHeight,boolean filter)它给了我一个新的位图.现在我们有两个位图,原始的和缩放的.那就是我的应用程序内存不足时.

Is there anyway to resize/scale bitmap without creating a new bitmap? Let say i download image that height or width is larger than 2048px. Before i can display it, i have to resize it because ImageView does not support bitmaps larger than 2048px. If i use Bitmap.createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter) it gives me a new bitmap. Now we have two bitmaps, original and scaled. Thats when my application runs out of memory.

下面是我现在使用的代码.

Below is the code which i use right now.

// Async task to download the image
private ImageView mImage;
private ProgressBar progress;
private Button button;

        @Override
        protected void onPostExecute(Bitmap result){
            progress.setVisibility(View.GONE);
            if(result != null){

                if(result.getHeight() > 2048 || result.getWidth() > 2048){
                    float scaledvalues[] = scale(result.getWidth(), result.getHeight());
                    image = Bitmap.createScaledBitmap(result, (int)scaledvalues[0], (int)scaledvalues[1], false);
                    mImage.setBitmap(image);
                }
                else{
                    image = result;
                    mImage.setBitmap(result);
                }
                button.setEnabled(true);
            }
        }

    //I use this method to calculate new width and height
    public float[] scale(int width, int height){
        float scaledheight = -1f;
        float scaledwidth = -1f;
        float scaledheightpros = -1f;
        float scaledwidthpros = -1f;
        float finalheight = -1f;
        float finalwidth = -1f;
        if(height > 2048){
            scaledheight = height - 2048f;
            float s = scaledheight*100f;
            scaledheightpros = s / 100f;
        }
        if(width > 2048){
            scaledwidth = width - 2048f;
            float z = scaledwidth * 100f;
            scaledwidthpros = z / width;
        }
            if(scaledheightpros > scaledwidthpros){
                float a = height/100f;
                float b = width/100f;
                finalheight = height - (a * scaledheightpros);
                finalwidth = width - (b * scaledheightpros);
            }
            else{
                float a = height/100f;
                float b = width/100f;
                finalheight = height - (a * scaledwidthpros);
                finalwidth = width - (b * scaledwidthpros);
            }

        Log.i(TAG, "startingheight: " + height + " finalheight: " + finalheight + "%: " + scaledheightpros);
        Log.i(TAG, "startingwidth: " + width + " finalwidth: " + finalwidth + "%: " + scaledwidthpros);
        float array[] = {finalwidth, finalheight};
        return array;
    }

推荐答案

如果遇到内存不足的问题,请使用采样.

use sampling if you facing out of memory issue.

public static int calculateInSampleSize(
 BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;

if (height > reqHeight || width > reqWidth) {

    // Calculate ratios of height and width to requested height and width
    final int heightRatio = Math.round((float) height / (float) reqHeight);
    final int widthRatio = Math.round((float) width / (float) reqWidth);

    // Choose the smallest ratio as inSampleSize value, this will guarantee
    // a final image with both dimensions larger than or equal to the
    // requested height and width.
    inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}

return inSampleSize;
}

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
    int reqWidth, int reqHeight) {

// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);

// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}

最后在Imageview中设置图像时,请使用

At the end for setting image in Imageview, use this

 mImageView.setImageBitmap(decodeSampledBitmapFromResource(getResources(),R.id.myimage,    
Screen_width, screen_height));

必须阅读

这篇关于调整位图的大小而无需创建新的位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆