我有一个要执行的动画,其中包含一些水平对齐的箭头,箭头的alpha值会改变以实现动画效果(即第一个箭头有alpha 1.0,然后第二个箭头会得到一个值1.0等。)。
所以,如果我有这样的功能:
void highlightFirstArrow()
{
mArrow1.setAlpha(1.0f);
mArrow2.setAlpha(0.75f);
mArrow3.setAlpha(0.50f);
mArrow4.setAlpha(0.20f);
}
然后我想开始,重复多次,然后停止这样的功能:
void animateArrows()
{
highlightFirstArray();
pause;
highlightSecondArray();
pause;
etc.
}
显然,如果GUI线程是在for for look中执行的话,这会锁定GUI线程。有什么方法可以达到理想的目标:
- run a for loop in a separate thread
- don't use a loop, instead constantly execute the functions individually via a timer
- use built in specific android animation mechanisms. If so which is most appropriate? Would AnimatorSet() be good for this scenario, or something else
答案 0 :(得分:0)
你绝对不应该使用任何循环或计时器。有很多内置的类可以帮助动画你的视图。例如,您可以使用ValueAnimator
:
ValueAnimator.ofFloat(1f, 0.2f).setDuration(1000).addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override public void onAnimationUpdate(ValueAnimator animation) {
float value = (float) animation.getAnimatedValue();
arrow1.setAlpha(value);
arrow2.setAlpha(value);
}
});