我花了一整天的时间试图弄清楚这一点。我尝试了过多的代码组合,但没有一个想要工作。从技术上讲,它们确实有效,但不是我想要的方式。如果我添加 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);*/
}