昨天同事遇到如下效果的加载view,实现了一下
1.首先创建一个类继承view.
public ProgreesView(Context context) {
this(context,null);
}
public ProgreesView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public ProgreesView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mContext = context;
p = new Paint();
}
2.测量view的大小
MeasureSpec的specMode,一共三种类型:
EXACTLY:一般是设置了明确的值或者是MATCH_PARENT
AT_MOST:表示子布局限制在一个最大值内,一般为WARP_CONTENT
UNSPECIFIED:表示子布局想要多大就多大,很少使用
这里给出EXACTLY的测量值,AT_MOST模式给了一个固定值
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
if (widthMode == MeasureSpec.EXACTLY) {
mWidth = widthSize;
} else {
mWidth = 200;
}
if (heightMode == MeasureSpec.EXACTLY) {
mHeight = heightSize;
} else {
mHeight = 200;
}
setMeasuredDimension(mWidth, mHeight);
}
3.重写onDraw方法
@Override
protected void onDraw(Canvas canvas) {
p.setStyle(Paint.Style.STROKE);
p.setStrokeWidth(8);
p.setAntiAlias(true);
//百分比弧的矩形
RectF rectF = new RectF(5, 5, mWidth - 5, mHeight - 5);
p.setColor(Color.parseColor("#2EA4F2"));
if (two < 360){
if (progress < 360)
progress += 10;
if (progress == 360)
two += 5;
int per = progress-two;
//绘制圆
canvas.drawArc(rectF,two,per,false,p);
// if (progress != 360){
postInvalidateDelayed(100);
// }
Log.e("two ",two + "");
Log.e("progress ",progress + "");
}
}
这里用了canvas.drawArc画弧度 这个方法有5个参数,第一个参数Recf代表绘制的区域,第二个参数是其实的度数,第三个参数是画的弧度数. 刚开始先画360度弧,接着当第一条弧画满360度的时候,去不断改变起始弧的度数和所画弧的区域。