如何将 AnimationDrawable 放置在 SurfaceView 画布上
游戏开发新手,我无法将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过不断的挖掘,我找到了答案......似乎我喜欢在这里回答我自己的问题。
刚刚找到电影课程。我可以使用 InputStream 将动画 gif 加载到其中,然后在 onDraw() 中一点一点地播放电影,因为 Movie 类支持 draw() 方法,我可以在其中提供画布和 x,y 坐标。
下面是代码片段:
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: