问题描述
是否有可能实现在Android中下面的图片用帆布?
Is it possible to realize the following picture in Android with canvas?
我想有一个洞,不仅在红层是黄色圆。
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);
但是,当我用这个code,它使得通过两个位图的一个洞。最后,这个应用程序应该是一个球,孔和其他的东西一个迷宫。当球会陷入一个洞,应该出现在红位图下。是否有可能实现吗?
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.
推荐答案
我想你误会了帆布/位图是如何工作的。有没有存储层或对象(除非你存储它们)。这只是一个所显示的图像的像素再presentation像素。在一个红色正方形的黄色圆圈的是的你已经显示在上面的图片。
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的画布上 - 画一个洞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!