【问题标题】:How to add delay between animations如何在动画之间添加延迟
【发布时间】:2016-03-03 10:57:37
【问题描述】:

我在 android 中遇到动画问题。我有我的 animation_char.xml:

<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
    android:duration="300"
    android:fromAlpha="0.0"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:toAlpha="1.0"/>
</set>

没关系,但是在我的 MainActivity 中,我想一个接一个地开始动画。所以我创建了一个方法让它更简单,只需更改 ImageView

public void animation(ImageView imageView){
    animation = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.animation_char);
    imageView.startAnimation(animation);
}

为了制作连续动画,我正在尝试使用 AnimatorSet。但是当我读到 AnimatorSet 与 Animator 一起工作时,而不是与 Animation 一起工作。所以我的问题是: 有没有办法在动画师中加载动画?或者我应该使用另一种方式来实现我想要做的事情?提前致谢!

编辑 我改变了我的方法,现在我正在尝试这个,但问题是所有图像同时出现,我怎样才能在动画之间添加一些延迟?

public void animation() {
    animation= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.animation_char);

            w.startAnimation(animation);
            a.startAnimation(animation);
            r.startAnimation(animation);     
}

【问题讨论】:

    标签: android animation animator


    【解决方案1】:

    其实我已经回答了这个问题here。您应该在第一个动画的 AnimationListener 的 onAnimationEnd 中开始您的第二个动画。第二个也是一样。

    【讨论】:

      【解决方案2】:

      您应该使用 AnimationSet 类而不是 AnimatorSet。

      例如

      AnimationSet as = new AnimationSet(true);
      as.setFillEnabled(true);
      as.setInterpolator(new BounceInterpolator());
      as.addAnimation(firstAnim);
      as.addAnimation(secondAnim);
      as.setDuration(1000);
      imageView.startAnimation(as);
      

      【讨论】:

      • 但是AnimationSet没有任何playSequentially方法
      • 它们是为AnimatorSet 而不是AnimationSet
      • 所以,尝试使用 firstAnim 和 secondAnim 的 setStartOffset 和 setDuration 方法
      • 不起作用@karvoynistas,我有三张图片,我想一张接一张地显示。我被困在这一点上!我编辑了我的问题
      【解决方案3】:

      我把这个放在这里也许它可以帮助某人......

      我做了这样的事情。我有 3 张图片想要一个接一个地掉下来。

      note1 = findViewById(R.id.note1);
      note1.setVisibility(View.INVISIBLE);
      note2 = findViewById(R.id.note2);
      note2.setVisibility(View.INVISIBLE);
      note3 = findViewById(R.id.note3);
      note3.setVisibility(View.INVISIBLE);
      

      设置可见性以便它们最初不显示

      然后创建3个动画

      Animation slideDown = AnimationUtils.loadAnimation(this, R.anim.slide_down);
              note1.startAnimation(slideDown);
      
      final Animation slideDown2 = AnimationUtils.loadAnimation(SplashScreen.this, R.anim.slide_down);
          
      final Animation slideDown3 = AnimationUtils.loadAnimation(SplashScreen.this, R.anim.slide_down);
      

      下一步是添加@Divers 添加的事件监听器:

       slideDown.setAnimationListener(new Animation.AnimationListener() {
                  @Override
                  public void onAnimationStart(Animation animation) {
      
                  }
      
                  @Override
                  public void onAnimationEnd(Animation animation) {
                      note1.setVisibility(View.VISIBLE);
                      note2.startAnimation(slideDown2);
                      slideDown2.setAnimationListener(new Animation.AnimationListener() {
      
                          @Override
                          public void onAnimationStart(Animation animation) {
      
                          }
      
                          @Override
                          public void onAnimationEnd(Animation animation) {
                              note3.startAnimation(slideDown3);
                              note2.setVisibility(View.VISIBLE);
                              slideDown3.setAnimationListener(new Animation.AnimationListener() {
                                  @Override
                                  public void onAnimationStart(Animation animation) {
      
                                  }
      
                                  @Override
                                  public void onAnimationEnd(Animation animation) {
                                      note3.setVisibility(View.VISIBLE);
                                  }
      
                                  @Override
                                  public void onAnimationRepeat(Animation animation) {
      
                                  }
                              });
                          }
      
                          @Override
                          public void onAnimationRepeat(Animation animation) {
      
                          }
                      });
                  }
      
                  @Override
                  public void onAnimationRepeat(Animation animation) {
      
                  }
              });
      

      仅此而已

      我的动画 xml 类似于

      <?xml version="1.0" encoding="utf-8"?>
      <set xmlns:android="http://schemas.android.com/apk/res/android">
      
          <translate
              android:duration="800"
              android:fromXDelta="0%"
              android:fromYDelta="-50%p"
              />
      
          <alpha
              android:duration="500"
              android:fromAlpha="0.1"
              android:toAlpha="1.0"
              />
      </set>
      

      【讨论】:

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