简体   繁体   English

Android - AnimatorSet、Object Animator - 弹跳动画链正在合并?

[英]Android - AnimatorSet, Object Animator - Bounce animation chain is amalgamating?

I have a question concerning ObjectAnimator in Android.我有一个关于 Android 中的 ObjectAnimator 的问题。 I am trying to model a Bounce effect whereby the View slides up (decreasing Y value) and back down after by the same amount 'n', followed by the View sliding up and down again but this time by 'n/2' (so half the distance).我正在尝试模拟一个弹跳效果,其中视图向上滑动(减小 Y 值)并在相同数量“n”之后返回,然后视图再次向上和向下滑动,但这次是“n/2”(所以距离的一半)。

So a bigger bounce, followed by a shallower bounce - ie, the kinda thing a Mac icon does in the tray when it wants your attention.所以弹跳幅度更大,然后弹跳幅度更小 - 即,当 Mac 图标需要您的注意时,它会在托盘中执行类似的操作。

Here is what I've tried so far (assume v is a View ):到目前为止,这是我尝试过的(假设v是一个View ):

float y = v.getTranslationY(),distance = 20F;

                AnimatorSet s = new AnimatorSet();
                s.play(ObjectAnimator.ofFloat(v, "translationY", y- distance).setDuration(500))
                .before(ObjectAnimator.ofFloat(v, "translationY", y).setDuration(500))
                .before(ObjectAnimator.ofFloat(v, "translationY", y- (distance/2)).setDuration(500))
                .before(ObjectAnimator.ofFloat(v, "translationY", y).setDuration(500));
                s.start();

Ignore the code quality, it's a POC, I was hoping this would work, but it seems to only 'bounce' once as if its combined the animations despite the use of .before() .忽略代码质量,这是一个 POC,我希望这会起作用,但它似乎只“弹跳”一次,就好像它结合了动画,尽管使用了.before()

Could you please show me how I can create complex AnimatorSet chains that do not amalgamate in to one, as I seem to be missing something?你能告诉我如何创建复杂的 AnimatorSet 链,这些链不会合并为一个,因为我似乎遗漏了什么?

BONUS: For extra points, how can I set the repeat of an AnimatorSet?奖励为了加分,我如何设置 AnimatorSet 的重复?

Many thanks!非常感谢!

OK so I eventually found a fairly neat way to achieve sequential animation by ignoring the fluent builder, and just using the playSequentially() method such that:好的,所以我最终找到了一种相当简洁的方法来实现顺序动画,方法是忽略流畅的构建器,而只使用playSequentially()方法,这样:

AnimatorSet as = new AnimatorSet();
as.playSequentially(ObjectAnimator.ofFloat(...), // anim 1
                    ObjectAnimator.ofFloat(...), // anim 2
                    ObjectAnimator.ofFloat(...), // anim 3
                    ObjectAnimator.ofFloat(...)); // anim 4
as.setDuration(600);
as.start();

Still haven't worked out repeating though, other than a dirty hack involving the callback onAnimationEnd in a listener.不过,除了在监听器中涉及回调 onAnimationEnd 的肮脏 hack 之外,仍然没有解决重复问题。 Must be a simpler way, so perhaps someone can edit this when they know of one.必须是一种更简单的方法,所以也许有人知道后可以编辑它。

Anyway, hope the above helps someone.无论如何,希望以上内容对某人有所帮助。

When you use the Builder, all returning dependencies refer to the first Animator, so you had 3 bounces happening simultaneously after the first movement.当您使用 Builder 时,所有返回的依赖项都引用第一个 Animator,因此您在第一次移动后同时发生了 3 次反弹。 Unfortunately it seems AnimatorSet is broken on some aspects, one of them being repeats: https://code.google.com/p/android/issues/detail?id=17662不幸的是,AnimatorSet 似乎在某些方面被打破了,其中之一是重复的: https ://code.google.com/p/android/issues/detail?id=17662

If you are using Kotlin do it like this:如果您使用的是Kotlin ,请这样做:

           val animationSet =  AnimatorSet();
            animationSet.playSequentially(cornerAnimation, colorChangeAnimator,cardElevationAnimator )
            animationSet.duration = ANIMATION_INTERVAL_MS;
            animationSet.start();
            animationSet.addListener(object :AnimatorUpdateListener, Animator.AnimatorListener {
                override fun onAnimationEnd(animation: Animator?) {
                    currentColor = toColor
                }
                override fun onAnimationUpdate(animation: ValueAnimator?) {}
                override fun onAnimationRepeat(animation: Animator?) {}
                override fun onAnimationCancel(animation: Animator?) {}
                override fun onAnimationStart(animation: Animator?) {}
            })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM