Android:向非矩形项目添加位图纹理

发布于 2024-09-25 16:22:14 字数 306 浏览 3 评论 0原文

我有一个如下所示的小部件:

alt text

每个圆锥体都是一个“可触摸元素”。现在我想在每个锥体上方放置一个位图纹理。然而,位图图像都是矩形的,因此一个锥体上方的位图纹理会干扰另一锥体上方的位图纹理。

我想知道这种方法的最佳解决方案是什么。我是否应该创建一个正好适合圆锥体上方的图像(作为矩形)并使未使用的区域透明?

第二个问题是,位图纹理如何与拉伸一起使用?因为整个圆圈会自行绘制以适应整个屏幕尺寸,而位图纹理几乎只有一种尺寸。

I have a widget which looks like this:

alt text

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 技术交流群。

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

发布评论

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

评论(1

护你周全 2024-10-02 16:22:14

我想不出比您自己使用透明区域的建议更好的方法来在这些锥体上绘制位图。

不过,我可以帮助您解决第二个问题,因为拉伸位图并不难。 Canvas 类中有几个选项。例如:

canvas.save();
canvas.scale(xRatio, yRatio);
canvas.drawBitmap(....);
canvas.restore();

您还可以使用矩阵,使用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:

canvas.save();
canvas.scale(xRatio, yRatio);
canvas.drawBitmap(....);
canvas.restore();

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 your canvas.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!

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