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

编程入门 行业动态 更新时间:2024-10-14 22:17:35
本文介绍了如何使用 ViewPropertyAnimator 生成循环动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我想构建一个 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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-21 00:50:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/993807.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   动画   ViewPropertyAnimator

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!