android图片缩放比例怎么算,计算图片缩小比例

/**

* Calculate an inSampleSize for use in a {@link android.graphics.BitmapFactory.Options} object when decoding

* bitmaps using the decode* methods from {@link android.graphics.BitmapFactory}. This implementation calculates

* the closest inSampleSize that is a power of 2 and will result in the final decoded bitmap

* having a width and height equal to or larger than the requested width and height.

*

* @param options An options object with out* params already populated (run through a decode*

* method with inJustDecodeBounds==true

* @param reqWidth The requested width of the resulting bitmap

* @param reqHeight The requested height of the resulting bitmap

* @return The value to be used for inSampleSize

*/

public static int calculateInSampleSize(BitmapFactory.Options options,

int reqWidth, int reqHeight) {

// BEGIN_INCLUDE (calculate_sample_size)

// Raw height and width of image

final int height = options.outHeight;

final int width = options.outWidth;

int inSampleSize = 1;

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

final int halfHeight = height / 2;

final int halfWidth = width / 2;

// Calculate the largest inSampleSize value that is a power of 2 and keeps both

// height and width larger than the requested height and width.

while ((halfHeight / inSampleSize) > reqHeight

&& (halfWidth / inSampleSize) > reqWidth) {

inSampleSize *= 2;

}

// This offers some additional logic in case the image has a strange

// aspect ratio. For example, a panorama may have a much larger

// width than height. In these cases the total pixels might still

// end up being too large to fit comfortably in memory, so we should

// be more aggressive with sample down the image (=larger inSampleSize).

long totalPixels = width * height / inSampleSize;

// Anything more than 2x the requested pixels we'll sample down further

final long totalReqPixelsCap = reqWidth * reqHeight * 2;

while (totalPixels > totalReqPixelsCap) {

inSampleSize *= 2;

totalPixels /= 2;

}

}

return inSampleSize;

// END_INCLUDE (calculate_sample_size)

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值