(1)一个简单的自定义view的实现。设计到paint,自定义属性,invalidate,canvas的使用。
下面是实现的效果:圆弧的绘制,以及字体的绘制,圆弧的动画。
(2)主要代码实现
public class CircleView extends View {
private int textNumber,time;
private float itemNumber;
private TextPaint mTextPaint;
private Paint mArcPaint,mProgressPaint;
private int mArcColor,mProgressColor,mTextColor;
private int mArcWidth,mTextSize;
private float radio;
private float startAngle,scrollowAngle;
private float progress;
private int mWidth,mHeight;
private int text = 0;
public void setProgress(float progress) {
this.progress = progress;
if (progress > 0){
text = (int) (itemNumber * progress);
if (progress >= scrollowAngle){
text = textNumber;
}
}
invalidate();
}
public int getTime() {
return time;
}
public float getScrollowAngle() {
return scrollowAngle;
}
public CircleView(Context context) {
this(context,null);
}
public CircleView(Context context, @Nullable AttributeSet attrs) {
this(context,attrs,0);
}
public CircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context,attrs);
}
private void init(Context context,AttributeSet attrs){
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.circle_view_style);
mArcColor = array.getColor(R.styleable.circle_view_style_arc_color, Color.RED);
mProgressColor = array.getColor(R.styleable.circle_view_style_progress_color,Color.BLUE);
mArcWidth = array.getDimensionPixelOffset(R.styleable.circle_view_style_arc_width,20);
mTextColor = array.getColor(R.styleable.circle_view_style_text_color,Color.GRAY);
mTextSize = array.getDimensionPixelSize(R.styleable.circle_view_style_text_size,10);
time = array.getInteger(R.styleable.circle_view_style_progress_time,10000);
textNumber = array.getInteger(R.styleable.circle_view_style_max_number,1000);
radio = array.getDimensionPixelOffset(R.styleable.circle_view_style_arc_radio,100);
startAngle = array.getFloat(R.styleable.circle_view_style_start_angle,180);
scrollowAngle = array.getFloat(R.styleable.circle_view_style_scrollow_angle,180);
array.recycle();
mArcPaint = new Paint();
mArcPaint.setStyle(Paint.Style.STROKE);
mArcPaint.setColor(mArcColor);
mArcPaint.setStrokeWidth(mArcWidth);
mArcPaint.setAntiAlias(true);
mArcPaint.setStrokeCap(Paint.Cap.ROUND);
mProgressPaint = new Paint();
mProgressPaint.setStyle(Paint.Style.STROKE);
mProgressPaint.setColor(mProgressColor);
mProgressPaint.setStrokeWidth(mArcWidth);
mProgressPaint.setAntiAlias(true);
mProgressPaint.setStrokeCap(Paint.Cap.ROUND);
mTextPaint = new TextPaint();
mTextPaint.setTextSize(mTextSize);
mTextPaint.setColor(mTextColor);
mTextPaint.setTypeface(Typeface.DEFAULT_BOLD);
itemNumber = textNumber/scrollowAngle;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
mWidth = getMeasuredWidth();
mHeight = getMeasuredHeight();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
RectF arcRect = new RectF(mWidth/2 - radio +mArcWidth,mHeight/2-radio+mArcWidth,mWidth/2 + radio-mArcWidth,mHeight/2+radio-mArcWidth);
canvas.drawArc(arcRect,startAngle,scrollowAngle,false,mArcPaint);
RectF progressRect = new RectF(mWidth/2 - radio +mArcWidth,mHeight/2-radio+mArcWidth,mWidth/2 + radio-mArcWidth,mHeight/2+radio-mArcWidth);
canvas.drawArc(progressRect,startAngle,progress,false,mProgressPaint);
String content = "计数:"+text;
Log.e("test","aa=="+mTextPaint.measureText(content)/2);
float textX = mWidth/2 - mTextPaint.measureText(content)/2;
float textY = mHeight/2+(mTextPaint.ascent()+mTextPaint.density)/2;
canvas.drawText(content,textX,textY,mTextPaint);
}
}