CircleLoadingView.java
2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.metroapp.widget;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import com.metroapp.utils.UIUtil;
/**
* 转圈圈
* @author Yu
*
*/
public class CircleLoadingView extends View {
private final Paint mLoadingPaint;
private final Paint mBgPaint;
private final RectF mLoadingArc = new RectF();
private final int radius;
private int mViewWidth, mViewHeight;
private int mBgColor = Color.WHITE;
public CircleLoadingView(Context context, AttributeSet attrs) {
super(context, attrs);
mLoadingPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
mLoadingPaint.setColor(Color.parseColor("#66CDAA"));
mLoadingPaint.setStyle(Style.STROKE);
mLoadingPaint.setStrokeWidth(UIUtil.dip2px(getContext(), 3));
radius = UIUtil.dip2px(getContext(), 11);
mBgPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
mBgPaint.setColor(mBgColor);
setClickable(true);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mViewWidth = w;
mViewHeight = h;
mLoadingArc.set(mViewWidth / 2 - radius, mViewHeight / 2 - radius, mViewWidth / 2 + radius, mViewHeight / 2 + radius);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(mViewWidth / 2, mViewHeight / 2, radius * 2, mBgPaint);
canvas.drawArc(mLoadingArc, mStartAngle, mSweepAngle, false, mLoadingPaint);
}
public void startLoading() {
removeCallbacks(mLoadingAnimator);
post(mLoadingAnimator);
}
private int mStartAngle = 0;
private int mSweepAngle = 0;
boolean isIncrease = true;
private final Runnable mLoadingAnimator = new Runnable() {
public void run() {
if (isIncrease && mSweepAngle > 270) {
isIncrease = false;
} else if (!isIncrease && mSweepAngle < 0) {
isIncrease = true;
}
if (isIncrease) {
mSweepAngle += 8;
mStartAngle += 8;
} else {
mSweepAngle -= 8;
mStartAngle += 16;
}
if (mStartAngle > 360) mStartAngle -= 360;
invalidate();
postDelayed(this, 16);
}
};
public void setBgColor(int color) {
mBgColor = color;
mBgPaint.setColor(color);
}
}