2011-11-03 46 views
1

我正在嘗試一個簡單的幀框架動畫。我的動畫按鈕點擊工作,但我希望它應該在活動啓動或加載時啓動。我已經嘗試onWindowFocusChanged()方法也可以按照文檔中的說明啓動動畫。我認爲我犯了一個愚蠢的錯誤。任何人都有想法。我的動畫沒有開始?

public class FirstActivity extends Activity implements OnClickListener { 
/** Called when the activity is first created. */ 
Button btnalarm; 
AnimationDrawable AniFrame; 
ImageView images; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    images=(ImageView)findViewById(R.id.myImageView); 
    images.setBackgroundResource(R.drawable.demo_animation); 
    AniFrame = (AnimationDrawable)images.getBackground(); 

} 
@Override 
public void onWindowFocusChanged(boolean hasFocus) { 

    super.onWindowFocusChanged(hasFocus); 
    Log.v("in focus", "in focus"); 
    AniFrame.start(); 
} 

demo_animation.xml文件---->

<animation-list 
    xmlns:android="http://schemas.android.com/apk/res/android" 
     android:oneshot="false"> 
    <item android:drawable="@drawable/a" android:duration="50" /> 
    <item android:drawable="@drawable/b" android:duration="50" /> 
    <item android:drawable="@drawable/c" android:duration="50" /> 
    <item android:drawable="@drawable/d" android:duration="50" /> 
    <item android:drawable="@drawable/e" android:duration="50" /> 
    <item android:drawable="@drawable/f" android:duration="50" /> 
    <item android:drawable="@drawable/h" android:duration="50" /> 
    <item android:drawable="@drawable/i" android:duration="50" /> 
    <item android:drawable="@drawable/j" android:duration="50" /> 
    <item android:drawable="@drawable/k" android:duration="50" />  
</animation-list> 

回答

0

添加 AniFram.start()到您的onCreate結束()。

也膨脹你的R.layout.main。

例子:

LinearLayout layout = (LinearLayout)findViewById(R.id.main); 

layout.startAnimation(AniFrame); 

還要檢查你的logcat的問題。

編輯:

看看這從文檔應該幫助很多。

Animating a drawable

0

只要用花葯thread.It將正常工作。

public void onCreate(Bundle savedInstanceState) { 
//your code 
//At last of onCreate add these lines 
images.post(new MyAnimation()); 
} 

class MyAnimation implements Runnable{ 
@Override  
public void run(){ 
    AniFrame.start(); 
    } 
    } 

現在它會工作,只需檢查它。

+0

感謝您的答覆。我嘗試了上面的線程邏輯,仍然沒有開始動畫。 –

+0

你可以請把包含框架動畫的xml文件。 –

+0

是的,我把xml文件,請檢查編輯的問題。 –

0

嘗試使用setCallback啓動動畫之前如下:

AniFrame.setCallback(images);