java 构建位图_调整位图大小而不创建新位图

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

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

// 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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值