Java 如何使用AnimationDrawable和animation list创建连续动画

Java 如何使用AnimationDrawable和animation list创建连续动画,java,android,xml,animation,android-animation,Java,Android,Xml,Animation,Android Animation,我试图创建一个顺序加载动画,显示的5个点一个接一个地变黑,以便显示加载动画。我已经制作了动画-但到目前为止-只有一个点变黑-我尝试将第二个点变黑似乎没有效果(一旦我弄清楚了如何制作动画,我将把这个方法应用到剩下的3个点上——这样你就知道代码现在在哪里了——我只尝试在这一点上制作第二个点的动画,但它不起作用——第一个点应该闪烁——然后动画中的其他4个点没有任何变化) -阿曼尼·斯旺 SRC代码段: 动画XML片段#1: 动画XML片段#2: 布局XML: 为什么不使用动画列表?4个点作为一

我试图创建一个顺序加载动画,显示的5个点一个接一个地变黑,以便显示加载动画。我已经制作了动画-但到目前为止-只有一个点变黑-我尝试将第二个点变黑似乎没有效果(一旦我弄清楚了如何制作动画,我将把这个方法应用到剩下的3个点上——这样你就知道代码现在在哪里了——我只尝试在这一点上制作第二个点的动画,但它不起作用——第一个点应该闪烁——然后动画中的其他4个点没有任何变化)

-阿曼尼·斯旺

SRC代码段: 动画XML片段#1:

动画XML片段#2:

布局XML:
为什么不使用动画列表?4个点作为一个图像,动画列表中的每个项目作为动画中的一帧


像这样

这可以使用Android的单个
图像视图
动画列表
动画绘图类来完成

  • drawable
    文件夹中复制五幅图像
  • 动画列表的
    drawable
    文件夹中创建XML文件
  • 列出此xml文件中的所有五个图像
  • 在xml文件中设置每个图像的动画持续时间
  • 将xml文件设置为
    ImageView
    中的图像资源
  • 使用
    getDrawable()
    从xml获取每个图像
  • 启动动画
例如,假设您创建了五个名为dot01.png、dot02.png、dot03.png、dot04.png、dot05..png的点图像,则
drawable
文件夹中的xml文件应为

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true" >

    <item
        android:drawable="@drawable/dot01"
        android:duration="150"/>
    <item
        android:drawable="@drawable/dot02"
        android:duration="150"/>
    <item
        android:drawable="@drawable/dot03"
        android:duration="150"/>
    <item
        android:drawable="@drawable/dot04"
        android:duration="150"/>        
    <item
        android:drawable="@drawable/dot05"
        android:duration="150"/>


    <!-- Reset to original -->
    <item
        android:drawable="@drawable/dot01"
        android:duration="10"/>

</animation-list>
ImageView DotAnimImage = (ImageView) findViewById(R.id.imageView1);
DotAnimImage.setImageResource(R.drawable.dots_animation);
AnimationDrawable dotsAnimation = (AnimationDrawable) DotAnimImage.getDrawable();

if (dotsAnimation.isRunning()) {
    dotsAnimation.stop();
}

dotsAnimation.start();
上面的代码可以工作,但它只会为图像设置一次动画,这是因为android中有一个bug,android认为动画即使已经结束了也仍在运行。解决方案是使用
isRunning()检查动画是否正在运行
AndroidDrawable
实例的方法,并使用
stop()
方法手动停止它

//Check if animation is running and stop it,
if (dotsAnimation.isRunning()) {
    dotsAnimation.stop();
    // start again,
    dotsAnimation.start();
}
您的最终代码应该是

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true" >

    <item
        android:drawable="@drawable/dot01"
        android:duration="150"/>
    <item
        android:drawable="@drawable/dot02"
        android:duration="150"/>
    <item
        android:drawable="@drawable/dot03"
        android:duration="150"/>
    <item
        android:drawable="@drawable/dot04"
        android:duration="150"/>        
    <item
        android:drawable="@drawable/dot05"
        android:duration="150"/>


    <!-- Reset to original -->
    <item
        android:drawable="@drawable/dot01"
        android:duration="10"/>

</animation-list>
ImageView DotAnimImage = (ImageView) findViewById(R.id.imageView1);
DotAnimImage.setImageResource(R.drawable.dots_animation);
AnimationDrawable dotsAnimation = (AnimationDrawable) DotAnimImage.getDrawable();

if (dotsAnimation.isRunning()) {
    dotsAnimation.stop();
}

dotsAnimation.start();
ImageView DotAnimImage = (ImageView) findViewById(R.id.imageView1);
// dots_animation below is the name of your xml file.
DotAnimImage.setImageResource(R.drawable.dots_animation);
// Now create a reference to getDrawable
AnimationDrawable dotsAnimation = (AnimationDrawable) DotAnimImage.getDrawable();
// Start animation
dotsAnimation.start();
//Check if animation is running and stop it,
if (dotsAnimation.isRunning()) {
    dotsAnimation.stop();
    // start again,
    dotsAnimation.start();
}
ImageView DotAnimImage = (ImageView) findViewById(R.id.imageView1);
DotAnimImage.setImageResource(R.drawable.dots_animation);
AnimationDrawable dotsAnimation = (AnimationDrawable) DotAnimImage.getDrawable();

if (dotsAnimation.isRunning()) {
    dotsAnimation.stop();
}

dotsAnimation.start();