如何在另一个画布上绘制现有画布?

发布于 2024-11-09 03:08:53 字数 1804 浏览 0 评论 0原文

现在我需要在现有的 CanvasCanvas)中绘制一个新的 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);
        }
    }
    

真实示例用法:嗯,我有一个名为 CarSpriteSpriteCarSprite 有一个名为 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 others Sprite nodes (LinkedList<Sprite> nodes);
  • If don't have nodes (nodes = null) then, will draw the (Bitmap) Sprite.image directly to parent canvas, received on update 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 new Canvas on parent Canvas. 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?

Example

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里的微风 2024-11-16 03:08:53

我做。但速度非常慢,但工作正常。

    final Bitmap split = Bitmap.createBitmap(GameActivity.screenWidth,
        GameActivity.screenHeight,
        Bitmap.Config.ARGB_8888);
    final Canvas subCanvas = new Canvas(split);

    for(final Sprite sprite: this.nodes) {
        sprite.update(subCanvas);
    }

    canvas.drawBitmap(split, this.x, this.y, this.paint);
  1. 首先我们需要一个 Canvas 对象(这里只是 canvas);
  2. 之后,我们需要制作一个用于绘制的位图。这需要从当前canvas获取宽度高度。我在这个例子中使用了一个捷径;
  3. 接下来,我们需要创建一个新的Canvas,它将传递给Sprite.nodes
  4. 毕竟,我们需要在全局画布上绘制位图;

注意:这效果很好,但对速度影响很大。

I do it. But is very slow, but works fine.

    final Bitmap split = Bitmap.createBitmap(GameActivity.screenWidth,
        GameActivity.screenHeight,
        Bitmap.Config.ARGB_8888);
    final Canvas subCanvas = new Canvas(split);

    for(final Sprite sprite: this.nodes) {
        sprite.update(subCanvas);
    }

    canvas.drawBitmap(split, this.x, this.y, this.paint);
  1. First we need have a Canvas object (here just canvas);
  2. After, we need make a Bitmap where we will draw. This need get a width and height from current canvas. I used a shortcut on this case;
  3. Next, we need make a new Canvas, it will be passed to Sprite.nodes;
  4. After all, we need draw the bitmap on global canvas;

Note: this works very well, but affect to much the speed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文