如何在Android中的SurfaceView中播放动画

发布于 2024-11-16 05:49:50 字数 487 浏览 0 评论 0原文

我有一个 SurfaceView 当前正在绘制一系列位图。这些位图可以轻松地进行几何操作(缩放/旋转/平移/等)。一个例子是弹跳的球,它可能只使用平移/旋转操作。球只是一个位图。

我希望用一系列位图替换这个“球”位图,例如,如果球在一阵烟花中爆炸,我有单独的帧,我希望将它们串在一起。

无需在 SurfaceView 中的每个 onDraw() 调用上手动循环它们,我该如何解决这个问题?我知道在正常情况下,我可以使用 XML 文件中的帧创建一个 AnimationDrawable,将其绑定到 ImageView(设置为背景),然后调用它。但是,我使用 SurfaceView 在屏幕上显示所有图形,而不是 ImageView。我读过您可以在 SurfaceViews 之上覆盖其他视图,但我不确定是否可以获得所需的确切位置。例如,球可能会在我的 SurfaceView 上的 X=65 Y=21 处爆炸,我怎样才能将这个精确的坐标转换到我的 ImageView 上?

任何有关替代方法的帮助或提示将不胜感激。

I have a SurfaceView which is currently drawing a series of Bitmaps. These bitmaps can easily be manipulated geometrically (scale/rotate/translate/etc). An example would be a ball which is bouncing, which may just use translate/rotate operations. The ball is just a single bitmap.

I wish to replace this 'ball' Bitmap with a sequence of Bitmaps, for example if the ball were to explode in a flurry of fireworks, I have the individual frames and I wish to string them together.

Without having to manually cycle through them on each onDraw() call in my SurfaceView, how can I go about this? I know in normal conditions I can just make an AnimationDrawable using the frames in an XML file, bind it to an ImageView (set as background) then call it. However, I am using a SurfaceView for displaying all my graphics on screen, not ImageViews. I have read you can overlay other views on top of SurfaceViews, but I am unsure that I could get the exact position required. For example, the ball may explode at X=65 Y=21 on my SurfaceView, how could I translate this exact co-ordinate to my ImageView?

Any help or tips on alternative methods would be greatly appreciated.

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

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

发布评论

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

评论(1

倾城花音 2024-11-23 05:49:50

如果您想让它更加分离,您可以创建另一个类来处理此动画并跟踪它所在的帧以及要使用的位图,然后将精灵的绘制委托给 SurfaceView 中的此类的onDraw

If you want to keep it more separated, you can create another class that handles this animation and keeps track of which frame it's on and the bitmaps to use then delegate drawing of the sprite to this class in your SurfaceView's onDraw.

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