解读Android中的位图的大小合适 [英] Decoding bitmaps in Android with the right size

查看:132
本文介绍了解读Android中的位图的大小合适的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我去code使用SD卡位图 BitmapFactory.de codeFILE 。有时,位图是大于哪些应用需求或堆允许的话,所以我用 BitmapFactory.Options.inSampleSize 来申请二次采样(较小)的位图。

现在的问题是,该平台没有强制inSampleSize的精确值,而且有时我结束了一个位图要么太小,否则仍然过大,可用内存。

从<一个href="http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inSampleSize">http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inSampleSize:

  

注:德codeR会尽量满足   这一要求,但由此产生的位图   可以具有不同的尺寸即   precisely什么已要求。   此外,2的幂往往   更快/更容易去codeR到   荣誉。

我应该如何去code从SD卡位图来获得我所需要的确切大小的位图同时消耗尽可能少的内存,尽可能去code呢?

编辑:

当前源$ C ​​$ C:

  BitmapFactory.Options边界=新BitmapFactory.Options();
this.bounds.inJustDe codeBounds = TRUE;
BitmapFactory.de codeFILE(文件路径,边界);
如果(bounds.outWidth == -1){// TODO:错误}
INT宽度= bounds.outWidth;
INT高= bounds.outHeight;
布尔withinBounds =宽度LT; =&了maxWidth功放;&安培;高度&LT; =了maxHeight;
如果(!withinBounds){
    INT newWidth = calculateNewWidth(INT宽度,高度INT);
    浮动sampleSizeF =(浮点)宽/(浮点)newWidth;
    INT的采样大小= Math.round(sampleSizeF);
    BitmapFactory.Options重新取样=新BitmapFactory.Options();
    resample.inSampleSize =采样大小;
    位= BitmapFactory.de codeFILE(文件路径,重采样);
}
 

解决方案

你是在正确的轨道上,但你正在尝试做两件事情:读取文件,并将其扩展到合适的大小。

第一步是读取文件到一个位图比你需要稍微大一些,用BitmapFactory.Options.inSampleSize以确保您不会占用过多的内存读取大位图时,所有你想要的是一个更小的缩略图或屏幕分辨率图像。

第二步是调用Bitmap.createScaledBitmap()来创建一个新的位图到您所需要的精确的分辨率。

请确保您的临时位图后,清理回收它的内存。 (要么让变量超出范围,让与它的GC的交易,或拨打.recycle()就可以了,如果要加载大量的图片,并在内存中运行的紧。)

I decode bitmaps from the SD card using BitmapFactory.decodeFile. Sometimes the bitmaps are bigger than what the application needs or that the heap allows, so I use BitmapFactory.Options.inSampleSize to request a subsampled (smaller) bitmap.

The problem is that the platform does not enforce the exact value of inSampleSize, and I sometimes end up with a bitmap either too small, or still too big for the available memory.

From http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inSampleSize:

Note: the decoder will try to fulfill this request, but the resulting bitmap may have different dimensions that precisely what has been requested. Also, powers of 2 are often faster/easier for the decoder to honor.

How should I decode bitmaps from the SD card to get a bitmap of the exact size I need while consuming as little memory as possible to decode it?

Edit:

Current source code:

BitmapFactory.Options bounds = new BitmapFactory.Options();
this.bounds.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, bounds);
if (bounds.outWidth == -1) { // TODO: Error }
int width = bounds.outWidth;
int height = bounds.outHeight;
boolean withinBounds = width <= maxWidth && height <= maxHeight;
if (!withinBounds) {
    int newWidth = calculateNewWidth(int width, int height);
    float sampleSizeF = (float) width / (float) newWidth;
    int sampleSize = Math.round(sampleSizeF);
    BitmapFactory.Options resample = new BitmapFactory.Options();
    resample.inSampleSize = sampleSize;
    bitmap = BitmapFactory.decodeFile(filePath, resample);
}

解决方案

You are on the right track, however you are trying to do two things at once: read the file in and scale it to the appropriate size.

The first step is to read the file to a Bitmap slightly bigger than you require, using BitmapFactory.Options.inSampleSize to ensure that you do not consume excessive memory reading a large bitmap when all you want is a smaller thumbnail or screen resolution image.

The second step is to call Bitmap.createScaledBitmap() to create a new bitmap to the exact resolution you require.

Make sure you clean up after the temporary bitmap to reclaim its memory. (Either let the variable go out of scope and let the GC deal with it, or call .recycle() on it if you are loading lots of images and are running tight on memory.)

这篇关于解读Android中的位图的大小合适的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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