2011-10-02 78 views
0

所以,我是用android编程的新手,我试图做的第一件事就是创建一个基本的动画,它自己开始。没有什么复杂的,只是一个循环动画。我试图坚持一帧一帧,因为它似乎是最基本和最容易理解的。我已经看过很多教程/网站/视频(包括android开发网站)如何做到这一点,不知道我做错了什么。我确定我有一个简单的逻辑错误。以下是我发布的代码。有人可以帮我吗?感谢您的帮助! (另外,根据Nook开发者网站的说法,这个特别是在NookColor模拟器上运行,nook运行最新的android。不幸的是Nook网站没有提供任何教程或者任何有用的东西,只有和Android开发者有相同的链接。)Android动画 - 逐帧?

//main class 

public class WallpaperActivity extends Activity { 

    AnimationDrawable animSequence; 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     ImageView animImg = (ImageView) findViewById(R.id.animatepic); 
     animImg.setBackgroundResource(R.drawable.animation); 
     animSequence = (AnimationDrawable) animImg.getBackground(); 

    } 


    @Override 
    public void onWindowFocusChanged(boolean hasFocus) 
    { 
     super.onWindowFocusChanged(hasFocus); 
     animSequence.start(); 
    } 
} 


//animation.xml class (<< this is not my main.xml class) 

    <?xml version="1.0" encoding="utf-8"?> 
    <animation-list xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/animatepic" android:oneshot="false" > 

    <item android:drawable="@drawable/a" android:duration="200" /> 
    <item android:drawable="@drawable/b" android:duration="200" /> 
    <item android:drawable="@drawable/c" android:duration="200" /> 
    <item android:drawable="@drawable/d" android:duration="200" /> 
    <item android:drawable="@drawable/e" android:duration="200" /> 
    <item android:drawable="@drawable/f" android:duration="200" /> 
    <item android:drawable="@drawable/g" android:duration="200" /> 
    <item android:drawable="@drawable/h" android:duration="200" /> 
    <item android:drawable="@drawable/i" android:duration="200" /> 
    <item android:drawable="@drawable/j" android:duration="200" /> 
    <item android:drawable="@drawable/k" android:duration="200" /> 
    <item android:drawable="@drawable/l" android:duration="200" /> 
    <item android:drawable="@drawable/m" android:duration="200" /> 
    <item android:drawable="@drawable/n" android:duration="200" /> 
    <item android:drawable="@drawable/o" android:duration="200" /> 
    </animation-list> 
+0

您是否收到任何错误或动画,无法启动... –

+0

我得到的是模拟器将加载,然后它会说,应用程序必须是“唯一的反馈强制关闭“ – Derek

回答

0

你说你得到了什么错误?

在任何情况下,您的代码示例似乎都是updated

这是它的zip file你可以下载。

+0

感谢代码斯蒂芬,这真的帮助了我。我意识到我的错误是什么,当然这是一个简单的看起来很不错的代码。在我发布的animation.xml类中,我给动画设置了animatepic的id,然后尝试在WallPaperActivity中引用它。相反,我应该在我的main.xml文件中引用ImageView对象...感谢您的帮助! – Derek

0
ImageView animImg = (ImageView) findViewById(R.id.animatepic); 
animImg.setBackgroundResource(R.drawable.animation); 
animSequence = (AnimationDrawable) animImg.getBackground(); 
animSequence.start(); 

试试这个