简体   繁体   English

如何在android canvas上绘制而不丢失先前绘制的对象?

[英]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 : 详细资料

  • Display a drawable (based on image url). 显示一个可绘制对象(基于图像URL)。
  • Upon clicking a node, display 5 more nodes (which drawables to display is determined by a remote api call). 单击一个节点后,再显示5个节点(要显示的可绘制对象由远程api调用确定)。
  • Now clicking on any of the 5 nodes will follow same steps to display 5 more nodes & so on. 现在,单击5个节点中的任何一个将遵循相同的步骤以显示5个以上的节点,依此类推。

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.

相关问题 在画布上绘制而不清除先前绘制的位图 - Draw on canvas without making previously drawn bitmap cleared 如何在重绘时保存先前绘制到Canvas的对象? - How to save objects previously drawn to the Canvas on a redraw? Android:不擦除先前绘制的顺序绘制 - Android: Sequential Drawing Without Erasing Previously Drawn 如何在已经绘制的 canvas 上绘制 bitmap - How to draw bitmap over already drawn canvas Android如何在没有视图的画布上绘制 - Android how to draw on a canvas without a view 如何通过在Android中指定x和y坐标在画布上绘制的位图图像上绘制点? - How to Draw a point on over a bitmap image drawn on a canvas by specifying the x and y co-ordinate in android? 如何在画布上使用Android的绘制路径在绘制的路径内裁剪图像 - How to crop image inside the path drawn using draw path on canvas,Android 我可以在Android画布上绘制pi,看起来像是用drawText绘制的吗? - Can I draw pi on an android canvas to look like it was drawn with drawText? Android,画布:如何在不套印的情况下绘制透明圆圈? - Android, canvas: How can I draw transparent circle without overprinting? 如何在Android中旋转画布上绘制的矩形? - How to rotate a rectangle drawn on canvas in Android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM