Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 缩小大图像的比例_Android_Bitmap - Fatal编程技术网

Android 缩小大图像的比例

Android 缩小大图像的比例,android,bitmap,Android,Bitmap,我下面的链接加载大图像。因此,根据这个链接,我需要确定一个目标的高度和宽度,即目标分辨率和图像分辨率不应超过这个目标分辨率,如果它超过,那么我需要缩小它。因此,根据函数计算样本大小: public static int calculateInSampleSize( BitmapFactory.Options options, int reqWidth, int reqHeight) { // Raw height and width of image final int heig

我下面的链接加载大图像。因此,根据这个链接,我需要确定一个目标的高度和宽度,即目标分辨率和图像分辨率不应超过这个目标分辨率,如果它超过,那么我需要缩小它。因此,根据函数计算样本大小:

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) {

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

return inSampleSize;
}

inSampleSize使用半高半宽计算。我的问题是,为什么我们要以半高和haldf宽度为基础进行计算,为什么不以高度和宽度为基础进行如下计算:

while ((height / inSampleSize) >= reqHeight
            && (width / inSampleSize) >= reqWidth) {
        inSampleSize *= 2;
    }
编辑: 我想我的问题被误解了。我想问一下,在上面的示例中,为什么我们不将reqHeight和reqWidth与图像的全高和全宽进行比较。为什么Android开发者页面在上面的示例中使用半宽度和半图像???

您可以在这里找到,在中,当采样发生时,它返回的图像是原始图像宽度/高度的1/2^n。因此,采样图像取决于采样的高度和宽度


我们对高度和宽度的一半进行处理,当我们将高度和宽度除以2时,就像我们用2进行采样一样,当图像的这一半小于要求的高度和宽度时,我们不需要对原始图像进行采样,就像我们这样做一样,采样的图像将被像素化为“模糊”.

如果以纵向模式显示,您可以根据宽度缩小大图像。@RahulSharma我想您没有明白我的问题,我想在上面的示例中问一下,为什么我们在计算inSampleSize时没有使用全宽和全高???@RahulSharma请参见我的编辑。哦,对不起,这会导致图像模糊。好的,举个例子,我的目标宽度和高度各为300。现在让我们假设我有一个图像,其宽度和高度各为400。所以一半人每人200英镑。因此,calculate inSampleSize将返回1,并且将加载到内存中的图像将具有比目标图像更高的分辨率400*400。所以这是不可取的。默认情况下,Android会缩小您的图像以适应ImageView,请看这里