在 Android 中绘制一个漂浮在另一个图像上的图像

发布于 2024-12-04 20:16:48 字数 212 浏览 1 评论 0原文

我希望能够动态地将图像放置在我的应用程序中的另一个图像上。 将第一张图像视为背景,其他图像位于顶层,我还需要通过代码移动这些顶层图像(更改屏幕上的 x 和 y)。

想象一下,例如,在一片海洋中,用户放置了鱼和海洋动物,然后这些海洋动物开始在屏幕上到处移动:就像那样。

我该怎么做?如果您不知道但记得有任何简单的程序或演示可以做到这一点,也将非常受欢迎!

谢谢你!

I want to be able to dynamically place image(s) over another image in my app.
Consider the first image as background and the other images to be on top level, I will also need to move those top level images (change their x and y on the screen) by code too.

Imagine, for example, a sea in which the user places fish and sea animals, then those sea animals start to move here and there on the screen: it will be like that.

How can I do this? If you don't know but remember any simple program or demo that does that, it will be also very welcome!

Thank you!

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

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

发布评论

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

评论(1

尛丟丟 2024-12-11 20:16:48

当然,有不止一种方法可以做到这一点,但我想说最好的方法是创建一个自定义 View (从 View 派生的类) code>) 并让它处理您的位图绘制和所有触摸事件。

需要编写大量代码来加载 Bitmap 并跟踪它们的所有位置(然后在 onDraw 中将它们绘制到画布上),但是如果您开始非常小,只允许在屏幕上绘制和拖动一张图像,您可以在此基础上进行构建并使代码保持井井有条。

您需要在自定义View 中重写onDraw(Canvas)onTouchEvent(MotionEvent)。您将使用 BitmapFactory 加载位图(如果您将图像作为资源包含在项目中,则使用 decodeResource 方法),并且您需要记住调用 当您不再使用位图时,回收它们。

onDraw 中,您可以使用 Canvas.drawBitmap 将位图绘制到画布上的特定位置。您可以选择此方法的两种重载,一种将位图的顶部和左侧坐标视为浮点(因此不执行缩放或拉伸),另一种将目标和源作为参数矩形来执行缩放、拉伸和放置。

我总是使用后者,因为它可以让我更好地控制。如果您选择此路线,您将需要为每个绘制的图像保留两个 Rect 实例和 Bitmap 实例,在触摸事件中更新它们并将它们绘制到绘图事件中的画布。

当视图内部发生某些变化时(如触摸事件的情况),调用 invalidate() 方法,框架将知道重绘触发您的 onDraw 方法的所有内容。

There is, of course, more than one way to do this, but I would say that the best way to do it would be to create a custom View (class that derives from View) and have this handle your bitmap drawing and all of your touch events.

There's a lot of code to write for loading the Bitmaps and keeping track of all of their positions (and then drawing them to the canvas in onDraw), but if you start really small by just allowing one image to be drawn and dragged around the screen, you can build on that and keep your code organized.

You would need to override onDraw(Canvas) and onTouchEvent(MotionEvent) in your custom View. You'll load your bitmaps with BitmapFactory (decodeResource method if you're including your images as resources in your project) and you'll need to remember to call recycle on your bitmaps when you're no longer using them.

In onDraw, you draw your bitmaps to the canvas at a specific location using Canvas.drawBitmap. There are two overloads of this method you can choose from, one that takes the top and left coordinates of the bitmap as floats (and therefore performs no scaling or stretching) and one that takes a destination and source rectangle to perform scaling, stretching and placement.

I always use the latter as it gives me finer tuned control. If you choose this route, you'll want to keep two Rect instances and a Bitmap instance for each image being drawn, update them in the touch events and draw them to the canvas in the draw event.

When something changes inside your view (as in the case of a touch event), call invalidate() method and the framework will know to redraw everything which triggers your onDraw method.

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