2013-02-14 98 views
0

我想創建一個安卓遊戲。每次有人觸及顯示器時,玩家應該上去,如果他釋放玩家,則應該下降。Android動畫與onDraw或ImageView?

隨着許多教程的幫助,我做了它的工作,但現在我想動畫,我卡住了。這意味着玩家的形象應該每半秒鐘更換一次。另外,當玩家升起時,應該創建一個動畫旋轉。

但是(經過數小時的谷歌搜索)我找不到任何有用的答案來解決我的問題。 Android Developers網站討論瞭如何創建ImageView和XML文件。但是,多數民衆贊成在我堅持:我沒有一個ImageView的,我的播放器(我用一個PNG文件)簡單地通過的onDraw()方法創建:

public void onDraw(Canvas canvas) { 
     for (Sprite s : sprites) { 
      canvas.drawBitmap(s.getGraphic(), s.getLocation().x, 
        s.getLocation().y, null); 

     } 
} 

現在我想問問我應該怎麼做動畫和動畫旋轉。我應該從ImageView開始,還是可以以某種方式將onDraw方法「轉換」爲ImageView?或者還有另一種方法可以在沒有ImageView的情況下進行動畫和動畫旋轉?其次,如果我必須創建ImageView,我不明白我如何讓玩家變得「動態」,即:當有人觸摸顯示器時改變位置。

感謝提前:)

編輯:

好吧,我創建的文件夾繪製我animation.xml文件:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false" android:id="@+id/splashAnimation"> 
<item android:drawable="@drawable/ship" android:duration="200" /> 
<item android:drawable="@drawable/ship_2" android:duration="200" /> 
</animation-list> 

,在我的主文件我補充說:

ImageView img = (ImageView) findViewById(R.id.splashAnimation); 
    img.setBackgroundResource(R.drawable.animation); 
    ship_anim= (AnimationDrawable) img.getBackground(); 
ship_anim.start(); 

但是,現在我收到錯誤消息:NullPointerException

問題在哪裏?

回答

0

,如果你有描述動畫繪圖資源,您可以隨時在你的繪製文件夾中創建XML這樣的比這爲imageView添加到佈局

<?xml version="1.0" encoding="utf-8"?> 
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" > 
<item android:drawable="@drawable/img1" android:duration="100" /> 
<item android:drawable="@drawable/img2" android:duration="100" /> 
<item android:drawable="@drawable/img3" android:duration="100" /> 
<item android:drawable="@drawable/mimg4" android:duration="100" /> 
<item android:drawable="@drawable/img5" android:duration="100" /> 
</animation-list> 

它的代碼是這樣的:

myAnimation = (AnimationDrawable) findViewById(R.id.animation); 

只是添加事件處理程序比

if (!isPlaying) 
{ 
playDrawableAnimation(); 
    isPlaying = true; 
} else 
{ 
    faceAnimation.stop(); 
    isPlaying = false; 

}