【发布时间】:2016-08-25 08:12:01
【问题描述】:
我需要创建和保存单色 PNG 图像(用单色填充的位图)。
我正在创建位图:
public static Bitmap createColorSwatchBitmap(int width, int height, int color) {
final Bitmap colorBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
colorBitmap.eraseColor(color);
return colorBitmap;
}
并将其保存到设备存储上的文件中:
stream = new FileOutputStream(filePath);
success = bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
如果我创建一个 1200x1200 位图,则内存消耗为 5,760,000 字节 (5.76 MB),由 bitmap.getAllocationByteCount() 报告。但是 PNG 文件大小只有 8,493 字节。
为一个只有 8 KB 的文件分配将近 6 MB 的内存似乎太过分了。
还有更好的方法吗?
【问题讨论】:
-
为什么?几乎可以肯定,使用 ColorDrawable 会更好。
-
@GabeSechan 你可以直接将 ColorDrawable 写入 PNG 文件,而不在内存中分配位图吗?