拿到这个需求,首先想到的是自定义一个ImageView来实现此功能,即在onDraw()中绘制圆弧和文字。同时因为要保证圆弧的位置可以任意摆放,圆弧的颜色、透明度以及文字大小、颜色等都是可控的,所以增加了一些自定义属性。实现代码非常简单,如下:
1 package com.chunk.customviewsdemo.views.ArcImageView;
2
3 import android.content.Context;
4 import android.content.res.TypedArray;
5 import android.graphics.Canvas;
6 import android.graphics.Paint;
7 import android.graphics.Path;
8 import android.graphics.RectF;
9 import android.util.AttributeSet;
10 import android.widget.ImageView;
11
12 import com.chunk.customviewsdemo.R;
13
14 /**
15 * Description:A custom ImageView with circular arc and text
16 * Author: XiaoYu
17 * Date: 2016/5/10 13:55
18 */
19 public class ArcImageView extends ImageView {
20 /**
21 * The default text size.
22 */
23 private final float DEFAULT_TEXT_SIZE = 20;
24 /**
25 * The default scale value which decides the width of arc.
26 */
27 private final float DEFAULT_SCALE = 0.5f;
28 /**
29 * The default transparency of arc.
30 */
31 private final int DEFAULT_ARC_ALPHA =100;
32 /**
33 * The default width of arc.
34 */
35 private final int DEFAULT_ARC_WIDTH =160;
36 /**
37 * The default angle that the arc starts with.
38 */
39 private final int DEFAULT_START_ANGLE = 180;
40 /**
41 * The default angle that the arc.
42 */
43 private final int DEFAULT_SWEEP_ANGLE = 90;
44 /**
45 * The default distance along the path to add to the text's starting position.
46 */
47 private final int DEFAULT_H_OFFSET = 100;
48 /**
49 * The default distance above(-) or below(+) the path to position the text.
50 */
51 private final int DEFAULT_V_OFFSET = 20;
52 private Context mContext;
53 /**
54 * The text displayed on ImageView along arc.
55 */
56 private String mDrawStr;
57 /**
58 * The font size of text.
59 */
60 private float mTextSize = DEFAULT_TEXT_SIZE;
61 /**
62 * The scale value which decides the width of arc.
63 */
64 private float mScale = DEFAULT_SCALE;
65 /**
66 * The transparency of arc.
67 */
68 private int mArcAlpha = DEFAULT_ARC_ALPHA;
69 /**
70 * The width of arc.
71 */
72 private int mArcWidth = DEFAULT_ARC_WIDTH;
73 /**
74 * The start angle of angle.
75 */
76 private int mStartAngle = DEFAULT_START_ANGLE;
77 /**
78 * The swept angle of angle.
79 */
80 private int mSweepAngle = DEFAULT_SWEEP_ANGLE;
81 /**
82 * The default distance along the path to add to the text's starting position.
83 */
84 private float mHOffset = DEFAULT_H_OFFSET;
85 /**
86 * The default distance above(-) or below(+) the path to position the text.
87 */
88 private float mVOffset = DEFAULT_V_OFFSET;
89 /**
90 * The style of arc, all styles includes LEFT_TOP, LEFT_BOTTOM, RIGHT_TOP, RIGHT_BOTTOM, CENTER。
91 * of course, you can add your own style according to your demands.
92 */
93 private int mDrawStyle;
94 /**
95 * The color of arc.
96 */
97 private int mArcColor;
98 /**
99 * The color of text.
100 */
101 private int mTextColor;
102
103 public ArcImageView(Context context) {
104 super(context);
105 this.mContext = context;
106 }
107
108 public ArcImageView(Context context, AttributeSet attrs) {
109 super(context, attrs);
110 this.mContext = context;
111 obtainAttributes(attrs);
112 }
113
114 public ArcImageView(Context context, AttributeSet attrs, int defStyleAttr) {
115 super(context, att