2

我已经创建了一个 AnimationDrawable 并且我想重复该动画无限次数并在单击按钮时停止该动画,我正在分享我的代码

动画.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true" >
    <item android:drawable="@drawable/download_anim1" android:duration="100" />
    <item android:drawable="@drawable/download_anim2" android:duration="100" />
    <item android:drawable="@drawable/download_anim3" android:duration="100" />
    <item android:drawable="@drawable/download_anim4" android:duration="100" />
    <item android:drawable="@drawable/download_anim5" android:duration="100" />
</animation-list>

在我的布局文件中

<ImageView
    android:id="@+id/imageViewAnimation"
    android:layout_width="wrap_content"
    android:layout_height="30dp"
    android:padding="3dp"
    android:layout_marginBottom="10dp"
    android:layout_weight="2"
    android:background="@color/colorOrange"
    android:src="@drawable/anim" />

在我开始动画的活动中

ImageView imageViewAnimationr = (ImageView) headerView.findViewById(R.id.imageViewHeaderDownloadAnimation);
AnimationDrawable animationDrawable = (AnimationDrawable) imageViewAnimationr.getDrawable();
animationDrawable.start();
4

2 回答 2

2

您可以更改oneshot=false为重复无限次并在您可以使用的任何条件下停止animationDrawable.stop() :-

<?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/img_one" android:duration="200" />
    <item android:drawable="@drawable/img_two" android:duration="200" />
    <item android:drawable="@drawable/img_three" android:duration="200" />
</animation-list>
于 2017-04-22T11:55:11.733 回答
2

我只是找到setOneShot来重复动画并找到Stop在任何情况下停止动画。

于 2015-12-15T05:53:54.973 回答