[英]How to draw on android canvas without losing previously drawn object?
In android, is it possible to draw something on canvas, Then draw something else to the same canvas without losing the previously drawn objects? 在android中,是否可以在画布上绘制某些内容,然后在不丢失先前绘制的对象的情况下在同一画布上绘制其他内容?
Details : 详细资料 :
I have gotten to a point where the first drawable is clicked & 5 more drawables are fetched. 我已经点到了第一个可绘制对象,然后单击了5个可绘制对象。 But when I translate the canvas & render the 5 nodes (invoking OnDraw), the first drawable is gone (expected).
但是,当我翻译画布并渲染5个节点(调用OnDraw)时,第一个可绘制对象消失了(预期)。 I dont want to draw all nodes again.
我不想再次绘制所有节点。
1) So how to append to the same canvas? 1)那么如何附加到同一画布上?
2) Is canvas.concat the way to go? 2)canvas.concat是要走的路吗? If so any pointers on how it could be used?
如果可以,那么如何使用它的任何指示?
My OnDraw method: 我的OnDraw方法:
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
canvas.translate(mPosX, mPosY);
canvas.scale(mScaleFactor, mScaleFactor);
this.canvas = canvas;
canvas.translate(50,50);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
drawable.draw(canvas);
canvas.restore();
}
UPDATE1 : I found that this can be achieved only with a workaround. UPDATE1 :我发现这只能通过解决方法来实现。 Basically, I need to create a bitmap/Canvas outside onDraw, draw my drawables on this new canvas & then, when done, draw this pre-drawn bitmap on the canvas supplied by onDraw.
基本上,我需要在onDraw外部创建一个位图/画布,在此新画布上绘制我的可绘制对象,然后在完成后在onDraw提供的画布上绘制该预绘制位图。 But the problem with this approach is that when I create a bitmap, I need to provide a dimension.
但是这种方法的问题在于,当我创建位图时,我需要提供一个尺寸。 But the nodes can grow in size & there is no guarantee that I can accommodate them grown-nodes within 1000x1000.
但是节点的大小可以增长,并且不能保证我可以在1000x1000以内容纳它们。 Assigning bigger numbers can result in out of memory.
分配更大的数字可能会导致内存不足。 So unless there is a way to make the bitmap dimension match the canvas (impractical right?) I need to change my approach (SurfaceView?) here... suggestions welcome.
因此,除非有一种方法可以使位图尺寸与画布匹配(不切实际,对吗?),我需要在此处更改方法(SurfaceView?)……欢迎提出建议。
mBitmap = Bitmap.createBitmap(1000, 1000, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
Did you call "canvas.drawColor( your_background_color )" before you call onDraw(Canvas canvas) function? 在调用onDraw(Canvas canvas)函数之前,您是否调用过“ canvas.drawColor( your_background_color )”? If you do that just remove that line, which will clean your canvas as the color you assigned.
如果这样做,则删除该行,这将清洁您的画布作为您分配的颜色。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.