【问题标题】:Performance in merging several PNG files during runtime and displaying the resulting Bitmap在运行时合并多个 PNG 文件并显示生成的位图的性能
【发布时间】:2017-08-30 19:55:34
【问题描述】:

我试图在运行时合并几个 png 文件并显示生成的位图。但是性能似乎很差(显示图像大约需要半秒钟)。是否有其他方法可以提高性能?

所有图像都是全屏PNG图像,其中一些图像具有不同的透明区域,因此合并的顺序很重要。

我尝试通过 decodeResource 预加载资源,但在将大约 40 个 PNG 文件加载到位图文件后,内存不足。尽管当我将 PNG 文件解码为位图时,它的大小只有 10KB,但它的大小也只有几 MB。我有数百个 PNG 文件,因此无法预加载资源,并且由于内存耗尽导致应用程序崩溃。

示例代码:

Bitmap background = decodeResource(context.getResources(), R.drawable.background);
Bitmap bmp1 = decodeResource(context.getResources(), R.drawable.png1);
Bitmap bmp2 = decodeResource(context.getResources(), R.drawable.png2);
Bitmap bmp3 = decodeResource(context.getResources(), R.drawable.png3);
Bitmap bmp4 = decodeResource(context.getResources(), R.drawable.png4);
Bitmap bmp5 = decodeResource(context.getResources(), R.drawable.png5);
Bitmap bmp6 = decodeResource(context.getResources(), R.drawable.png6);
Bitmap bmp7 = decodeResource(context.getResources(), R.drawable.png7);
Bitmap bmp8 = decodeResource(context.getResources(), R.drawable.png8);
Bitmap bmp9 = decodeResource(context.getResources(), R.drawable.png9);
Bitmap bmp10 = decodeResource(context.getResources(), R.drawable.png10);
Bitmap bmp11 = decodeResource(context.getResources(), R.drawable.png11);
Bitmap bmp12 = decodeResource(context.getResources(), R.drawable.png12);

Bitmap bmOverlay = Bitmap.createBitmap(background.getWidth(), background.getHeight(), background.getConfig());
canvas = new Canvas(bmOverlay);     
canvas.drawBitmap(background, new Matrix(), null);
canvas.drawBitmap(bmp1, 0, 0, null);
canvas.drawBitmap(bmp2, 0, 0, null);
canvas.drawBitmap(bmp3, 0, 0, null);
canvas.drawBitmap(bmp4, 0, 0, null);
canvas.drawBitmap(bmp5, 0, 0, null);
canvas.drawBitmap(bmp6, 0, 0, null);
canvas.drawBitmap(bmp7, 0, 0, null);
canvas.drawBitmap(bmp8, 0, 0, null);
canvas.drawBitmap(bmp9, 0, 0, null);
canvas.drawBitmap(bmp10, 0, 0, null);
canvas.drawBitmap(bmp11, 0, 0, null);
canvas.drawBitmap(bmp12, 0, 0, null);

Drawable drawable =new BitmapDrawable(context.getResources(),bmOverlay);
ImageView image.setImageDrawable(drawable); 

我可以使用任何其他巧妙的技术来解决这个问题吗?

【问题讨论】:

    标签: android performance merge bitmap png


    【解决方案1】:

    首先,png 和 jpeg 是图像的压缩格式,因此它们的尺寸更小,但顾名思义,位图是图像每一位的映射,其大小取决于图像的实际分辨率,因此一个 10kb 的 jpeg 可以变成 10mb 的位图。

    现在是解决方案部分,
    正如您提到的,您的 png 具有透明度并且合并的顺序很重要,您可以制作类似的方法

    public static Bitmap mergePng(Bitmap baseBitmap, Bitmap newBitmap){
        Bitmap targetBitmap; //use target bitmap to store the result of merging
        //your login to merge the newBitmap on top of base bitmap
        newBitmap.recycle;
        baseBitmap.recycle;
        return targetBitmap;
    }
    

    如果你不希望这个方法在多个类中使用,那么在你的类中将目标位图设置为全局,并将方法设置为private void,这样可以节省更多内存。

    然后在一个循环中调用这个方法,一次传递两个位图。这种方法将帮助您以您想要的顺序合并Bitmaps,一次最多有 3 个活动位图实例。

    【讨论】:

      【解决方案2】:

      不要一次从资源中创建这么多位图。开!你一个。然后画出来。并回收。然后从资源中取出下一个,绘制并回收。

      你当然应该为它做一个循环。

      您的 png 文件大小并不能说明一切。图像的分辨率决定了位图所需的内存量。

      【讨论】:

        猜你喜欢
        • 2013-04-02
        • 1970-01-01
        • 1970-01-01
        • 2012-03-17
        • 2020-04-21
        • 2011-05-26
        • 1970-01-01
        相关资源
        最近更新 更多