Android画布 - 画一个洞 [英] Android canvas - Draw a hole

查看:209
本文介绍了Android画布 - 画一个洞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在Android中使用canvas实现以下图片?

Is it possible to realize the following picture in Android with canvas?

我想要一个洞,而不仅仅是一个圆形的红色层是黄色的。我在 onDraw() -Method:

I want to have a hole and not only a Circle over the red layer which is yellow colored. I tried this (and failed) with the following Code in my onDraw()-Method:

canvas.drawBitmap(yellow, 0, 0, paint);
canvas.drawBitmap(red, 0, 200, paint);
Paint p = new Paint();
p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
canvas.drawCircle(300, 300, radius, p);

但是当我使用这个代码时,它通过两个位图形成一个洞。最后,这个应用程序应该是一个有球,孔和其他东西的迷宫。当球落入一个洞时,它应该出现在红色位图下。是否有可能实现这一点?

But when I use this code, it makes a hole through both bitmap's. At the end, this App should be a Maze with a ball, holes and other stuff. When the ball would fall into a hole it should appear under the red-Bitmap. Is it possible to realize this?

回答:

同样的问题:使用查看而不是SurfaceView。这是我的错,因为SurfaceView的bg不能设置透明。

If someone should have the same problem: use View and not SurfaceView. That was my fault, because the bg of a SurfaceView could not be set transparent.

推荐答案

我认为你误解了canvas / bitmaps的工作原理。没有层或对象存储(除非你存储它们)。它只是显示的图像的一个像素一个像素表示。

I think you're misunderstanding how the canvas/bitmaps work. There aren't layers or objects stored (unless you store them). It's just a pixel by pixel representation of the image displayed. A yellow circle over a red square is what you have shown in the above picture.

如果你真的想要一个红色的图层,你必须复合两个位图。在一个位图中绘制红色方块上的孔,在一个位图中绘制黄色层。在画布上,绘制黄色位图,然后在顶部绘制红色正方形与孔位图。

If you truly want a red layer, you have to composite two bitmaps. Draw the hole over the red square in one bitmap, draw the yellow layer in one bitmap. On the canvas, draw the yellow bitmap, then the "red square with a hole" bitmap on top.

这篇关于Android画布 - 画一个洞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆