Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
378 views
in Technique[技术] by (71.8m points)

android - How to generate looping animation with ViewPropertyAnimator?

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

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.

Thanks in advance

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Well, I am going to answer myself again.

TranslateAnimation class has methods about repeating the animation, so I used it instead of ViewPropertyAnimator.

The following code seems to work:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...