2011-03-25 119 views
7

我的应用程序通过调用在我看来的onDraw()方法如下实现自己的精灵补间动画:安卓:位图

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

的应用程序是一个物理模拟,到目前为止,这已制定了很大的。但是现在我想在某些事件发生时通过图像之间的变体来增强动画。

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

但Android tween animation example假设您有一个ImageView静态定义在您的XML配置的某处。

有没有办法使用补间动画在onDraw()中绘制动画位图?或者我需要转换我的精灵来使用某种ImageView?如果是后者,你能指点我一个适当的Android精灵动画的例子吗?

感谢

回答

10

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

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

您只需实现TweenAccessor接口即可将Tween支持添加到所有的精灵中。您甚至不需要更改Sprite类,只需创建一个SpriteTweenAccessor类,该类实现TweenAccessor<Sprite>,并在初始化时将其注册到引擎。只要有一个看看GetStarted wiki page

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

logo

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

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

// 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); 

当然,没有吐温引擎将是不完整的提供了一个方法来建立强大的序列:)

Timeline.createSequence() 
    // First, set all objects to their initial positions 
    .push(Tween.set(...)) 
    .push(Tween.set(...)) 
    .push(Tween.set(...)) 

    // Wait 1s 
    .pushPause(1.0f) 

    // Move the objects around, one after the other 
    .push(Tween.to(...)) 
    .push(Tween.to(...)) 
    .push(Tween.to(...)) 

    // Then, move the objects around at the same time 
    .beginParallel() 
     .push(Tween.to(...)) 
     .push(Tween.to(...)) 
     .push(Tween.to(...)) 
    .end() 

    // And repeat the whole sequence 2 times 
    // with a 0.5s pause between each iteration 
    .repeatYoyo(2, 0.5f) 

    // Let's go! 
    .start(); 

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

+0

哇,这太棒了,谢谢。 – 2011-04-14 13:21:17

+0

我刚刚更新了一下说明,因为从我第一次写答案开始,引擎已经取得了巨大的进步:) – 2012-05-31 14:47:29

+0

@AurélienRibon,它是否提供从一个精灵到另一个精灵的动画? – 2014-05-21 21:59:36

1

你可以不使用的ImageView从XML文件来补间动画,但它确实需要实际上是在您的视图层次视图。

您在Canvas中执行的绘图对视图层次结构不透明。我认为你有两种选择:

  1. 在你的自定义视图的顶部覆盖一个ImageView并使用补间动画动画。
  2. 使用Canvas绘制例程来动画爆炸。

我会说,使用Canvas程序,用自己maxtrix变换一起,是有道理的因为你可能已经在你的应用的动画线程。

+1

如果您创建游戏,可以使用Vectors(数学)来更新图像的位置,然后使用getX和getY绘制。你应该通过一定的速度在某个方向迭代和更新这个向量。 – 2011-03-25 21:00:45

+0

@Marcos:我已经在我的Sprite类(x/y位置,x/y veclocities)中实现了一种向量。有没有这个我忽略的标准课程? – 2011-03-25 21:19:55

+0

你认为使用ImageViews进行某种绝对定位的精灵是有意义的,然后还要依靠它们进行绘制?这就是我用Core Animation制作iPhone的那种动画,我很惊讶类似的东西不是Android推荐的例子。 Android似乎鼓励定期手动更新画布。我想知道这是否仅仅是因为我选择的例子是物理模拟,没有自动化的方法来模拟数十个粒子,每个粒子都对其他粒子施加重力。 – 2011-03-25 21:21:32

0

如果要创建一个游戏导出精灵图像,您可以使用向量(数学)来更新的位置然后用getX和getY绘制图像。你应该通过一定的速度在某个方向迭代和更新这个向量。

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

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

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