2013-08-04 47 views
0

我想通過嘗試設置多個圖像幀來使我的RelativeLayout背景圖像具有動畫效果。嘗試將圖像幀設置爲背景時出現「類演員異常」

這裏是我設置動畫

private View fullView; 
fullView = (View)findViewById(R.id.fullView); 

Resources res = getResources(); 
      Drawable drawable = res.getDrawable(R.drawable.eng_anim_1); 
      drawable.setAlpha(100); 

      fullView.setBackgroundDrawable(drawable); 
      AnimationDrawable progressAnimation = (AnimationDrawable) fullView.getBackground(); 
      progressAnimation.start(); 

這裏是我的「圖像」

<?xml version="1.0" encoding="utf-8"?> 


    <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/selected" android:oneshot="false"> 
<item android:drawable="@drawable/eng_anim_1" android:duration="50" /> 
<item android:drawable="@drawable/eng_anim_2" android:duration="50" /> 
<item android:drawable="@drawable/eng_anim_3" android:duration="50" /> 
<item android:drawable="@drawable/eng_anim_5" android:duration="50" /> 
<item android:drawable="@drawable/eng_anim_6" android:duration="50" /> 
<item android:drawable="@drawable/eng_anim_7" android:duration="50" /> 
<item android:drawable="@drawable/eng_anim_8" android:duration="50" /> 
<item android:drawable="@drawable/eng_anim_9" android:duration="50" /> 
<item android:drawable="@drawable/eng_anim_10" android:duration="50" /> 
<item android:drawable="@drawable/eng_anim_11" android:duration="50" /> 
<item android:drawable="@drawable/eng_anim_12" android:duration="50" /> 
<item android:drawable="@drawable/eng_anim_13" android:duration="50" /> 
<item android:drawable="@drawable/eng_anim_14" android:duration="50" /> 
<item android:drawable="@drawable/eng_anim_15" android:duration="50" /> 
<item android:drawable="@drawable/eng_anim_16" android:duration="50" /> 
<item android:drawable="@drawable/eng_anim_17" android:duration="50" /> 
<item android:drawable="@drawable/eng_anim_18" android:duration="50" /> 
<item android:drawable="@drawable/eng_anim_19" android:duration="50" /> 
<item android:drawable="@drawable/eng_anim_20" android:duration="50" /> 

</animation-list> 

但問題是,我收到以下錯誤

08-04 19:03:51.582: E/AndroidRuntime(504): FATAL EXCEPTION: main 
08-04 19:03:51.582: E/AndroidRuntime(504): java.lang.RuntimeException: Unable to start activity ComponentInfo{yohanSoftware.languageGame/yohanSoftware.languageGame.Game}: java.lang.ClassCastException: android.graphics.drawable.BitmapDrawable 
08-04 19:03:51.582: E/AndroidRuntime(504): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 
08-04 19:03:51.582: E/AndroidRuntime(504): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
08-04 19:03:51.582: E/AndroidRuntime(504): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
08-04 19:03:51.582: E/AndroidRuntime(504): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
08-04 19:03:51.582: E/AndroidRuntime(504): at android.os.Handler.dispatchMessage(Handler.java:99) 
08-04 19:03:51.582: E/AndroidRuntime(504): at android.os.Looper.loop(Looper.java:123) 
08-04 19:03:51.582: E/AndroidRuntime(504): at android.app.ActivityThread.main(ActivityThread.java:3683) 
08-04 19:03:51.582: E/AndroidRuntime(504): at java.lang.reflect.Method.invokeNative(Native Method) 
08-04 19:03:51.582: E/AndroidRuntime(504): at java.lang.reflect.Method.invoke(Method.java:507) 
08-04 19:03:51.582: E/AndroidRuntime(504): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
08-04 19:03:51.582: E/AndroidRuntime(504): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
08-04 19:03:51.582: E/AndroidRuntime(504): at dalvik.system.NativeStart.main(Native Method) 
08-04 19:03:51.582: E/AndroidRuntime(504): Caused by: java.lang.ClassCastException: android.graphics.drawable.BitmapDrawable 
08-04 19:03:51.582: E/AndroidRuntime(504): at yo.lang.Game.setUI(Game.java:236) 
08-04 19:03:51.582: E/AndroidRuntime(504): at yo.lan.Game.onCreate(Game.java:127) 
08-04 19:03:51.582: E/AndroidRuntime(504): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
08-04 19:03:51.582: E/AndroidRuntime(504): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 
08-04 19:03:51.582: E/AndroidRuntime(504): ... 11 more 
名單

我不明白我爲什麼得到這個。請幫忙!

回答

3

這是因爲您試圖將BitmapDrawable(動畫的第一幀)投射到AnimationDrawable。像這樣嘗試:

Drawable drawable = res.getDrawable(R.drawable.<your_animation_xml_file_name>); 
+0

感謝您的回覆。那麼,這是否意味着我必須將該XML文件放入drwable文件夾中?目前它在'佈局'文件夾中 –

+0

是的,這裏有一個例子:http://developer.android.com/reference/android/graphics/drawable/AnimationDrawable.html – Caner

+0

真棒!非常感謝!!!!!!! –