Android-AnimatorSet,Object Animator-反弹动画链是否融合在一起? [英] Android - AnimatorSet, Object Animator - Bounce animation chain is amalgamating?

查看:110
本文介绍了Android-AnimatorSet,Object Animator-反弹动画链是否融合在一起?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Android中的ObjectAnimator有疑问。我正在尝试对弹跳效果进行建模,即视图以相同的数量'n'向上滑动(Y值减小)然后向下滑动,然后视图又一次向上和向下滑动但这次为'n / 2'(所以一半的距离)。

I have a question concerning ObjectAnimator in Android. 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).

跳动较大,然后跳动变浅,即Mac图标在需要您注意时在托盘中执行的操作。

So a bigger bounce, followed by a shallower bounce - i.e., the kinda thing a Mac icon does in the tray when it wants your attention.

这是到目前为止我尝试过的(假设 v 视图 ):

Here is what I've tried so far (assume v is a 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();

忽略代码质量,这是一个POC!我希望这能起作用,但是尽管使用了 .before()

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().

您能告诉我如何创建复杂的AnimatorSet链,因为我似乎缺少了一些东西,所以它们不会融合在一起?

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?

非常感谢!

推荐答案

确定,所以我最终找到了一种相当整齐的方式来实现连续动画,方法是忽略流畅的生成器,而只需使用 playSequentially() 方法,使得:

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:

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();

除了在侦听器中涉及到回调onAnimationEnd的肮脏hack之外,仍然没有解决重复问题。必须是一种更简单的方法,以便也许有人在知道一个时就可以对其进行编辑。

Still haven't worked out repeating though, other than a dirty hack involving the callback onAnimationEnd in a listener. Must be a simpler way, so perhaps someone can edit this when they know of one.

无论如何,希望以上内容对某人有所帮助。

Anyway, hope the above helps someone.

这篇关于Android-AnimatorSet,Object Animator-反弹动画链是否融合在一起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆