2013-02-14 85 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; 

}