drawable资源文件,选择器<animation-list>被识别为StateListDrawble

时间:2017-10-05 11:45:50

标签: android animation

我想逐帧添加动画,所以我使用了正式参考并找到了AnimationDrawable对象。 这是链接:https://developer.android.com/reference/android/graphics/drawable/AnimationDrawable.html

我在放置在drawable文件夹中的XML文件中定义了动画,这里是代码:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <animation-list android:id="@+id/exAnim" android:oneshot="true">
        <item android:drawable="@drawable/explosion1" android:duration="50"/>
        <item android:drawable="@drawable/explosion2" android:duration="50"/>
        <item android:drawable="@drawable/explosion3" android:duration="50"/>
        <item android:drawable="@drawable/explosion4" android:duration="50"/>
        <item android:drawable="@drawable/explosion5" android:duration="50"/>
        <item android:drawable="@drawable/explosion6" android:duration="50"/>
        <item android:drawable="@drawable/explosion7" android:duration="50"/>
        <item android:drawable="@drawable/explosion8" android:duration="50"/>
        <item android:drawable="@drawable/explosion9" android:duration="50"/>
        <item android:drawable="@drawable/explosion10" android:duration="50"/>
        <item android:drawable="@drawable/explosion11" android:duration="50"/>
        <item android:drawable="@drawable/explosion12" android:duration="50"/>
        <item android:drawable="@drawable/explosion13" android:duration="50"/>
        <item android:drawable="@drawable/explosion14" android:duration="50"/>
        <item android:drawable="@drawable/explosion15" android:duration="50"/>
    </animation-list>

</selector>

我还定义了一个ImageView,它将在布局XML中托管动画:

<ImageView
        android:onClick="startIt"
        android:id="@+id/fire"
        android:layout_width="85dp"
        android:layout_height="85dp"
        android:layout_below="@+id/btn_game"
        android:layout_centerHorizontal="true"/>

</RelativeLayout>

然后我定义了一个方法,该方法应该在单击ImageView时启动动画:

 public void startIt(View v){
        // Load the ImageView that will host the animation and
        // set its background to our AnimationDrawable XML resource.
        ImageView img = (ImageView)findViewById(R.id.fire);
        img.setImageResource(R.drawable.explode_anim);

        // Get the background, which has been compiled to an AnimationDrawable object.
        AnimationDrawable frameAnimation = (AnimationDrawable) img.getDrawable();
    }

当我尝试单击ImageView时会抛出此异常:

Caused by: java.lang.ClassCastException: android.graphics.drawable.StateListDrawable cannot be cast to android.graphics.drawable.AnimationDrawable

感谢所有帮助者。

2 个答案:

答案 0 :(得分:0)

我找到了解决这个问题的方法。

array(15) {
    [0] => array(9) {
        "james" ["name"] => string(19)
        "42" ["age"] => string(18)
        "teacher" ["position"] => string(17)
  ....

我希望,这会有所帮助!

答案 1 :(得分:0)

我通过从动画xml中删除选择器标记解决了这个问题,并将xmlns属性移动到动画列表标记中,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/exAnim" android:oneshot="true">
    <item android:drawable="@drawable/explosion1" android:duration="50"/>
    <item android:drawable="@drawable/explosion2" android:duration="50"/>
    <item android:drawable="@drawable/explosion3" android:duration="50"/>
    <item android:drawable="@drawable/explosion4" android:duration="50"/>
    <item android:drawable="@drawable/explosion5" android:duration="50"/>
    <item android:drawable="@drawable/explosion6" android:duration="50"/>
    <item android:drawable="@drawable/explosion7" android:duration="50"/>
    <item android:drawable="@drawable/explosion8" android:duration="50"/>
    <item android:drawable="@drawable/explosion9" android:duration="50"/>
    <item android:drawable="@drawable/explosion10" android:duration="50"/>
    <item android:drawable="@drawable/explosion11" android:duration="50"/>
    <item android:drawable="@drawable/explosion12" android:duration="50"/>
    <item android:drawable="@drawable/explosion13" android:duration="50"/>
    <item android:drawable="@drawable/explosion14" android:duration="50"/>
    <item android:drawable="@drawable/explosion15" android:duration="50"/>
</animation-list>

它有效。