只是记录下学习的过程哈
刚开始研究这个东西,这个也是之前项目里遇到的一个 ,当时百度好久都没找到,现在自己返回来写了写 还有好多小问题,慢慢处理
/**
* author : 桶哥二号
* motto : Anything is possible
* date : 2019-12-1211:23
* desc : 我好难呀,我太难了呀
* version: 1.0
*/
public class CustomProgressBar extends View {
private int startColor;//起始颜色
private int modleColor;//中间颜色
private int endColor;//末尾颜色
private float startPercent;//起始百分比
private float modlePercent;//中间百分比
private float startPercen;//起始百分比
private float modlePercen;//中间百分比
private float endPercent;//末尾百分比
private float progressWidth;//进度条的宽度
private int defaultWidth;
private int defaultHeight;
private int width;//计算出的宽度
private int height;//计算出的高度
private int paddingLeft = getPaddingLeft();
private Paint paintS = null;
private Paint paintM = null;
private Paint paintE = null;
int textColor;
public CustomProgressBar(Context context) {
super(context);
}
public CustomProgressBar(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public CustomProgressBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
getScreenWidth(context);
Log.i("屏幕的宽度为:", "--------" + getScreenWidth(context));
defaultWidth = DpOrPxUtils.dip2px(context, (getScreenWidth(context) - 200) / 2);
defaultHeight = DpOrPxUtils.dip2px(context, 30);
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomLines);
startColor = ta.getColor(R.styleable.CustomLines_startColor, Color.RED);
modleColor = ta.getColor(R.styleable.CustomLines_modleColor, Color.GREEN);
endColor = ta.getColor(R.styleable.CustomLines_endColor, Color.YELLOW);
TypedArray taPercent = context.obtainStyledAttributes(attrs, R.styleable.CustomPercent);
startPercent = taPercent.getFloat(R.styleable.CustomPercent_startPercent, 300);
modlePercent = taPercent.getFloat(R.styleable.CustomPercent_startPercent, 500);
endPercent = taPercent.getFloat(R.styleable.CustomPercent_endPercent, 600);
progressWidth = taPercent.getFloat(R.styleable.CustomPercent_progressWidth, 20);
paintS = new Paint(Paint.ANTI_ALIAS_FLAG);
paintS.setStyle(Paint.Style.FILL);
paintS.setStrokeWidth(progressWidth);
paintS.setStrokeCap(Paint.Cap.ROUND);
paintS.setColor(startColor);
paintM = new Paint(Paint.ANTI_ALIAS_FLAG);
paintM.setStyle(Paint.Style.FILL);
paintM.setStrokeWidth(progressWidth);
paintM.setColor(modleColor);
paintE = new Paint(Paint.ANTI_ALIAS_FLAG);
paintE.setStyle(Paint.Style.FILL);
paintE.setStrokeWidth(progressWidth);
paintE.setStrokeCap(Paint.Cap.ROUND);
paintE.setColor(endColor);
ta.recycle();
taPercent.recycle();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width = getWidSize(widthMeasureSpec);
height = getHeiSize(heightMeasureSpec);
setMeasuredDimension(width, height);
}
private int getWidSize(int measureSpace) {
int result;
int mode = MeasureSpec.getMode(measureSpace);
int size = MeasureSpec.getSize(measureSpace);
if (mode == MeasureSpec.EXACTLY) {
result = size;
} else if (mode == MeasureSpec.AT_MOST) {
result = Math.min(defaultWidth + defaultHeight, size);
} else {
result = defaultWidth;
}
return result;
}
private int getHeiSize(int measureSpace) {
int result;
int mode = MeasureSpec.getMode(measureSpace);
int size = MeasureSpec.getSize(measureSpace);
if (mode == MeasureSpec.EXACTLY) {
result = size;
} else if (mode == MeasureSpec.AT_MOST) {
result = Math.min(defaultHeight , size);
} else {
result = defaultHeight;
}
return result;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Log.i("左边的距离", getPaddingLeft() + "");
startPercent = startPercen * ((width-height ) / 100);
modlePercent = modlePercen * ((width-height ) / 100);
Log.i("计算后的百分比", startPercent+"============"+modlePercent);
canvas.drawLine(height/2, 50, startPercent + height/2, 50, paintS);
canvas.drawLine(startPercent + height + modlePercent, 50, width-height, 50, paintE);
canvas.drawLine(startPercent + height/2, 50, startPercent + height + modlePercent, 50, paintM);
}
public void setPerCent(String startPercent, String modlePercent) {
this.startPercen = Float.valueOf(startPercent);
this.modlePercen = Float.valueOf(modlePercent);
}