首页 > 解决方案 > 同时在一个对象上使用相同的对象动画属性 (translationx) 两次

问题描述

我花了一整天的时间试图弄清楚这一点。我尝试了过多的代码组合,但没有一个想要工作。从技术上讲,它们确实有效,但不是我想要的方式。如果我添加 translationY 属性,它就可以工作。我基本上想同时在一个对象上运行 2 个动画,两个是translationx。对象应该在整个屏幕宽度上从左到右移动,并同时前后移动一小段距离。所以主要的问题是,是否有可能实现这一点,或者不能同时使用与 AnimatorSet 相同的属性?

这是我正在使用的当前代码:

private void _ballLevel20Animation () {
    move1.cancel();
    int center = (board.getMeasuredWidth() / 2);
    int lr = board.getMeasuredWidth();
    final float left = Float.valueOf(100 - center);
    final float right = Float.valueOf(center - 100);
    int center1 = (board.getMeasuredWidth() / 6);
    final float left1 = Float.valueOf(100 - center);
    final float right1 = Float.valueOf(center - 100);
    move1.setTarget(ball);
    move1.setPropertyName("translationX");
    move1.setFloatValues(left, right);
    move1.setRepeatCount(ObjectAnimator.INFINITE);
    move1.setDuration((int)(ball_duration_increa));
    move1.setRepeatMode(ValueAnimator.REVERSE);
    bounce_ani.setTarget(ball);
    bounce_ani.setPropertyName("translationX");
    bounce_ani.setFloatValues((float)(SketchwareUtil.getDip(getApplicationContext(), (int)(-20))), (float)(SketchwareUtil.getDip(getApplicationContext(), (int)(20))));
    bounce_ani.setRepeatCount(ObjectAnimator.INFINITE);
    bounce_ani.setDuration((int)(ball_duration_increa / 6));
    bounce_ani.setRepeatMode(ValueAnimator.REVERSE);
    AnimatorSet animatorSet = new AnimatorSet();

    animatorSet.play(bounce_ani).with(move1);

    animatorSet.start();
    /*bounce_ani.setFloatValues(right1, left1);*/
}

标签: androidclassobjectanimatoranimatorset

解决方案


您可以尝试向动画添加动画侦听器。在监听器中,有 onAnimationEnd() 在动画完成时被调用。在这里,您可以调用后续动画,使它们看起来像是被链接的。

Android 动画指南 - 动画监听器


推荐阅读