【问题标题】:Loop animation drawable循环动画可绘制
【发布时间】:2015-06-24 19:41:05
【问题描述】:

我正在尝试为一些 png 制作动画,我会循环播放动画。这是我的代码:

wave.setBackgroundResource(R.drawable.wave_animation);
                frameAnimation = (AnimationDrawable)wave.getBackground();
                frameAnimation.setCallback(wave);
                frameAnimation.setVisible(true, true);
                frameAnimation.start();

这里是带有png的xml

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"  android:oneshot="true">
    <item android:drawable="@drawable/wave_01" android:duration="200" />
    <item android:drawable="@drawable/wave_02" android:duration="200" />
    <item android:drawable="@drawable/wave_03" android:duration="200" />
    <item android:drawable="@drawable/wave_04" android:duration="200" />
</animation-list>

我还添加了 android:oneshot=false 但不起作用。

【问题讨论】:

  • 我更新了我的答案,如果有帮助请告诉我。

标签: java android xml android-drawable animationdrawable


【解决方案1】:

android:oneshot="false"改成这样

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"  android:oneshot="false">
    <item android:drawable="@drawable/wave_01" android:duration="200" />
    <item android:drawable="@drawable/wave_02" android:duration="200" />
    <item android:drawable="@drawable/wave_03" android:duration="200" />
    <item android:drawable="@drawable/wave_04" android:duration="200" />
</animation-list>

【讨论】:

    【解决方案2】:

    上面的代码显示

        android:oneshot="true"
    

    这将使您的动画运行一次且仅一次。

    你说你试过 android:oneshot="false"。
    这对于多次运行动画列表至关重要。所以放回去吧。

    请记住,运行动画是一项“后台”任务,无论其自身设置如何,都会在主/前台任务完成时终止。

    如果您想要其他东西,您可能需要采取不同的方法。

    【讨论】:

      【解决方案3】:

      这是在图像上运行动画。在开始动画之前,您需要确保它尚未运行。在启动动画之前添加一个检查,如果它正在运行则停止它然后启动它。

      private void startAnimation(){
      
              imageView.setImageResource(R.drawable.img_animation);
      
              AnimationDrawable imgAnimation = (AnimationDrawable) imageView.getDrawable();
      
              if (imgAnimation .isRunning()) {
                  imgAnimation .stop();
              }
              imgAnimation .start();
      
          }
      

      img_animation.xml // 检查下面的 cmets

      <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true" >
      
          <!-- 24 frame per sec | 1000ms/24 i.e. 41ms per image -->
          <item
              android:drawable="@drawable/ball01" android:duration="41"/>
          <item
              android:drawable="@drawable/ball02" android:duration="41"/>
         ..........so on ..........
          <item
              android:drawable="@drawable/ball24" android:duration="41"/>
      
           <!-- Reset to first when animation stops-->
          <item
              android:drawable="@drawable/ball01"
              android:duration="10"/>
      
      </animation-list>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多