- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试弄清楚如何使用 Canvas 将一个小图形(它是什么并不重要)绘制到一个大的白色表面上。问题是,如果我从一个大的空位图开始,当我使用 ARGB_8888 制作它的可变副本时,Android 会立即耗尽内存。我很好奇我是否遗漏了什么,或者由于 Android 中的内存限制实际上不可能将小图形合成到大的白色表面上并将其保存为 PNG 或 JPG。
最佳答案
当然,当您想要创建巨大的位图时,您会受到内存的限制,但是您有足够的内存来创建相当大的位图。例如,一个 1024*1024 ARGB_8888 位图将需要大约 4 MB 的内存,如果您的应用程序通常内存不足,这不是问题。 Android 应用程序的正常堆大小通常在 16-32 MB 之间,具体取决于 Android 版本,只是为了让您了解您必须使用的内容。
你说你制作了一个大位图的副本,这可能是你的主要问题。无需复制大位图,您只需要一个。下面是一个示例项目,它创建一个大的 (1024*1024) 白色位图并在您的应用中在其中间绘制一个 View ,然后将结果写入 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
的地方得到一个位图,看起来像这样:
关于android - 如何使用 Canvas 创建一个巨大的白色位图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9279600/
我通过在共享首选项中使用 GSON 将其转换为 json 来存储我的复杂对象。但是在检索它时,无法获得预期的字符串。 代码 这里 holderListCustomizationMap 是对象的复杂映射
因此,我正在尝试对大于可用RAM的gz压缩文件执行某种面向行的操作,因此排除了将其首先读取为字符串的情况。问题是,如何在rust(缺少gunzip file.gz|./my-rust-program)
我试图更好地理解为什么具有潜在大精度的大数字处理不一致,特别是在 JavaScript 及其本地化工具(例如 ECMA-402/Intl)中。我假设这与 float 的使用有关,但我想了解限制在哪里和
我们有一个 5GB 的 csv 文件,这是我们业务的主列表。 有多个类别,每个类别包含数千条记录。我们的目标是将每个类别导出为其自己的 csv 文件。 我们如何运行查询并导出数据? 运行 OSX。有没
基于上一个问题 ( see here ),我试图通过 xmlEventParse 读取许多大型 xml 文件,同时保存节点变化数据。使用此示例 xml:https://www.nlm.nih.gov/
我正在开发一个系统,它加载一个巨大的 CSV 文件(超过 100 万行)并保存到数据库中。每行也有超过一千个字段。 CSV 文件被视为一个批处理,每一行都被视为其子对象。在添加对象的过程中,每个对象都
借助node-google模块 我编写了一个简单的 Node 模块来为我的网络应用程序启用“文本网络搜索”功能,并在我的一个 View 中显示结果。 由于在来自同一 IP 的少量查询后 Google
我有相当大的 4D 阵列 [20x20x40x15000],我使用 h5py 将其作为 HDF5 文件保存到磁盘.现在的问题是我想计算整个数组的平均值,即使用: numpy.average(HDF5_
我在遗留代码库中连接巨大的 CString 时遇到问题。 CStrings 可以包含 base64 编码的文件,因此可能很大。在某些时候,这些 CString 会像这样连接起来: result +=
我正在尝试让我的服务器提供来自另一台服务器的巨大文件。但是,为了保护我的凭据免受该远程服务器的攻击,我不能简单地将请求者重定向到文件 url;另一方面,虽然使用 StreamingHttpRespon
感谢对此的任何见解,我有 2 个问题: 1) 弄清楚为什么我的本地数据库 oplog 庞大且不断增长 2) 安全删除(或重置)我的 local.oplog 以释放 18 GB 的浪费空间 场景:我一直
我的预期任务:获取大量数据(1 GB 及更多大小)json 字符串,操作(进行一些格式化、解析 json、重组 json 数据)并写入新格式化的 json 字符串作为响应。处理这种情况的更好方法是什么
我做了一个小的 Angular 4 应用程序,但我不知道如何应用 tree shaking 和 aot 编译。我运行的命令如下: ng build --prod --aot 但我得到的结果仍然很大,供
我是一名优秀的程序员,十分优秀!