Android ObjectAnimator 无限循环播放,实现上下左右浮动效果

Android Property Animation 动画有很多大神写过,都非常好,这里只通过ObjectAnimator /ValueAnimator实现上下左右浮动效果。

很简单看代码就好

private void floatAnim(View view,int delay){
    List<Animator> animators = new ArrayList<>();
    ObjectAnimator translationXAnim = ObjectAnimator.ofFloat(view, "translationX", -6.0f,6.0f,-6.0f);
    translationXAnim.setDuration(1500);
    translationXAnim.setRepeatCount(ValueAnimator.INFINITE);//无限循环
    translationXAnim.setRepeatMode(ValueAnimator.INFINITE);//
    translationXAnim.start();
    animators.add(translationXAnim);
    ObjectAnimator translationYAnim = ObjectAnimator.ofFloat(view, "translationY", -3.0f,3.0f,-3.0f);
    translationYAnim.setDuration(1000);
    translationYAnim.setRepeatCount(ValueAnimator.INFINITE);
    translationYAnim.setRepeatMode(ValueAnimator.INFINITE);
    translationYAnim.start();
    animators.add(translationYAnim);

    AnimatorSet btnSexAnimatorSet = new AnimatorSet();
    btnSexAnimatorSet.playTogether(animators);
    btnSexAnimatorSet.setStartDelay(delay);
    btnSexAnimatorSet.start();
}

开始设置setRepeatMode(ValueAnimator.INFINITE);方法没有效果,只有设置setRepeatCount(ValueAnimator.INFINITE)才可以。