首先在value文件夹当中创建attrs.xml文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CircleView">
<attr name="circleColor" format="color"/>
<attr name="arcColor" format="color"/>
<attr name="circleRadius" format="dimension"/>
<attr name="arcStartAngle" format="integer"/>
<attr name="arcSweepAngle" format="integer"/>
</declare-styleable>
</resources>
然后在代码中创建CircleView类
package com.animee.day411;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
/**
* Created by Administrator on 2018/3/19.
*/
public class CircleView extends View{
private Paint circlePaint;
private Paint pathPaint;
private int startAngle,sweepAngle;
private int radius;
public CircleView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initPaint();
TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.CircleView);
int arccolor = typedArray.getColor(R.styleable.CircleView_arcColor, Color.GREEN);
pathPaint.setColor(arccolor);
int circlecolor = typedArray.getColor(R.styleable.CircleView_circleColor, Color.RED);
circlePaint.setColor(circlecolor);
radius = (int) typedArray.getDimension(R.styleable.CircleView_circleRadius, 100);
startAngle = typedArray.getInt(R.styleable.CircleView_arcStartAngle, 0);
sweepAngle = typedArray.getInt(R.styleable.CircleView_arcSweepAngle, 90);
typedArray.recycle();
}
/**
* 初始化画笔
* */
private void initPaint() {
circlePaint = new Paint();
circlePaint.setStyle(Paint.Style.FILL);
circlePaint.setAntiAlias(true);
pathPaint = new Paint();
pathPaint.setStyle(Paint.Style.FILL);
pathPaint.setAntiAlias(true);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(getHeight()/2,getWidth()/2,radius,circlePaint);
canvas.drawArc(0,0,getWidth(),getHeight(),startAngle,sweepAngle,true,pathPaint);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int hsize = MeasureSpec.getSize(heightMeasureSpec);
int wsize = MeasureSpec.getSize(widthMeasureSpec);
int hmode = MeasureSpec.getMode(heightMeasureSpec);
int wmode = MeasureSpec.getMode(widthMeasureSpec);
switch (wmode) {
case MeasureSpec.AT_MOST:
if (hmode== MeasureSpec.AT_MOST) {
int isize = radius * 2;
wsize = hsize = isize;
}else{
int isize = radius * 2;
int min = Math.min(isize, hsize);
wsize = hsize = min;
}
break;
case MeasureSpec.EXACTLY:
if (hmode== MeasureSpec.AT_MOST) {
int isize = radius * 2 + getPaddingLeft() + getPaddingRight();
int min = Math.min(isize, wsize);
wsize = hsize = min;
}else{
int min = Math.min(wsize, hsize);
radius = min/2;
wsize = hsize = min;
}
break;
}
setMeasuredDimension(wsize,hsize);
}
}
然后在布局中引入这个类文件 circle_test
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<com.animee.day411.CircleView
android:id="@+id/cv"
android:layout_width="200dp"
android:layout_height="200dp"
app:circleColor="#FF00F5"
app:arcColor="#00FFFF"
app:arcStartAngle="-90"
app:arcSweepAngle="180"
app:circleRadius="50dp"/>
</LinearLayout>
然后在grdiview当中使用。