7

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

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

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

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

但 Android补间动画示例假定您ImageView在 XML 配置中静态定义了一个。

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

谢谢

4

4 回答 4

10

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

你可以在这里看到一个完整的演示作为一个 android 应用程序,或者在这里作为一个 WebGL html 页面(需要 Chrome)!

您所要做的就是实现TweenAccessor接口以将 Tween 支持添加到所有精灵。您甚至不必更改 Sprite 类,只需创建一个SpriteTweenAccessor实现 的类TweenAccessor<Sprite>,并在初始化时将其注册到引擎。看看GetStarted wiki 页面;)

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

标识

我还在构建一个可以嵌入到任何应用程序中的可视化时间线编辑器。它将具有类似于 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 动画。

于 2011-04-13T13:30:02.713 回答
1

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

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

  1. 在您的自定义视图上覆盖一个ImageView并使用补间动画对其进行动画处理。
  2. 使用Canvas绘图程序为您的爆炸设置动画。

我会说使用Canvas例程以及它们的 maxtrix 转换是有意义的,因为您的应用程序中可能已经有一个动画线程。

于 2011-03-25T20:56:47.457 回答
0

你可以在 Adob​​e Flash 中绘制和动画你的爆炸,使用swfSheet或类似的导出精灵图像,然后使用帧动画

于 2011-03-25T20:57:36.377 回答
0

如果您正在创建游戏,您可以使用 Vectors(Mathematical) 来更新图像的位置,然后使用 getX 和 getY 进行绘制。您应该以某种速度在某个方向上迭代和更新此向量。

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

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

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

于 2011-03-25T21:25:23.710 回答