溫馨提示×

在Android中如何設置AnimationDrawable的動畫

小樊
134
2024-08-15 09:47:36
欄目: 編程語言

要設置AnimationDrawable的動畫,首先需要在res/drawable文件夾下創建一個XML文件來定義動畫幀序列。例如,創建一個名為animation.xml的文件,定義如下:

<animation-list android:id="@+id/selected" android:oneshot="false">
    <item android:drawable="@drawable/frame1" android:duration="50" />
    <item android:drawable="@drawable/frame2" android:duration="50" />
    <item android:drawable="@drawable/frame3" android:duration="50" />
    <!-- 添加更多的動畫幀 -->
</animation-list>

然后在代碼中加載AnimationDrawable并設置到ImageView中:

// 加載AnimationDrawable
ImageView imageView = findViewById(R.id.imageView);
AnimationDrawable animation = (AnimationDrawable) getResources().getDrawable(R.drawable.animation);

// 將AnimationDrawable設置到ImageView中
imageView.setBackground(animation);

// 開始動畫
animation.start();

這樣就可以在ImageView中顯示AnimationDrawable的動畫了。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女