找不到xml文件动画的符号

ctehm74n  于 2021-07-12  发布在  Java
关注(0)|答案(5)|浏览(322)

我需要帮助。它说:错误:找不到符号fruitimage.setbackgroundresource(r.drawable.myfruits);^符号:变量位置:可绘制类

qv7cva1a

qv7cva1a2#

--

myfruits.xml (located at drawable)

<?xmlversion="1.0"encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/img_grapes" android:duration="200"/>
<item android:drawable="@drawable/img_lemon" android:duration="200"/>
<item android:drawable="@drawable/img_orange" android:duration="200"/>
<item android:drawable="@drawable/img_pear" android:duration="200"/>
<item android:drawable="@drawable/img_strawberry" android:duration="200"/>
</animation-list>
dkqlctbz

dkqlctbz4#


@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ImageView fruitImage = (ImageView) findViewById(R.id.imgFruit);
        fruitImage.setBackgroundResource(R.drawable.myfruits);
        fruitAnimation = (AnimationDrawable) fruitImage.getBackground();
    }
7vhp5slm

7vhp5slm5#

这是解释。
https://developer.android.com/reference/android/graphics/drawable/animationdrawable
在代码中,必须更改myfruits.xml。

<animation-list android:id="@+id/selected" android:oneshot="false">
<item android:drawable="@drawable/img_grapes" android:duration="200"/>
<item android:drawable="@drawable/img_lemon" android:duration="200"/>
<item android:drawable="@drawable/img_orange" android:duration="200"/>
<item android:drawable="@drawable/img_pear" android:duration="200"/>
<item android:drawable="@drawable/img_strawberry" android:duration="200"/>
</animation-list>

-----主要活动---

ImageView fruitImage = (ImageView) findViewById(R.id.imgFruit);
fruitImage.setBackgroundResource(R.drawable.myfruits);
AnimationDrawable fruitAnimation = (AnimationDrawable) fruitImage.getBackground();
fruitAnimation.start();

相关问题