如何使用 ViewPropertyAnimator 生成循环动画? [英] How to generate looping animation with ViewPropertyAnimator?

查看:24
本文介绍了如何使用 ViewPropertyAnimator 生成循环动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to build an animation of TextViews, which repeats itself just after completion.

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

For each View I want to animate, I use the following piece of code

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); 

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

I tried to place the last piece of code into the onAnimationEnd, but Java will not compile since it considers the object listener as not initialized. Moreover, I don't think that this "recursive" animation invocation is a good solution, it was the first thing which came to my mind. I am suspicious that there is a simple and sound way to implement looping property animation, but I failed to locate it, so I turned here for help.

提前致谢

推荐答案

好吧,我又要自己回答了.

Well, I am going to answer myself again.

TranslateAnimation 类有关于重复动画的方法,所以我用它代替了 ViewPropertyAnimator.

TranslateAnimation class has methods about repeating the animation, so I used it instead of 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);
            }

这篇关于如何使用 ViewPropertyAnimator 生成循环动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆