如何将 AnimationDrawable 放置在 SurfaceView 画布上

发布于 2024-11-28 07:11:14 字数 756 浏览 2 评论 0原文

游戏开发新手,我无法将 AnimationDrawable 放置到 SurfaceView 画布上。这是一个简单游戏的一部分,用户触摸屏幕,一个动画 gif 被放置在看起来像爆炸的位置。我可以使用下面所示的代码通过位图来完成此操作,但将其转换为 AnimationDrawable 是我遇到的问题。我可以从 ImageView 创建 AnimationDrawable,但我也找不到将 ImageView 放到画布上的方法......

我是否以错误的方式处理这个问题?有没有更简单的方法来让动画 gif 显示在 SurfaceView 画布上的 x,y 坐标处?

Bitmap explodeBmp = BitmapFactory.decodeResource(getResources(), R.drawable.explode4);
canvas.drawBitmap(explodeBmp, coords.getX()-(explodeBmp.getWidth()/2), coords.getY()-(explodeBmp.getHeight()/2), paint);

如果我尝试将位图转换为 AnimationDrawable 并启动它,则会抛出 ClassCastException:

AnimationDrawable explosionAnimation = (AnimationDrawable) ((Drawable) new BitmapDrawable(explodeBmp));
explosionAnimation.start();

Noob to game development and I'm having trouble placing an AnimationDrawable onto a SurfaceView canvas. It's part of a simple game, user touches screen and an animated gif is placed at that location that looks like an explosion. I can accomplish this with a Bitmap using the code shown below, but converting this to an AnimationDrawable is where I'm stuck. I could create the AnimationDrawable from an ImageView, but I can't find a way to get the ImageView onto the canvas either...

Am I going about this in the wrong way? Is there a simpler way to get an animated gif to display at an x,y coordinate on a SurfaceView's canvas?

Bitmap explodeBmp = BitmapFactory.decodeResource(getResources(), R.drawable.explode4);
canvas.drawBitmap(explodeBmp, coords.getX()-(explodeBmp.getWidth()/2), coords.getY()-(explodeBmp.getHeight()/2), paint);

This throws a ClassCastException if I try to convert the Bitmap to an AnimationDrawable and start it:

AnimationDrawable explosionAnimation = (AnimationDrawable) ((Drawable) new BitmapDrawable(explodeBmp));
explosionAnimation.start();

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

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

发布评论

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

评论(1

ぃ双果 2024-12-05 07:11:14

经过不断的挖掘,我找到了答案......似乎我喜欢在这里回答我自己的问题。

刚刚找到电影课程。我可以使用 InputStream 将动画 gif 加载到其中,然后在 onDraw() 中一点一点地播放电影,因为 Movie 类支持 draw() 方法,我可以在其中提供画布和 x,y 坐标。

下面是代码片段:

InputStream is = context.getResources().openRawResource(R.drawable.dotz_explosion);
Movie explodeGif = Movie.decodeStream(is);

...

@Override
protected void onDraw(Canvas canvas) {
    ...
      GraphicObject explosion = (GraphicObject)ex.next();

      long now = android.os.SystemClock.uptimeMillis();
      if (explosion.getMovieStart() == 0) { // first time
         explosion.setMovieStart(now);
      }

      int relTime = (int)((now - explosion.getMovieStart()) % explodeGif.duration());
      if ((now - explosion.getMovieStart()) >= explodeGif.duration()) {
         removeArrayExplosions.add(removeIndex);
         explosion.setMovieStart(0);
      } else {
         explodeGif.setTime(relTime);
         explodeGif.draw(canvas, explosion.getX()-(explodeGif.width()/2), explosion.getY()-(explodeGif.height()/2));
      }
    }
   ...
}

After continuous digging I've found the answer... seems I like answering my own questions here.

Just found the Movie class. I can load my animated gif into it using an InputStream, then play the movie bit by bit in my onDraw() because the Movie class supports a draw() method where I can supply my canvas and x,y coordinates.

Here's the code snippit below:

InputStream is = context.getResources().openRawResource(R.drawable.dotz_explosion);
Movie explodeGif = Movie.decodeStream(is);

...

@Override
protected void onDraw(Canvas canvas) {
    ...
      GraphicObject explosion = (GraphicObject)ex.next();

      long now = android.os.SystemClock.uptimeMillis();
      if (explosion.getMovieStart() == 0) { // first time
         explosion.setMovieStart(now);
      }

      int relTime = (int)((now - explosion.getMovieStart()) % explodeGif.duration());
      if ((now - explosion.getMovieStart()) >= explodeGif.duration()) {
         removeArrayExplosions.add(removeIndex);
         explosion.setMovieStart(0);
      } else {
         explodeGif.setTime(relTime);
         explodeGif.draw(canvas, explosion.getX()-(explodeGif.width()/2), explosion.getY()-(explodeGif.height()/2));
      }
    }
   ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文