我试图通过尝试设置图像帧的数量来使我的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
我不明白为什么我会这样做。
答案 0 :(得分:3)
这是因为您正在尝试将BitmapDrawable
(动画的第一帧)投射到AnimationDrawable
。试试这样:
Drawable drawable = res.getDrawable(R.drawable.<your_animation_xml_file_name>);