如何生成循环动画与ViewPropertyAnimator? [英] How to generate looping animation with ViewPropertyAnimator?
问题描述
我想建立TextViews的动画,这只是完成后重演。
I want to build an animation of TextViews, which repeats itself just after completion.
对于每个视图我想动画,我用的是下面这段code
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);
我试图把最后一块code到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.
在此先感谢
推荐答案
嗯,我会再回答我。
TranslateAnimation类有关于重复动画的方法,所以我用,而不是ViewPropertyAnimator吧。
TranslateAnimation class has methods about repeating the animation, so I used it instead of ViewPropertyAnimator.
下面code似乎工作:
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);
}
这篇关于如何生成循环动画与ViewPropertyAnimator?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!