public class ComLoadingView extends Button {
private Paint mPaint;
private float mWidth = 0f;
private float mHeight = 0f;
private float mMaxRadius = 9;
private float mPadding = 21;
private int circularCount = 3;
private int mJumpValue = 0;
private int mAniTime = 250;
private int hightColor = 0xFFFFFFFF;
private int middleColor = 0xCCFFFFFF;
private int lowColor = 0x44FFFFFF;
private boolean mIsRest = false;
private final Handler mHandler = new Handler(Looper.getMainLooper());
private Runnable mUpdateTimeRunner = new Runnable() {
@Override
public void run() {
mJumpValue--;
postInvalidate();
mHandler.removeCallbacks(mUpdateTimeRunner);
mHandler.postDelayed(mUpdateTimeRunner, mAniTime);
}
};
public ComLoadingView(Context context, AttributeSet attrs) {
super(context, attrs);
}
private void initPaint(int jumpValue) {
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setStyle(Paint.Style.FILL);
if (jumpValue == 0) {
mPaint.setColor(hightColor);
} else if (jumpValue == -1) {
mPaint.setColor(middleColor);
} else {
mPaint.setColor(lowColor);
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (!isEnabled() && mIsRest) {
float startDistance = mWidth / 2 - mPadding - mMaxRadius * 2;
for (int i = 0; i < circularCount; i++) {
int index = (i + mJumpValue) % circularCount;
initPaint(index);
canvas.drawCircle(startDistance + i * (mPadding + mMaxRadius * 2), mHeight / 2, mMaxRadius, mPaint);
}
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
mWidth = getMeasuredWidth();
mHeight = getMeasuredHeight();
}
public void startAnim() {
stopAnim();
mIsRest = true;
mHandler.post(mUpdateTimeRunner);
}
public void stopAnim() {
if (mHandler != null) {
mIsRest = false;
mJumpValue = 0;
mHandler.removeCallbacks(mUpdateTimeRunner);
invalidate();
}
}
}