【问题标题】:how do I create a huge, white bitmap with Canvas?如何使用 Canvas 创建一个巨大的白色位图?
【发布时间】:2012-03-05 23:32:48
【问题描述】:

我正试图弄清楚如何使用 Canvas 在一个大的白色表面上绘制一个小图形(实际上并不重要)。问题是,如果我从一个大的空位图开始,当我使用 ARGB_8888 制作它的可变副本时,Android 会立即耗尽内存。我很好奇我是否遗漏了什么,或者由于 Android 中的内存限制,实际上无法将小图形合成到大的白色表面上并将其保存为 PNG 或 JPG。

【问题讨论】:

  • 根据您提供的信息,最简单的优化是不使用 ARGB_8888 而只使用 RGB_888,因为听起来您的图像是一种不透明的颜料。因此,这将为您的图像的每个像素节省一个字节。也许您可以提供您用来绘制图像的代码,然后我们可以帮助您更好地回答您的问题?
  • 这是一种不透明的涂料,但这不是我的问题。我的愿望是将画布的内容转储到 *new 位图,而不是重新使用以前的位图。有没有办法将画布写入字节数组?还是我注定总是要从一个预先存在的位图开始绘制?

标签: android memory-management bitmap


【解决方案1】:

当然,当您想要创建巨大的位图时,您会受到内存的限制,但您有足够的内存来创建相当大的位图。例如,一个 1024*1024 ARGB_8888 位图将需要大约 4 MB 的内存,如果您的应用程序一般都节省内存,这不是问题。 Android 应用程序的正常堆大小通常在 16-32 MB 之间,具体取决于 Android 版本,这只是为了让您对所玩的游戏有所了解。

您说您制作了大型位图的副本,这可能是您的主要问题。无需复制大位图,您只需要一个。这是一个示例项目,它创建一个大的 (1024*1024) 白色位图并在您的应用程序中绘制一个视图,然后将结果写入 PNG:

package com.example.android;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class WhitePngActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        findViewById(R.id.draw_to_bitmap).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Bitmap largeWhiteBitmap = Bitmap.createBitmap(1024, 1024, Bitmap.Config.ARGB_8888);

                // Make a canvas with which we can draw to the bitmap
                Canvas canvas = new Canvas(largeWhiteBitmap);

                // Fill with white
                canvas.drawColor(0xffffffff);

                // Draw the view to the middle of the big white bitmap. In this
                // case, it will be the button, but you can draw any View in
                // your view hierarchy to the bitmap like this. And of course
                // you can position the View anywhere you want
                canvas.save();
                canvas.translate(
                        largeWhiteBitmap.getWidth() / 2 - view.getWidth() / 2,
                        largeWhiteBitmap.getHeight() / 2 - view.getHeight() / 2);
                view.draw(canvas);
                canvas.restore();

                // Write the file (don't forget android.permission.WRITE_EXTERNAL_STORAGE)
                File pictureDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
                File pngFile = new File(pictureDir, "big-white-image-with-view.png");
                try {
                    largeWhiteBitmap.compress(Bitmap.CompressFormat.PNG, 0, new FileOutputStream(pngFile));
                } catch (FileNotFoundException e) {
                    Log.e("WhitePngActivity", "Could not write " + pngFile, e);
                }

                // Immediately release the bitmap memory to avoid OutOfMemory exception
                largeWhiteBitmap.recycle();
            }
        });
    }
}

连同这个主布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/draw_to_bitmap"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Click to draw to bitmap" />

</LinearLayout>

你会得到一个像 /mnt/sdcard/Pictures/big-white-image-with-view.png 这样的位图,看起来像这样:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-28
    • 1970-01-01
    • 2015-09-29
    • 1970-01-01
    • 2010-11-28
    • 1970-01-01
    • 2019-12-25
    相关资源
    最近更新 更多