Android的位图重用 [英] Android bitmaps reuse

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

问题描述

我在Android的一个新手! 我有一些巨大的问题,Android的SDK,因为我正在开发使用大量位图的应用程序(> 1200 * 1000),问题是应用程序,我需要多层时/同样大小的位图(其中3-5) 。问题是内存堆被消耗得很快这些分配之一,内存溢出的异常被触发。

I am a newbie in Android ! I have some huge problems with Android's SDK, as I'm developing an app that uses huge bitmaps (> 1200 * 1000) and the problem is during the app I need multiple layers/bitmaps of the same size (3-5 of them). The problem is memory heap is consumed very quickly on one of these allocations and OutOfMemory exception is triggered.

好部分是,我不需要在同一时间所有的位图。

The good part is that I don't need all of the bitmaps in the same time.

我试图回收()方法调用,虽然我知道这是过时的,作为位图的内存现在位于堆,而不是在本机内存。 我也试图从一个特定的位图中删除所有引用,甚至调用System.gc()手动,但没有用,该位图无法删除。 好吧,也许就不会有内存泄漏,我不知道,但我对此表示怀疑。

I tried the recycle() method calling, although I understood it's obsolete, as the memory for the bitmap now resides on the heap, not on the native memory. I also tried to remove all references from a specific bitmap and even call System.gc() manually but no use, the bitmap couldn't be removed. Ok, maybe there would be memory leaks I don't know about, although I doubt it.

我尝试各种解决方案,其中没有工作。

I tried all sorts of solutions and none of them work.

对我来说,最好的选择将是重复使用位图,但BitmapFactory的德code方法并不只知道分配一个新的位图消耗的内存。

The best choice for me would have been to reuse a bitmap, but the decode method of BitmapFactory does only know to allocate a new Bitmap which consumes memory.

我的问题是,你知道重用位图像素(覆盖位图的像素不分配另一个位/像素阵列)可以与Android合作的方法???问题是我不能驻留在GarbageCollector解决方案,因为我有我的应用程序特定的时刻,当我要肯定这些位图从内存/其他位图将不会被分配删除。

My question is, do you know a method of reusing the bitmaps pixels (overwrite a bitmap's pixels without allocating another Bitmap/pixels array) that can work with Android ??? the problem is I can't reside on a GarbageCollector solution because I have specific moments in my app when I have to be certain those bitmaps are removed from memory/other bitmaps won't be allocated.

另一种解决办法是:你知不知道画一些位图直接在SD卡,而不通过堆内存的方法是什么?在瞬间崩溃,当我尝试分配一个很大的位图(双原大小)来连接两个处理位图)...这就是为什么我问。难道还有一种可能性,直接从数据流中实例化画布r其他图形对象?

Another solution would be: do you know a method of drawing some bitmaps directly on sdcard, without passing through the heap memory? On of the moments it crashes it when I try to allocate a big bitmap (double of the original size) to concatenate two other processed bitmaps) ... that's why I'm asking. Could there be a possibility to instantiate a Canvas r other drawing object directly from a data stream ?

其他的解决办法是利用类似虚拟交换内存这将模拟堆内存中,但与缓存的SD卡什么的。但找不到这样做也是一种方式。

Other solution would be to make use of something like virtual swap memory which would simulate heap memory but with caching on SDCARD or something. But can't find a way on doing this also.

推荐答案

写一个单独的类进行自定义位图操作:

Write a separate class for performing Custom Bitmap operations:

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;


class BitmapLoader
{
public static int getScale(int originalWidth,int originalHeight,
   final int requiredWidth,final int requiredHeight)
{
    //a scale of 1 means the original dimensions 
    //of the image are maintained
    int scale=1;

    //calculate scale only if the height or width of 
    //the image exceeds the required value. 
    if((originalWidth>requiredWidth) || (originalHeight>requiredHeight)) 
    {
        //calculate scale with respect to
        //the smaller dimension
        if(originalWidth<originalHeight)
            scale=Math.round((float)originalWidth/requiredWidth);
        else
          scale=Math.round((float)originalHeight/requiredHeight);

    }

    return scale;
}

public static BitmapFactory.Options getOptions(String filePath,
        int requiredWidth,int requiredHeight)
{

    BitmapFactory.Options options=new BitmapFactory.Options();
    //setting inJustDecodeBounds to true
    //ensures that we are able to measure
    //the dimensions of the image,without
    //actually allocating it memory
    options.inJustDecodeBounds=true;

    //decode the file for measurement
    BitmapFactory.decodeFile(filePath,options);

    //obtain the inSampleSize for loading a 
    //scaled down version of the image.
    //options.outWidth and options.outHeight 
    //are the measured dimensions of the 
    //original image
    options.inSampleSize=getScale(options.outWidth,
            options.outHeight, requiredWidth, requiredHeight);

    //set inJustDecodeBounds to false again
    //so that we can now actually allocate the
    //bitmap some memory
    options.inJustDecodeBounds=false;

    return options;

}



public static Bitmap loadBitmap(String filePath,
        int requiredWidth,int requiredHeight){


    BitmapFactory.Options options= getOptions(filePath,
            requiredWidth, requiredHeight);

    return BitmapFactory.decodeFile(filePath,options);
}
}

然后从你的活动,呼吁位图reqBitmap = loadBitmap(字符串文件路径,诠释requiredWidth,INT requiredHeight)此类方法提供的文件路径,以及requiredWidth和requiredHeight设置为你希望缩放位图的尺寸。现在,使用 reqBitmap

Then from your activity, call Bitmap reqBitmap = loadBitmap(String filePath,int requiredWidth,int requiredHeight) method of this class providing the filepath of the bitmap obtained from the sd card, and setting the requiredWidth and requiredHeight to the dimensions you wish to scale the bitmap to. Now use the reqBitmap.

此方式大的位图将被按比例缩小之前它们被装载到堆存储器。我有同样的问题,这解决了我。 :)

This way large bitmaps will be scaled down before they are loaded into the heap memory. I had the same problem and this solved it for me. :)

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

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