【问题标题】:Use the same object animator property (translationx) twice on one object at the same time同时在一个对象上使用相同的对象动画属性 (translationx) 两次
【发布时间】:2019-01-17 07:17:23
【问题描述】:

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

【问题讨论】:

    标签: android class objectanimator animatorset


    【解决方案1】:

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

    Android Guide on Animation - Animation Listeners

    【讨论】:

    • 如果我将其中一个的属性更改为除了翻译 X 之外的其他内容,我拥有的代码它可以工作,但似乎有一个问题,我不能同时在一个上播放相同的属性目的。如果我将一个更改为翻译,为什么在另一个翻译 X 中它会起作用。是否有任何特殊的类可以让我运行到相同的对象动画师?感谢您的回复,谢谢。
    猜你喜欢
    • 1970-01-01
    • 2010-09-19
    • 2023-03-17
    • 2020-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多