package io.dcloud.H58E83894.weiget;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import androidx.annotation.Nullable;
import com.blankj.utilcode.util.SizeUtils;
import io.dcloud.H58E83894.R;
/**
* Author: Night
* 圆环
* Date: 2021/2/9 16:13
* Description: 还需要添加 占比文字
*/
public class RingCircleView extends View {
private int rightColor;//正确部分颜色
private int errorColor;//错误部分颜色
private int rightLineWidth;
private int errorLineWidth;
private Paint rightPaint;
private Paint errorPaint;
private Paint whitePaint;
private Path mPath;
private Path rightPath;
private Path mErrorPath;
private Path errorPath;
private int mWidth;
private int mHeight;
private int radius;
private int errorRadius;
private String TAG="RingView";
private double mBili=0.99;
public RingCircleView(Context context) {
this(context,null);
}
public RingCircleView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs,0);
}
public RingCircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
Log.e(TAG, "RingView: " );
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RingCircleView);
rightColor=typedArray.getColor(R.styleable.RingCircleView_rightColor,Color.GREEN);
errorColor=typedArray.getColor(R.styleable.RingCircleView_errorColor,Color.RED);
rightLineWidth=typedArray.getInt(R.styleable.RingCircleView_rightLineWidth, SizeUtils.dp2px(30));
errorLineWidth=typedArray.getInt(R.styleable.RingCircleView_errorLineWidth,SizeUtils.dp2px(20));
radius=typedArray.getInt(R.styleable.RingCircleView_ringRadius,SizeUtils.dp2px(40));
typedArray.recycle();
initPaint();
initPath();
setBackgroundColor(Color.WHITE);
}
private void initPath() {
mPath=new Path();
mErrorPath=new Path();
}
private void initPaint() {
rightPaint=new Paint();
rightPaint.setDither(true);
rightPaint.setAntiAlias(true);
rightPaint.setStrokeWidth(rightLineWidth);
rightPaint.setColor(rightColor);
rightPaint.setStyle(Paint.Style.STROKE);
whitePaint=new Paint();
whitePaint.setDither(true);
whitePaint.setAntiAlias(true);
whitePaint.setStrokeWidth(rightLineWidth+1);
whitePaint.setColor(Color.WHITE);
whitePaint.setStyle(Paint.Style.STROKE);
errorPaint=new Paint();
errorPaint.setDither(true);
errorPaint.setAntiAlias(true);
errorPaint.setStrokeWidth(errorLineWidth);
errorPaint.setColor(errorColor);
errorPaint.setStyle(Paint.Style.STROKE);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mWidth=w;
mHeight=h;
}
/**
*
* @param pro
*/
public void setProportion(int pro,int max){
double bili = pro / max;
mBili=bili;
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPath.addCircle(mWidth/2,mHeight/2,radius, Path.Direction.CCW);
errorRadius=radius-(rightLineWidth-errorLineWidth)/2;
mErrorPath.addCircle(mWidth/2,mHeight/2,errorRadius, Path.Direction.CCW);
PathMeasure pathMeasure=new PathMeasure(mPath,false);
rightPath=new Path();
pathMeasure.getSegment( (float) (pathMeasure.getLength()*mBili),pathMeasure.getLength(),rightPath,true);
canvas.drawPath(rightPath,rightPaint);
PathMeasure errorPathMeasure=new PathMeasure(mErrorPath,false);
errorPath=new Path();
errorPathMeasure.getSegment(0, (float) (errorPathMeasure.getLength()*(mBili)),errorPath,true);
canvas.drawPath(errorPath,errorPaint);
}
}
<declare-styleable name="RingCircleView">
<attr name="rightColor" format="color"></attr>
<attr name="errorColor" format="color"></attr>
<attr name="rightLineWidth" format="integer"></attr>
<attr name="errorLineWidth" format="integer"></attr>
<attr name="ringRadius" format="integer"></attr>
</declare-styleable>
使用:
<com.thinku.ring.ringview.RingCircleView
android:layout_width="match_parent"
android:layout_height="200dp">
</com.thinku.ring.ringview.RingCircleView>
效果: