2011-03-25 114 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製作動畫並不是一個好主意。他們需要系統隨着時間的推移重新計算所有佈局。