SurfaceView 中的 Android 动画角色
我想在屏幕上制作一个角色的动画(例如遛狗)。 AnimationDrawable 似乎非常适合此目的,并且 AnimationDrawable 需要 ImageView。如何在 SurfaceView 中添加和移动 ImageView?
谢谢。
I want to animate a character (for eg run a dog) on screen. AnimationDrawable seemed a perfect fit for this, and AnimationDrawable requires an ImageView. How do I add and move around the ImageView in SurfaceView?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不需要
ImageView
。如果您的动画是
XML Drawable
,您可以直接从Resources
将其加载到AnimationDrawable
变量中:然后设置其边界并在canvas:
您还需要手动设置动画以在下一个计划间隔运行。这可以通过使用animation.setCallback创建一个新的回调,然后实例化android.os.Handler并使用handler.postAtTime方法来完成将下一个动画帧添加到当前
Thread
的消息队列中。You don't need an
ImageView
.If your animation is an
XML Drawable
, you can load it directly from theResources
into anAnimationDrawable
variable:Then set it's bounds and draw on the canvas:
You also need to manually set the animation to run on it's next scheduled interval. This can be accomplished by creating a new Callback using
animation.setCallback
, then instantiating anandroid.os.Handler
and using thehandler.postAtTime
method to add the next animation frame to the currentThread
's message queue.使用 SurfaceView,您有责任绘制其中的所有内容。您不需要 AnimationDrawable 或任何视图来在其上渲染您的角色。查看 Google 的 Lunar Lander 示例游戏。
With a SurfaceView it is your responsibility to draw everything within it. You do not need AnimationDrawable or any view to render your character on it. Take a look onto Lunar Lander example game from Google.