SurfaceView 中的 Android 动画角色

发布于 2024-11-18 05:29:06 字数 137 浏览 1 评论 0原文

我想在屏幕上制作一个角色的动画(例如遛狗)。 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 技术交流群。

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

发布评论

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

评论(2

陪你到最终 2024-11-25 05:29:06

您不需要 ImageView

如果您的动画是 XML Drawable,您可以直接从 Resources 将其加载到 AnimationDrawable 变量中:

Resources res = context.getResources();
AnimationDrawable animation = (AnimationDrawable)res.getDrawable(R.drawable.anim);      

然后设置其边界并在canvas:

animation.setBounds(left, top, right, bottom);
animation.draw(canvas);

您还需要手动设置动画以在下一个计划间隔运行。这可以通过使用animation.setCallback创建一个新的回调,然后实例化android.os.Handler并使用handler.postAtTime方法来完成将下一个动画帧添加到当前Thread 的消息队列中。

animation.setCallback(new Callback() {

  @Override
  public void unscheduleDrawable(Drawable who, Runnable what) {
    return;
  }

  @Override
  public void scheduleDrawable(Drawable who, Runnable what, long when) {
    //Schedules a message to be posted to the thread that contains the animation
    //at the next interval. 
    //Required for the animation to run. 
    Handler h = new Handler(); 
    h.postAtTime(what, when);
  }

  @Override
  public void invalidateDrawable(Drawable who) {
    return;
  }
});

animation.start();

You don't need an ImageView.

If your animation is an XML Drawable, you can load it directly from the Resources into an AnimationDrawable variable:

Resources res = context.getResources();
AnimationDrawable animation = (AnimationDrawable)res.getDrawable(R.drawable.anim);      

Then set it's bounds and draw on the canvas:

animation.setBounds(left, top, right, bottom);
animation.draw(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 an android.os.Handler and using the handler.postAtTime method to add the next animation frame to the current Thread's message queue.

animation.setCallback(new Callback() {

  @Override
  public void unscheduleDrawable(Drawable who, Runnable what) {
    return;
  }

  @Override
  public void scheduleDrawable(Drawable who, Runnable what, long when) {
    //Schedules a message to be posted to the thread that contains the animation
    //at the next interval. 
    //Required for the animation to run. 
    Handler h = new Handler(); 
    h.postAtTime(what, when);
  }

  @Override
  public void invalidateDrawable(Drawable who) {
    return;
  }
});

animation.start();
人间☆小暴躁 2024-11-25 05:29:06

使用 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.

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