如何在Android中的SurfaceView中播放动画
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想让它更加分离,您可以创建另一个类来处理此动画并跟踪它所在的帧以及要使用的位图,然后将精灵的绘制委托给
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
'sonDraw
.