Android:位图的补间动画
我的应用程序通过在视图的 onDraw()
方法中调用以下内容来实现自己的精灵:
canvas.drawBitmap(sprite.getBitmap(), sprite.getX(), sprite.getY(), null);
该应用程序是一个物理模拟,到目前为止效果很好。但现在我想通过在发生某些事件时在精灵图像之间进行变形来增强动画。
例如,当发生碰撞时,我想播放爆炸动画。我的想法是用爆炸 PNG 替换通常的精灵位图,并使用 Android“补间动画”使爆炸变大。
但是 Android 补间动画示例 假设您有一个ImageView
在 XML 配置中静态定义。
有没有办法使用补间动画对 onDraw()
中绘制的位图进行动画处理?或者我是否需要转换我的精灵以使用某种ImageView
?如果是后者,你能给我举一个 Android 中正确的精灵动画的例子吗?
谢谢
My app implements its own sprites by calling the following in my view's onDraw()
method:
canvas.drawBitmap(sprite.getBitmap(), sprite.getX(), sprite.getY(), null);
The app is a physics simulation, and so far this has worked out great. But now I'd like to enhance the animation by morphing between images for the sprites when certain events occur.
For example- when a collision happens, I would like to play an animation of an explosion. My idea was to replace the usual sprite bitmap with that of an explosion PNG, and use Android "tween animation" to make the explosion grow larger.
But the Android tween animation example assumes that you have an ImageView
defined somewhere statically in your XML configuration.
Is there a way to animate a bitmap as drawn in onDraw()
using tween animation? Or do I need to convert my sprites to use some sort of ImageView
? If the latter, can you point me to an example of proper sprite animation in Android?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我用 java 构建了一个通用的 Tween 引擎,你可以用它来制作任何东西的动画,包括你的精灵。它针对 Android 和游戏进行了优化,因为它不会在运行时分配任何内容,以避免任何垃圾收集。此外,Tweens 是池化的,所以真的:根本没有垃圾收集!
您可以在此处查看完整的演示:一个 Android 应用程序,或此处 作为 WebGL html 页面(需要Chrome)!
您所要做的就是实现
TweenAccessor
接口,为您的所有精灵添加Tween支持。您甚至不必更改 Sprite 类,只需创建一个实现TweenAccessor
的SpriteTweenAccessor
类,并在初始化时将其注册到引擎即可。只需查看 GetStarted wiki 页面即可;)http://code.google.com/p/java-universal-tween- engine/
我还在构建一个可以嵌入到任何应用程序中的可视化时间线编辑器。它将具有类似于 Flash 创作工具和 Expression Blend(Silverlight 开发工具)的时间线。
整个引擎有大量文档(所有公共方法和类都有详细的 javadoc),并且语法与 Flash 世界中使用的 Greensock 的 TweenMax/TweenLite 引擎非常相似。请注意,它支持每个 Robert Penner 缓动方程。
当然,如果不提供构建强大序列的方法,任何补间引擎都是不完整的:)
我希望你相信:)有很多人已经在他们的游戏或 Android UI 动画中使用该引擎。
I built a generic Tween Engine in java that you can use to animate anything, including your sprites. It's optimized for Android and games because it does not allocate anything at runtime, to avoid any garbage collection. Moreover, Tweens are pooled, so really: no garbage collection at all!
You can see a complete demo here as an android application, or here as a WebGL html page (requires Chrome)!
All you have to do is implement the
TweenAccessor
interface to add Tween support to all your sprites. You don't even have to change your Sprite class, just create aSpriteTweenAccessor
class that implementsTweenAccessor<Sprite>
, and register it to the engine at initialization. Just have a look at the GetStarted wiki page ;)http://code.google.com/p/java-universal-tween-engine/
I'm also building a visual timeline editor that can be embedded in any application. It will feature a timeline similar to the Flash authoring tool and Expression Blend (a Silverlight dev tool).
The whole engine is heavily documented (all public methods and classes have detailed javadoc), and the syntax is quite similar to Greensock's TweenMax/TweenLite engine that is used in the Flash world. Note that it supports every Robert Penner easing equation.
Of course, no tween engine would be complete without providing a way to build powerful sequences :)
I hope you're convinced :) There are a lot of people already using the engine in their games or for android UI animation.
您可以在没有来自 xml 文件的
ImageView
的情况下执行补间动画,但它确实需要是视图层次结构中的一个 View。您在
Canvas
中进行的绘图对于视图层次结构来说是不透明的。我认为您有两个选择:ImageView
并使用补间动画对其进行动画处理。Canvas
绘制例程来制作爆炸动画。我想说,考虑到您的应用程序中可能已经有一个动画线程,使用 Canvas 例程及其 maxtrix 转换是有意义的。
You can do the tween animation without the
ImageView
coming from an xml file, but it does need to actually be a View in your view hierarchy.The drawing you're doing in your
Canvas
is opaque to the view hierarchy. I think you have two options:ImageView
on top of your custom view and animate that using tween animations.Canvas
draw routines to animate your explosion.I'd say that using
Canvas
routines, along with their maxtrix transformations, makes sense given that you probably already have an animation thread in your app.您可以在 Adobe Flash 中绘制爆炸并为其设置动画,使用 swfSheet< 导出精灵图像/a> 或类似的,然后使用 框架动画
You could draw and animate your explosion in Adobe Flash, export the sprite images using swfSheet or similar and then use Frame animation
如果您正在创建游戏,您可以使用向量(数学)来更新图像的位置,然后使用 getX 和 getY 进行绘制。您应该以某种速度在某个方向上迭代和更新该向量。
这些向量不是原生的(游戏 API 是原生的)。
您需要有一个循环来迭代和更新位置,然后重绘。
对于游戏来说,使用 ImageView 等组件来制作动画并不是一个好主意。他们需要系统随着时间的推移一次又一次地计算所有布局。
If you are creating a game, you can use Vectors(Mathematical) to update the position of your images and then draw then with getX and getY. You should iterate and update this vector in some direction through some speed.
Those Vectors are not native (Game APIs has).
You need have a loop to iterate and update the position, then redraw.
For games, it's not a good idea to have components such ImageViews to make animations. They need the system to calc all the layout again and again over time.