Android:补间动画的位图

时间:2011-03-25 20:44:02

标签: android animation sprite tween

我的应用通过在我的视图onDraw()方法中调用以下内容来实现自己的精灵:

  canvas.drawBitmap(sprite.getBitmap(), sprite.getX(), sprite.getY(), null);

该应用程序是一个物理模拟,到目前为止,这已经很好了。但是现在我想通过在某些事件发生时为精灵变换图像来增强动画效果。

例如 - 当发生碰撞时,我想播放爆炸动画。我的想法是用爆炸PNG替换通常的精灵位图,并使用Android“补间动画”使爆炸变大。

但Android tween animation example假设您在XML配置中静态定义了ImageView

有没有办法使用补间动画为onDraw()中绘制的位图设置动画效果?或者我是否需要转换我的精灵以使用某种ImageView?如果是后者,你能指点一下Android中正确的精灵动画的例子吗?

由于

4 个答案:

答案 0 :(得分:10)

我在java中构建了一个通用的Tween引擎,您可以使用它来动画任何内容,包括您的精灵。它针对Android和游戏进行了优化,因为它不会在运行时分配任何内容,以避免任何垃圾回收。此外,Tweens是汇总的,所以真的:根本没有垃圾收集!

您可以看到完整的演示here作为Android应用,或here作为WebGL html页面(需要Chrome)!

您所要做的就是实现TweenAccessor界面,为所有精灵添加Tween支持。您甚至不必更改Sprite类,只需创建一个实现SpriteTweenAccessor的{​​{1}}类,并在初始化时将其注册到引擎。只需查看GetStarted wiki page;)

即可

http://code.google.com/p/java-universal-tween-engine/

logo

我还在构建一个可嵌入任何应用程序的可视化时间轴编辑器。它将具有类似于Flash创作工具和Expression Blend(Silverlight开发工具)的时间轴。

整个引擎都有大量文档记录(所有公共方法和类都有详细的javadoc),语法与Flash世界中使用的Greensock的TweenMax / TweenLite引擎非常相似。请注意,它支持每个罗伯特·彭纳缓和方程式。

TweenAccessor<Sprite>

当然,没有提供构建强大序列的方法,任何补间引擎都不会完整:)

// Arguments are (1) the target, (2) the type of interpolation, 
// and (3) the duration in seconds. Additional methods specify  
// the target values, and the easing function. 

Tween.to(mySprite, Type.POSITION_XY, 1.0f).target(50, 50).ease(Elastic.INOUT);

// Possibilities are:

Tween.to(...); // interpolates from the current values to the targets
Tween.from(...); // interpolates from the given values to the current ones
Tween.set(...); // apply the target values without animation (useful with a delay)
Tween.call(...); // calls a method (useful with a delay)

// Current options are:

yourTween.delay(0.5f);
yourTween.repeat(2, 0.5f);
yourTween.repeatYoyo(2, 0.5f);
yourTween.pause();
yourTween.resume();
yourTween.setCallback(callback);
yourTween.setCallbackTriggers(flags);
yourTween.setUserData(obj);

// You can of course chain everything:

Tween.to(...).delay(1.0f).repeat(2, 0.5f).start();

// Moreover, slow-motion, fast-motion and reverse play is easy,
// you just need to change the speed of the update:

yourTween.update(delta * speed);

我希望你确信:)有很多人已经在他们的游戏或Android UI动画中使用引擎。

答案 1 :(得分:1)

您可以在不使用ImageView来自xml文件的情况下执行补间动画,但它确实需要在视图层次结构中实际成为视图。

您在Canvas中所做的绘图对视图层次结构是不透明的。我认为你有两个选择:

  1. 在自定义视图的顶部叠加ImageView,并使用补间动画为其设置动画。
  2. 使用Canvas绘制例程为爆炸制作动画。
  3. 我会说使用Canvas例程以及它们的maxtrix转换是有道理的,因为你的应用程序中可能已经有了一个动画线程。

答案 2 :(得分:0)

您可以在Adobe Flash中绘制爆炸并为其制作动画,使用swfSheet或类似内容导出精灵图像,然后使用Frame animation

答案 3 :(得分:0)

如果您正在创建游戏,可以使用向量(数学)更新图像的位置,然后使用getX和getY绘制。你应该通过某种速度迭代并更新这个向量。

那些向量不是原生的(游戏API有)。

你需要一个循环来迭代和更新位置,然后重绘。

对于游戏,使用ImageViews等组件制作动画并不是一个好主意。他们需要系统随着时间的推移一次又一次地计算所有布局。