rs, defStyleAttr);
116 this.mContext = context;
117 obtainAttributes(attrs);
118 }
119
120 /**
121 * Set the text that will be drawn on arc.
122 * @param drawStr the text content.
123 */
124 public void setDrawStr(String drawStr) {
125 this.mDrawStr = drawStr;
126 //refresh this view
127 invalidate();
128 }
129
130 /**
131 * Set the transparency of arc.
132 * @param mArcAlpha the value of transparency.
133 */
134 public void setArcAlpha(int mArcAlpha) {
135 this.mArcAlpha = mArcAlpha;
136 //refresh this view
137 invalidate();
138 }
139
140 @Override
141 protected void onDraw(Canvas canvas) {
142 super.onDraw(canvas);
143 //draw arc
144 Paint arcPaint = new Paint();
145 arcPaint.setStrokeWidth(mArcWidth);
146 arcPaint.setStyle(Paint.Style.STROKE);
147 arcPaint.setColor(mArcColor);
148 arcPaint.setAlpha(mArcAlpha);
149 int width = getWidth();
150 int height = getHeight();
151 float radius;
152 if (width > height) {
153 radius = mScale * height;
154 } else {
155 radius = mScale * width;
156 }
157 RectF oval = new RectF();
158
159 int center_x = width;
160 int center_y = height;
161
162 switch (mDrawStyle) {
163 case 0:
164 center_x = 0;
165 center_y = 0;
166 mStartAngle = 90;
167 mSweepAngle = -90;
168 break;
169 case 1:
170 center_x = 0;
171 center_y = height;
172 mStartAngle = 270;
173 mSweepAngle = 90;
174 break;
175 case 2:
176 center_x = width;
177 center_y = 0;
178 mStartAngle = 180;
179 mSweepAngle = -90;
180 break;
181 case 3:
182 center_x = width;
183 center_y = height;
184 mStartAngle = 180;
185 mSweepAngle = 90;
186 break;
187 case 4:
188 center_x = width / 2;
189 center_y = height / 2;
190 mStartAngle = 270;
191 mSweepAngle = 90;
192 break;
193 }
194 float left = center_x - radius;
195 float top = center_y - radius;
196 float right = center_x + radius;
197 float bottom = center_y + radius;
198 oval.set(left, top, right, bottom);
199 canvas.drawArc(oval, mStartAngle, mSweepAngle, false, arcPaint);
200
201 //draw text
202 Paint textPaint = new Paint();
203 textPaint.setTextSize(mTextSize);
204 textPaint.setStyle(Paint.Style.FILL);
205 textPaint.setColor(mTextColor);
206 Path path = new Path();
207 path.addArc(oval, mStartAngle, mSweepAngle);
208 canvas.drawTextOnPath(mDrawStr, path, mHOffset, mVOffset, textPaint);
209 }
210
211 /**
212 * Obtain custom attributes that been defined in attrs.xml.
213 * @param attrs A collection of attributes.
214 */
215 private void obtainAttributes(AttributeSet attrs) {
216 TypedArray ta = mContext.obtainStyledAttributes(attrs, R.styleable.ArcImageView);
217 mDrawStr = ta.getString(R.styleable.ArcImageView_drawStr);
218 mTextSize = ta.getDimension(R.styleable.ArcImageView_textSize, DEFAULT_TEXT_SIZE);
219 mArcAlpha = ta.getInteger(R.styleable.ArcImageView_arcAlpha, DEFAULT_ARC_ALPHA);
220 mArcWidth = ta.getInteger(R.styleable.ArcImageView_arcWidth, DEFAULT_ARC_WIDTH);
221 mStartAngle = ta.getInteger(R.styleable.ArcImageView_startAngle, DEFAULT_START_ANGLE);
222 mSweepAngle = ta.getInteger(R.styleable.ArcImageView_startAngle, DEFAULT_SWEEP_ANGLE);
223 mHOffset = ta.getInteger(R.styleable.ArcImageView_hOffset, DEFAULT_H_OFFSET);
224 mVOffset = ta.getInteger(R.styleable.ArcImageView_vOffset, DEFAULT_V_OFFSET);
225 mArc |