【发布时间】:2014-04-15 14:45:20
【问题描述】:
在 Android 培训文档中,article 讨论了如何有效加载大型位图,其中讨论了计算 inSampleSize 以在加载图像时对其进行下采样。这是共享的代码示例。
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;
}
对我来说不太有意义的是在这里使用halfHeight 和halfWidth。让我们通过一个真实世界的例子来说明我的意思。
我想将用户的照片加载到 OpenGL 纹理中。我已经查询发现GL_MAX_TEXTURE_SIZE是4096。用户选择了一张4320x2432的照片,所以我需要把它缩小一点。
我调用了文档中提供的静态辅助方法:
options.inSampleSize = BitmapUtils.calculateInSampleSize(options, maxTextureSize, maxTextureSize);
单步执行此代码,halfHeight 将是 1216,halfWidth 将是 2160,如果该值除以 inSampleSize 仍大于请求的维度,则 inSampleSize 只会不是 1。
当我运行此设置时,inSampleSize 设置为 1,这根本不会缩小图像,并且 OpenGL 会抛出一个拟合,因为它大于 GL_MAX_TEXTURE_SIZE。
我的问题是为什么我们在这里除以二?我不在乎我的图像的一半是否适合要求的尺寸,我希望整个图像适合。只要(halfHeight / inSampleSize) > reqHeight 和(halfWidth / inSampleSize) > reqWidth 不断碰撞inSampleSize 不是更有意义吗?
【问题讨论】:
标签: android graphics opengl-es bitmap