如何在另一个画布上绘制现有画布?
现在我需要在现有的 Canvas
(父 Canvas
)中绘制一个新的 Canvas
,例如 位图
的作品。
逻辑是这样的:
- 每个
Sprite
可以有其他Sprite
节点(LinkedList
);节点 如果没有节点 (
nodes = null
),则会将(Bitmap) Sprite.image
直接绘制到父画布上,在update 时接收
方法,例如(当前正在工作):public void update(Canvas canvas){ if(this.nodes == null){ canvas.drawBitmap(this.image, ...); } ... }
如果它有节点,我需要创建一个新的
Canvas
,在其上绘制节点位图这个,并在父级上绘制新的Canvas
画布
。这不起作用,但想法是这样的:public void update(Canvas canvas){ ... else { // this.nodes != null 画布 newCanvas = new Canvas(); for(精灵节点: this.nodes){ 节点.update(newCanvas); } // 画布像位图一样工作! (?) canvas.drawCanvas(newCanvas, this.x, this.y, this.paint); } }
真实示例用法:嗯,我有一个名为 CarSprite
的 Sprite
。 CarSprite
有一个名为 WheelSprite extends Sprite
的私有类。 CarSprite
实现 WheelSprite
的两个节点(二维示例),它们将位于 CarSprite
中以看起来汽车的轮子。到目前为止,没有问题。
但如果CarSprite
受到关卡中恶魔一物体的影响,这将变成透明。例如,我将执行objectOfCarSprite.paint.setAlpha(127);
。问题是,如果我这样做(当前工作模式),只有汽车位图会变成透明,但井不会,因为它直接绘制到全局画布
对象。
这部分影响不大:目前,我正在获取父级(parentN...)Paint
对象alpha,并通过一些数学运算,我得到了一个透明的解决方案车轮的工作效果几乎与我需要的一样好,但是,例如,如果 WheelSprite
对象位于汽车位图上,则可以看到汽车的地面,就像图片一样。
是否有更好的方式来做我想要的,或者只是目前喜欢的工作?
Now I need to draw a new Canvas
in an existent Canvas
(the parent Canvas
), like Bitmap
's works.
The logic is this:
- Each
Sprite
can have othersSprite
nodes (LinkedList<Sprite> nodes
); If don't have nodes (
nodes = null
) then, will draw the(Bitmap) Sprite.image
directly to parent canvas, received onupdate
method, like (currently working):public void update(Canvas canvas){ if(this.nodes == null){ canvas.drawBitmap(this.image, ...); } ... }
If it have nodes, I need create a new
Canvas
, draw nodes bitmap on this, and draw the newCanvas
on parentCanvas
. This don't works, but the idea is like this:public void update(Canvas canvas){ ... else { // this.nodes != null Canvas newCanvas = new Canvas(); for(Sprite node: this.nodes){ node.update(newCanvas); } // Canvas working like Bitmap! (?) canvas.drawCanvas(newCanvas, this.x, this.y, this.paint); } }
Real example usage: well, I have a Sprite
called CarSprite
. The CarSprite
have a private class called WheelSprite extends Sprite
. The CarSprite
implements two nodes (two dimensional example) of WheelSprite
, that will be positioned in the CarSprite
to seems the whell of car. So far, no problem.
But if CarSprite
is affected by a devil one object in the level, this will be turn transparent. I will do, for instance, objectOfCarSprite.paint.setAlpha(127);
. The problem is, if I do it (currently working mode), only the car bitmap will turn transparent, but not the well, because it is drawed directly to global Canvas
object.
This part does not influence much: currently, I'm getting the parent (of parentN...) Paint
object alpha, and with some math, I get a transparent solution to wheel that works almost as well I need, but if, for instance, the WheelSprite
object is over car bitmap, is possible to see the ground of car, like picture.
Exists a better way to do like I want, or only like work currently?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我做。但速度非常慢,但工作正常。
Canvas
对象(这里只是canvas
);canvas
获取宽度
和高度
。我在这个例子中使用了一个捷径;Canvas
,它将传递给Sprite.nodes
;注意:这效果很好,但对速度影响很大。
I do it. But is very slow, but works fine.
Canvas
object (here justcanvas
);Bitmap
where we will draw. This need get awidth
andheight
from currentcanvas
. I used a shortcut on this case;Canvas
, it will be passed toSprite.nodes
;Note: this works very well, but affect to much the speed.