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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
| public class ArcView extends View {
private final int MAX_SWEEP_ANGLE = 240; private final int START_SWEEP_ANGLE = 150; private final int DEFAULT_MAX_PROGRESS = 100;
private final int DEFAULT_ARC_COLOR = Color.RED; private final int DEFAULT_BG_COLOR = Color.DKGRAY; private final int DEFAULT_TEXT_COLOR = Color.BLACK; private final int DEFAULT_TEXT_SIZE = 40;
private int mArcColor = DEFAULT_ARC_COLOR; private int mBgColor = DEFAULT_BG_COLOR; private int mTextColor = DEFAULT_TEXT_COLOR; private int mTextSize = DEFAULT_TEXT_SIZE;
private int progress = 0; private int mMaxProgress = DEFAULT_MAX_PROGRESS;
private Paint mCirclePaint; private Paint mBgPaint; private Paint mTextPaint;
private final Rect mTextBound = new Rect();
public ArcView(Context context) { this(context, null); }
public ArcView(Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); }
public ArcView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); final TypedArray attributes = getContext().obtainStyledAttributes(attrs, R.styleable.ArcView);
mArcColor = attributes .getColor( R.styleable.ArcView_arcColor, DEFAULT_ARC_COLOR);
mBgColor = attributes .getColor( R.styleable.ArcView_bgColor, DEFAULT_BG_COLOR);
mTextColor = attributes .getColor( R.styleable.ArcView_arc_textColor, DEFAULT_TEXT_COLOR);
mTextSize = (int) attributes .getDimension( R.styleable.ArcView_arc_textSize, DEFAULT_TEXT_SIZE); attributes.recycle(); init(); }
private void init() { mCirclePaint = new Paint(); mCirclePaint.setColor(mArcColor); mCirclePaint.setStrokeWidth(8.0F); mCirclePaint.setDither(true); mCirclePaint.setAntiAlias(true); mCirclePaint.setStyle(Paint.Style.STROKE);
mBgPaint = new Paint(); mBgPaint.setColor(mBgColor); mBgPaint.setStrokeWidth(20.0F); mBgPaint.setAntiAlias(true); mBgPaint.setStyle(Paint.Style.STROKE); mBgPaint.setStrokeCap(Paint.Cap.ROUND);
mTextPaint = new Paint(); mTextPaint.setStrokeWidth(4);
int size = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, mTextSize, getResources().getDisplayMetrics()); mTextPaint.setTextSize(size); mTextPaint.setAntiAlias(true); mTextPaint.setColor(mTextColor); mTextPaint.setTextAlign(Paint.Align.LEFT); }
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); int paddingLeft = getPaddingLeft(); int paddingRight = getPaddingRight(); int paddingTop = getPaddingTop(); int paddingBottom = getPaddingBottom();
int circleWidth = getWidth() - paddingLeft - paddingRight; int circleHeight = getHeight() - paddingTop - paddingBottom;
int radius = Math.min(circleWidth, circleHeight) / 2;
int left = getLeft() + paddingLeft; int right = left + radius * 2; int top = getTop() + getPaddingTop(); int bottom = top + 2 * radius; canvas.drawArc(left, top, right, bottom, START_SWEEP_ANGLE, MAX_SWEEP_ANGLE, false, mBgPaint);
int sweepArc = MAX_SWEEP_ANGLE * progress / mMaxProgress; canvas.drawArc(left, top, right, bottom, START_SWEEP_ANGLE, sweepArc, false, mCirclePaint);
String text = String.valueOf(progress) + "%"; mTextPaint.getTextBounds(text, 0, text.length(), mTextBound); canvas.drawText(text, (left + right) / 2 - mTextBound.width() / 2, (top + bottom) / 2 + mTextBound.height() / 2, mTextPaint); }
public void setProgress(int progress) { if (progress < 0 || progress > mMaxProgress) { return; } this.progress = progress; invalidate(); }
public void setMaxProgress(int maxProgress) { this.mMaxProgress = maxProgress; } }
|