如何在android中绘制位图? [英] How to draw on Bitmap in android?
问题描述
我正在尝试弄清楚如何在 android 中绘制位图,并为撤消功能保留这些更改后的位图的副本.
I'm trying to figure out how to draw on a bitmap in android, and keep a copy of these changed bitmaps for an undo function.
Bitmap b = ...
Paint p = new Paint();
canvas.drawBitmap(b, new Matrix(), null);
canvas.drawCircle(0,0,20,20);
//does Bitmap b have the circle drawn on it next time?
或者如何在使用画布绘制位图后获取位图(我想保留一堆位图,其中包含画布绘制应用的更改)?也许我完全错了.
Or how do I get the bitmap after its been drawn on with the canvas(I want to preserve a stack of bitmaps with the changes applied by canvas drawing)? Maybe I'm going about this entirely wrong.
推荐答案
使用 new Canvas(Bitmap bitmap)
为 Canvas
提供一个 Bitmap
绘图操作的结果.
Use new Canvas(Bitmap bitmap)
to provide a Canvas
with a Bitmap
which will contain the result of your drawing operations.
您使用 drawBitmap
在 Canvas
上绘制的原始 Bitmap
永远不会被修改.
The original Bitmap
that you draw on your Canvas
with drawBitmap
will never be modified.
在用户完成每个操作后,您可能会:
After each operation done by the user you might:
- 在内存中保留已完成操作的列表
- 使用 Bitmap.compress 将中间结果保存到外部存储
另一种方法是使用 LayerDrawable
将连续的绘制操作堆叠在一起.您可以想象允许用户禁用每个单独的操作.
Another approach could be to use a LayerDrawable
to stack successive drawing operations on top of each other. You can imagine allowing the user to disable each individual operation done.
这篇关于如何在android中绘制位图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!