如何使用ViewPropertyAnimator生成循环动画?

Ufu*_*ici 10 animation android android-animation

我想构建一个TextViews的动画,它在完成后重复.

对于我想要动画的每个视图,我使用以下代码

final float oldX = v.getX();
final float newX = v.getX() - (float)totalWidth;
final AnimatorListenerAdapter listener = new AnimatorListenerAdapter() {
    @Override
    public void onAnimationEnd(Animator animation) {
        v.setX(oldX);
        animFinished = true;

        //This line won't compile
        //v.animate().setDuration(animDuration).setInterpolator(newsInterpolator)
        //    .setListener(listener).x(newX);
    }
};

v.animate().setDuration(animDuration).setInterpolator(newsInterpolator)
    .setListener(listener).x(newX); 
Run Code Online (Sandbox Code Playgroud)

我试图将最后一段代码放入onAnimationEnd,但Java不会编译,因为它认为对象监听器没有初始化.而且,我不认为这种"递归"动画调用是一个很好的解决方案,这是我想到的第一件事.我怀疑有一种简单而有效的方法来实现循环属性动画,但是我找不到它,所以我转过来寻求帮助.

提前致谢

Ufu*_*ici 8

好吧,我将再次回答自己.

TranslateAnimation类有关于重复动画的方法,所以我使用它而不是ViewPropertyAnimator.

以下代码似乎有效:

            long duration = 1000* ((long)totalWidth / newsScrollSpeed);
            System.out.println("totalWidth="+totalWidth);
            TranslateAnimation  anim = new TranslateAnimation(0,-totalWidth,0,0);
            anim.setInterpolator(linearInterpolator);
            anim.setDuration(duration);
            anim.setRepeatCount(TranslateAnimation.INFINITE);
            anim.setRepeatMode(TranslateAnimation.RESTART);

            for(i=0;i<this.getChildCount();i++)
            {
                View v = this.getChildAt(i);

                if(v.getId() == R.id.yuruyen_yazi)
                {
                    continue;
                }

                v.startAnimation(anim);
            }
Run Code Online (Sandbox Code Playgroud)