如何为一个图层列表项设置动画

gla*_*man 11 animation android android-animation

我有一个图层列表对象,它包含两个图像,一个是背景,另一个是旋转磁盘图像,它将在背景图像的顶部进行raotated.即我使用此图层列表作为线性布局背景,我只想动画图层列表的"disk_bg"项;

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/
android">
       <item android:drawable="@drawable/player_bg" />

       <item  android:top="166dp" >
             <bitmap  android:id="@+id/disk_bg" android:src="@drawable/cd"
       android:gravity="center" />
       </item>
Run Code Online (Sandbox Code Playgroud)

我使用这个图层列表作为布局背景,你知道如何在我的应用程序中为disk_bg图层设置动画吗?

能帮助我吗,非常感谢你〜

你不明白我的问题吗?或者没有办法那样做?

sot*_*cha 2

首先创建 2 个(或更多)图层列表资源,即 *layer_frame1.xml* 和 *layer_frame2.xml* ,您可以在其中设置框架。在您的情况下,假设更改磁盘项的 android:top 。

然后创建一个动画列表资源,在其中设置帧的时间和顺序:

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">

    <item
        android:drawable="@drawable/layer_frame1"
        android:duration="100"/>
    <item
        android:drawable="@drawable/layer_frame2"
        android:duration="100"/>

</animation-list>
Run Code Online (Sandbox Code Playgroud)

将其保存在文件中,即 *drawable/player_animation.xml* 并将其设置为视图上的背景

<View
        android:id="@+id/animation_test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/player_animation" />
Run Code Online (Sandbox Code Playgroud)

最后在您的代码中只需说明您希望动画何时开始。

 ((AnimationDrawable)findViewById(R.id.animation_test).getBackground()).start();
Run Code Online (Sandbox Code Playgroud)

注意不要在onCreate()方法中启动动画。