Android:向非矩形项目添加位图纹理
我有一个如下所示的小部件:
每个圆锥体都是一个“可触摸元素”。现在我想在每个锥体上方放置一个位图纹理。然而,位图图像都是矩形的,因此一个锥体上方的位图纹理会干扰另一锥体上方的位图纹理。
我想知道这种方法的最佳解决方案是什么。我是否应该创建一个正好适合圆锥体上方的图像(作为矩形)并使未使用的区域透明?
第二个问题是,位图纹理如何与拉伸一起使用?因为整个圆圈会自行绘制以适应整个屏幕尺寸,而位图纹理几乎只有一种尺寸。
I have a widget which looks like this:
Every cone is a "touchable element". Now I want to put a bitmap texture above each cone. However, bitmap images are all rectangular, so a bitmap texture above one cone would interfere with the bitmap texture above another cone.
I'm wondering what is the best solution to this approach. Should I just create an image which fits (as a rectangle) exactly above the cone and make the non used areas transparent?
A second question is, how do bitmap textures work with stretching? Because this whole circle draws itself to fit the whole screen size, while bitmap textures are pretty much one size.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想不出比您自己使用透明区域的建议更好的方法来在这些锥体上绘制位图。
不过,我可以帮助您解决第二个问题,因为拉伸位图并不难。 Canvas 类中有几个选项。例如:
您还可以使用矩阵,使用
matrix.postScale(xRatio, yRatio)
,然后生成更大的位图并正常绘制它,或者将矩阵传递到您的画布中.drawBitmap(....)
命令使其在绘制时缩放。所有这些方法都假设您可以访问绘图画布本身。如果您使用的是视图,则可以对其进行子类化并重写
onDraw(Canvas canvas)
方法,以在开始绘制画布之前抓取画布。如果您使用的是 SurfaceHolder,那么您应该已经知道如何获取画布。编辑:我忘记了我要描述的第三种方法。您还可以使用canvas.drawBitmap(bitmap,srcRect,dstRect,paint)使画布缩放位图以适合目标矩形。简而言之,有很多方法可以做到这一点 - 根据您的应用选择最简单的一种!
Offhand I can't think of a better way to draw bitmaps over those cones than your own suggestion of using transparent zones.
However, I can help with your second question as stretching bitmaps is not hard. You've got a few options in the Canvas class. For example:
You can also use a Matrix, using
matrix.postScale(xRatio, yRatio)
, to then generate either a larger bitmap and draw it normally, or pass the matrix in to yourcanvas.drawBitmap(....)
command to make it scale while it draws.All of these methods assume you have access to the drawing canvas itself. If you are using a view, you can subclass it and override the
onDraw(Canvas canvas)
method to grab the canvas before it starts drawing it. If you're using a SurfaceHolder, then you should already know how to get the canvas.Edit: I forgot the third method I was going to describe. You can use the
canvas.drawBitmap(bitmap, srcRect, dstRect, paint)
to also make the canvas scale the bitmap to fit the destination rectangle. In short, there are lots of methods to do this - pick the one that's easiest based on your application!