【问题标题】:How to make a sequence of two animations in Kotlin?如何在 Kotlin 中制作两个动画的序列?
【发布时间】:2019-08-25 18:57:35
【问题描述】:

我必须在我的 Kotlin 场景中制作动画,我想从这两个场景中制作一个序列,并且我希望序列具有无限重复,例如循环序列。

//First Animation
ObjectAnimator.ofFloat(block, "translationX", 50f).apply {
                duration = 500
                start()
            }

//Second Animation
ObjectAnimator.ofFloat(block, "translationX", 0f).apply {
                duration = 500
                start()
            }

感谢您的帮助!

解决方案:

 import android.support.v7.app.AppCompatActivity
 import android.os.Bundle
 import android.view.View
 import android.view.WindowManager
 import kotlinx.android.synthetic.main.activity_main.*
 import android.animation.ValueAnimator
 import android.animation.ObjectAnimator
 import android.animation.AnimatorSet
 import android.animation.Animator

 override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)


        animateTogether(
        stripe.objectAnimate() // https://github.com/blipinsk/ViewPropertyObjectAnimator
            .translationX(100f)
            .get(),
        stripe.objectAnimate()
            .translationX(-100f)
            .get()
    ).start()
}

fun animateTogether(vararg animators: Animator): AnimatorSet =
    AnimatorSet().apply {
        ObjectAnimator.ofFloat(stripe, "translationX", 100f).apply {
            duration = 500
            start()
        }
        ObjectAnimator.ofFloat(stripe, "translationX", -100f).apply {
            duration = 500
            start()
        }
    }

【问题讨论】:

  • 把你的两个动画师放在AnimatorSet中。
  • 您需要启动animatorSet,而不是其中的两个单独的动画师。我认为您实际上并不需要 animateTogether 中的对象动画师,这就是为什么它最初也不是我的答案的一部分。

标签: android-studio animation kotlin sequence


【解决方案1】:

试试 AnimatorSet

AnimatorSet as = new AnimatorSet();
                as.playSequentially(ObjectAnimator.ofFloat(...),
                ObjectAnimator.ofFloat(...));

那么您有两种方法可以循环播放一组重复模式,每个动画师取自here

objectAnimator.setRepeatCount(ObjectAnimator.INFINITE);
objectAnimator.setRepeatMode(ObjectAnimator.RESTART/REVERSE...);

第二次使用AnimatorListenerAdapter 监听动画何时结束并重新启动相同的动画。

mAnimationSet.addListener(new AnimatorListenerAdapter() {
@Override
 public void onAnimationEnd(Animator animation) {
      super.onAnimationEnd(animation);
      mAnimationSet.start();
 }
});
mAnimationSet.start();

【讨论】:

  • 感谢您的回答,但我需要 Kotlin 中的解决方案。 @巴厘岛
  • 将此代码复制粘贴到 Android IDE 中的 kotlin 类中,它会要求您将此代码转换为 kotlin。如果你不能这样做,那么我会在 kotlin 中更改它。告诉我
  • 它不会自动改变。 @巴厘岛
  • 自己翻译@JoyLucas
【解决方案2】:

我喜欢做以下事情:

fun View.objectAnimate() = ViewPropertyObjectAnimator.animate(this) // https://github.com/blipinsk/ViewPropertyObjectAnimator

fun animateTogether(vararg animators: Animator): AnimatorSet =
    AnimatorSet().apply {
        playTogether(*animators)
    }

现在我可以了

animateTogether(
    someView.objectAnimate()
            .translationX(40.dp)
            .get(),
    otherView.objectAnimate()
            .translationX(-40.dp)
            .get()
).start()

对于序列,你可以使用Animator.duration.initialDelay值,并且可以设置重复模式等。

【讨论】:

  • 我更新了我的答案。我实现的代码对吗?
  • 我需要导入什么才能使用 objectAnimate?我收到错误:未解决的参考:objectAnimate @EpicPandaForce
  • 这是我可能应该添加到答案本身的扩展功能,但它只是fun View.objectAnimate() = ViewPropertyObjectAnimator.animate(this)
  • 您能否更新您的答案,因为我再次遇到同样的错误?谢谢! @EpicPandaForce
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-04-15
  • 1970-01-01
  • 2023-03-07
  • 2021-05-11
  • 2017-08-31
  • 2020-02-27
  • 1970-01-01
相关资源
最近更新 更多