1. 圆圈通过Canvas去绘制
通过canvas.draw Arc 去绘制 startAngle则是绘制开始的角度 通过加上 incrementAngle这一个变量是为了更好去做动画控制。
private void startAnimation(){
if (animatorSet != null && animatorSet.isRunning()) {
animatorSet.cancel() ; // 取消动画
}
animatorSet = new AnimatorSet() ; //设置一个动画集合
AnimatorSet set = circuAnimator(); // 创建运行一圈动画的AnimatorSet
animatorSet.play(set) ;
animatorSet.addListener(new AnimatorListener() {
private boolean isCancel = false ;
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
if (!isCancel) {
startAnimation() ; // 不停的去循环动画
}
}
@Override
public void onAnimationCancel(Animator animation) {
isCancel = true ;
}
}) ;
animatorSet.start() ;
}
//默认的动画时间
private int DEFULT_DURATION = 660 ;
/**
* 循环的动画
*/
private AnimatorSet circuAnimator(){
//从小圈到大圈
ValueAnimator holdAnimator1 = ValueAnimator.ofFloat(incrementAngele + DEFULT_MIN_ANGLE , incrementAngele + 115f) ;
holdAnimator1.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
incrementAngele = (float) animation.getAnimatedValue() ;
}
}) ;
holdAnimator1.setDuration(DEFULT_DURATION ) ;
holdAnimator1.setInterpolator(new LinearInterpolator()) ;
ValueAnimator expandAnimator = ValueAnimator.ofFloat(DEFULT_MIN_ANGLE , DEFULT_MAX_ANGLE) ;
expandAnimator.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
sweepAngle = (float) animation.getAnimatedValue() ;
incrementAngele -= sweepAngle ;
invalidate() ;
}
}) ;
expandAnimator.setDuration(DEFULT_DURATION) ;
expandAnimator.setInterpolator(new DecelerateInterpolator(2)) ;
//从大圈到小圈
ValueAnimator holdAnimator = ValueAnimator.ofFloat(startAngle , startAngle + 115f) ;
holdAnimator.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
startAngle = (float) animation.getAnimatedValue() ;
}
});
holdAnimator.setDuration(DEFULT_DURATION ) ;
holdAnimator.setInterpolator(new LinearInterpolator()) ;
ValueAnimator narrowAnimator = ValueAnimator.ofFloat(DEFULT_MAX_ANGLE , DEFULT_MIN_ANGLE) ;
narrowAnimator.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
sweepAngle = (float) animation.getAnimatedValue() ;
invalidate() ;
}
}) ;
narrowAnimator.setDuration(DEFULT_DURATION) ;
narrowAnimator.setInterpolator(new DecelerateInterpolator(2)) ;
AnimatorSet set = new AnimatorSet() ;
set.play(holdAnimator1 ).with(expandAnimator) ;
set.play(holdAnimator).with(narrowAnimator).after(holdAnimator1);
return set ;
}