Compare commits

..

5 Commits

@ -0,0 +1,2 @@
1111
222

@ -1,199 +1,196 @@
package com.sbw.atrue.Order.Activity; package practice.csy.com.customprogress.view;
import android.content.Context; import android.content.Context;
import android.content.res.TypedArray; import android.content.res.TypedArray;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.graphics.Paint; import android.graphics.Paint;
import android.graphics.Rect; import android.graphics.Rect;
import android.graphics.RectF; import android.graphics.RectF;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.TypedValue; import android.util.TypedValue;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import com.sbw.atrue.Order.R; import practice.csy.com.customprogress.R;
/**
* Created by user on 2017-8-8. /**
* * Created by user on 2017-8-8.
* ProgressBar *
*/ * ProgressBar
*/
public class CustomHorizontalProgres extends ProgressBar{
public class CustomHorizontalProgres extends ProgressBar{
//默认值
private static final int DEAFUALT_PROGRESS_UNREACH_HEIGHH = 10;//dp //默认值
private static final int DEAFUALT_PROGRESS_UNREACH_CORLOR = 0xFFD3D6DA; private static final int DEAFUALT_PROGRESS_UNREACH_HEIGHH = 10;//dp
private static final int DEAFUALT_PROGRESS_REACH_HEIGHH = 10;//dp private static final int DEAFUALT_PROGRESS_UNREACH_CORLOR = 0xFFD3D6DA;
private static final int DEAFUALT_PROGRESS_REACH_CORLOR = 0xFFFC00D1; private static final int DEAFUALT_PROGRESS_REACH_HEIGHH = 10;//dp
private static final int DEAFUALT_PROGRESS_TEXT_SIZE = 10;//sp private static final int DEAFUALT_PROGRESS_REACH_CORLOR = 0xFFFC00D1;
private static final int DEAFUALT_PROGRESS_TEXT_CORLOR = 0xFFD3D6DA; private static final int DEAFUALT_PROGRESS_TEXT_SIZE = 10;//sp
private static final int DEAFUALT_PROGRESS_TEXT_OFFSET = 10;//dp private static final int DEAFUALT_PROGRESS_TEXT_CORLOR = 0xFFD3D6DA;
private static final int DEAFUALT_PROGRESS_VIEW_WIDTH = 200;//进度条默认宽度 private static final int DEAFUALT_PROGRESS_TEXT_OFFSET = 10;//dp
private static final int DEAFUALT_PROGRESS_VIEW_WIDTH = 200;//进度条默认宽度
private int HorizontalProgresUnReachColor;//不能用static修饰,不然多个View会共用此属性
private int HorizontalProgresUnReachHeight; private int HorizontalProgresUnReachColor;//不能用static修饰,不然多个View会共用此属性
private int HorizontalProgresReachColor; private int HorizontalProgresUnReachHeight;
private int HorizontalProgresReachHeight; private int HorizontalProgresReachColor;
private int HorizontalProgresTextColor; private int HorizontalProgresReachHeight;
private int HorizontalProgresTextSize; private int HorizontalProgresTextColor;
private int HorizontalProgresTextOffset; private int HorizontalProgresTextSize;
private int HorizontalProgresTextOffset;
private Paint mPaint = new Paint();
public CustomHorizontalProgres(Context context) { private Paint mPaint = new Paint();
this(context,null); public CustomHorizontalProgres(Context context) {
} this(context,null);
}
public CustomHorizontalProgres(Context context, AttributeSet attrs) {
this(context,attrs,0); public CustomHorizontalProgres(Context context, AttributeSet attrs) {
} this(context,attrs,0);
}
public CustomHorizontalProgres(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr); public CustomHorizontalProgres(Context context, AttributeSet attrs, int defStyleAttr) {
geStyleabletAttr(attrs); super(context, attrs, defStyleAttr);
mPaint.setTextSize(HorizontalProgresTextSize);//设置画笔文字大小,便于后面测量文字宽高 geStyleabletAttr(attrs);
mPaint.setColor(HorizontalProgresTextColor); mPaint.setTextSize(HorizontalProgresTextSize);//设置画笔文字大小,便于后面测量文字宽高
} mPaint.setColor(HorizontalProgresTextColor);
}
/**
* /**
*/ *
private void geStyleabletAttr(AttributeSet attrs) { */
TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.CustomHorizontalProgresStyle); private void geStyleabletAttr(AttributeSet attrs) {
HorizontalProgresUnReachColor = typedArray.getColor(R.styleable.CustomHorizontalProgresStyle_HorizontalProgresUnReachColor,DEAFUALT_PROGRESS_UNREACH_CORLOR); TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.CustomHorizontalProgresStyle);
HorizontalProgresReachColor = typedArray.getColor(R.styleable.CustomHorizontalProgresStyle_HorizontalProgresReachColor,DEAFUALT_PROGRESS_REACH_CORLOR); HorizontalProgresUnReachColor = typedArray.getColor(R.styleable.CustomHorizontalProgresStyle_HorizontalProgresUnReachColor,DEAFUALT_PROGRESS_UNREACH_CORLOR);
//将sp、dp统一转换为sp HorizontalProgresReachColor = typedArray.getColor(R.styleable.CustomHorizontalProgresStyle_HorizontalProgresReachColor,DEAFUALT_PROGRESS_REACH_CORLOR);
HorizontalProgresReachHeight = (int) typedArray.getDimension(R.styleable.CustomHorizontalProgresStyle_HorizontalProgresReachHeight,dp2px(getContext(),DEAFUALT_PROGRESS_REACH_HEIGHH)); //将sp、dp统一转换为sp
HorizontalProgresUnReachHeight = (int) typedArray.getDimension(R.styleable.CustomHorizontalProgresStyle_HorizontalProgresUnReachHeight,dp2px(getContext(),DEAFUALT_PROGRESS_UNREACH_HEIGHH)); HorizontalProgresReachHeight = (int) typedArray.getDimension(R.styleable.CustomHorizontalProgresStyle_HorizontalProgresReachHeight,dp2px(getContext(),DEAFUALT_PROGRESS_REACH_HEIGHH));
HorizontalProgresTextColor = typedArray.getColor(R.styleable.CustomHorizontalProgresStyle_HorizontalProgresTextColor,DEAFUALT_PROGRESS_TEXT_CORLOR); HorizontalProgresUnReachHeight = (int) typedArray.getDimension(R.styleable.CustomHorizontalProgresStyle_HorizontalProgresUnReachHeight,dp2px(getContext(),DEAFUALT_PROGRESS_UNREACH_HEIGHH));
HorizontalProgresTextSize = (int) typedArray.getDimension(R.styleable.CustomHorizontalProgresStyle_HorizontalProgresTextSize,sp2px(getContext(),DEAFUALT_PROGRESS_TEXT_SIZE)); HorizontalProgresTextColor = typedArray.getColor(R.styleable.CustomHorizontalProgresStyle_HorizontalProgresTextColor,DEAFUALT_PROGRESS_TEXT_CORLOR);
HorizontalProgresTextOffset = (int) typedArray.getDimension(R.styleable.CustomHorizontalProgresStyle_HorizontalProgresTextOffset,DEAFUALT_PROGRESS_TEXT_OFFSET); HorizontalProgresTextSize = (int) typedArray.getDimension(R.styleable.CustomHorizontalProgresStyle_HorizontalProgresTextSize,sp2px(getContext(),DEAFUALT_PROGRESS_TEXT_SIZE));
typedArray.recycle();//记得加这句 HorizontalProgresTextOffset = (int) typedArray.getDimension(R.styleable.CustomHorizontalProgresStyle_HorizontalProgresTextOffset,DEAFUALT_PROGRESS_TEXT_OFFSET);
/*TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.); typedArray.recycle();//记得加这句
int textColor = a.getColor(R.styleable._, ); }
https://www.jianshu.com/p/8d388894d9e9*/
} @Override
protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
@Override super.onMeasure(widthMeasureSpec, heightMeasureSpec);
protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width = measureWidth(widthMeasureSpec);//计算宽高
super.onMeasure(widthMeasureSpec, heightMeasureSpec); int height = measureHeight(heightMeasureSpec);
int width = measureWidth(widthMeasureSpec);//计算宽高 setMeasuredDimension(width,height);//设置宽高
int height = measureHeight(heightMeasureSpec); }
setMeasuredDimension(width,height);//设置宽高
} @Override
protected synchronized void onDraw(Canvas canvas) {
@Override super.onDraw(canvas);
protected synchronized void onDraw(Canvas canvas) {
super.onDraw(canvas); canvas.save();//save、restore 图层的保存和回滚相关的方法 详见 http://blog.csdn.net/tianjian4592/article/details/45234419
canvas.translate(0,getHeight()/2);//移动图层到垂直居中位置
canvas.save();//save、restore 图层的保存和回滚相关的方法 详见 http://blog.csdn.net/tianjian4592/article/details/45234419 float radio = getProgress()*1.0f/getMax();
canvas.translate(0,getHeight()/2);//移动图层到垂直居中位置 int textWidth = (int) mPaint.measureText(getProgress()+"%");//The width of the text
float radio = getProgress()*1.0f/getMax(); float realWidth = getWidth() - getPaddingLeft() - getPaddingRight() - textWidth - HorizontalProgresTextOffset;//实际宽度减去文字宽度
int textWidth = (int) mPaint.measureText(getProgress()+"%");//The width of the text float progressX = radio * realWidth ;
float realWidth = getWidth() - getPaddingLeft() - getPaddingRight() - textWidth - HorizontalProgresTextOffset;//实际宽度减去文字宽度 //绘制走完的进度线
float progressX = radio * realWidth ; mPaint.setColor(HorizontalProgresReachColor);
//绘制走完的进度线 mPaint.setStrokeWidth(HorizontalProgresReachHeight);
mPaint.setColor(HorizontalProgresReachColor); //canvas.drawLine(getPaddingLeft(),getPaddingTop(),progressX,getPaddingTop(),mPaint);//直角 垂直在同一高度 float startY, float stopY 一样
mPaint.setStrokeWidth(HorizontalProgresReachHeight); RectF mRectF = new RectF(getPaddingLeft(),getPaddingTop()-HorizontalProgresReachHeight/2,(int)progressX,HorizontalProgresReachHeight/2);//圆角 int left, int top, int right, int bottom
//canvas.drawLine(getPaddingLeft(),getPaddingTop(),progressX,getPaddingTop(),mPaint);//直角 垂直在同一高度 float startY, float stopY 一样 canvas.drawRoundRect(mRectF,15,15,mPaint);//圆角矩形
RectF mRectF = new RectF(getPaddingLeft(),getPaddingTop()-HorizontalProgresReachHeight/2,(int)progressX,HorizontalProgresReachHeight/2);//圆角 int left, int top, int right, int bottom //绘制进度
canvas.drawRoundRect(mRectF,15,15,mPaint);//圆角矩形 mPaint.setColor(HorizontalProgresTextColor);
//绘制进度 mPaint.setTextSize(HorizontalProgresTextSize);
mPaint.setColor(HorizontalProgresTextColor); int y = (int) -((mPaint.descent() + mPaint.ascent())/2);//文字居中
mPaint.setTextSize(HorizontalProgresTextSize); canvas.drawText(getProgress()+"%",progressX + HorizontalProgresTextOffset/2,getPaddingTop() + y,mPaint);
int y = (int) -((mPaint.descent() + mPaint.ascent())/2);//文字居中 //绘制未做走完的进度
canvas.drawText(getProgress()+"%",progressX + HorizontalProgresTextOffset/2,getPaddingTop() + y,mPaint); if (progressX + HorizontalProgresTextOffset + textWidth < getWidth() - getPaddingLeft() - getPaddingRight()){//进度走完了,不再画未走完的
//绘制未做走完的进度 mPaint.setColor(HorizontalProgresUnReachColor);
if (progressX + HorizontalProgresTextOffset + textWidth < getWidth() - getPaddingLeft() - getPaddingRight()){//进度走完了,不再画未走完的 mPaint.setStrokeWidth(HorizontalProgresUnReachHeight);
mPaint.setColor(HorizontalProgresUnReachColor); //canvas.drawLine(getPaddingLeft()+progressX + HorizontalProgresTextOffset + textWidth,getPaddingTop(),getWidth() - getPaddingLeft() - getPaddingRight() ,getPaddingTop(),mPaint);//垂直在同一高度 float startY, float stopY 一样
mPaint.setStrokeWidth(HorizontalProgresUnReachHeight); RectF mRectF2 = new RectF(getPaddingLeft()+progressX + HorizontalProgresTextOffset + textWidth,getPaddingTop()-HorizontalProgresUnReachHeight/2,getWidth() - getPaddingLeft() - getPaddingRight(),HorizontalProgresUnReachHeight/2);//圆角 int left, int top, int right, int bottom
//canvas.drawLine(getPaddingLeft()+progressX + HorizontalProgresTextOffset + textWidth,getPaddingTop(),getWidth() - getPaddingLeft() - getPaddingRight() ,getPaddingTop(),mPaint);//垂直在同一高度 float startY, float stopY 一样 canvas.drawRoundRect(mRectF2,15,15,mPaint);//圆角矩形
RectF mRectF2 = new RectF(getPaddingLeft()+progressX + HorizontalProgresTextOffset + textWidth,getPaddingTop()-HorizontalProgresUnReachHeight/2,getWidth() - getPaddingLeft() - getPaddingRight(),HorizontalProgresUnReachHeight/2);//圆角 int left, int top, int right, int bottom }
canvas.drawRoundRect(mRectF2,15,15,mPaint);//圆角矩形 canvas.restore();
} }
canvas.restore();
} /**
* dppx
/** *
* dppx * @param context
* * @param dpVal
* @param context * @return
* @param dpVal */
* @return public static int dp2px(Context context, float dpVal)
*/ {
public static int dp2px(Context context, float dpVal) return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
{ dpVal, context.getResources().getDisplayMetrics());
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, }
dpVal, context.getResources().getDisplayMetrics());
} /**
* sppx
/** *
* sppx * @param context
* * @param spVal
* @param context * @return
* @param spVal */
* @return public static int sp2px(Context context, float spVal)
*/ {
public static int sp2px(Context context, float spVal) return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
{ spVal, context.getResources().getDisplayMetrics());
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, }
spVal, context.getResources().getDisplayMetrics());
}
/**
* Determines the width of this view
/** * @param measureSpec A measureSpec packed into an int
* Determines the width of this view * @return The width of the view, honoring constraints from measureSpec
* @param measureSpec A measureSpec packed into an int */
* @return The width of the view, honoring constraints from measureSpec private int measureWidth(int measureSpec) {
*/ int result = 0;
private int measureWidth(int measureSpec) { int specMode = MeasureSpec.getMode(measureSpec);
int result = 0; int specSize = MeasureSpec.getSize(measureSpec);
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec); if (specMode == MeasureSpec.EXACTLY) {
// We were told how big to be
if (specMode == MeasureSpec.EXACTLY) { result = specSize;
// We were told how big to be } else {
result = specSize; // Measure the text
} else { result = dp2px(getContext(),DEAFUALT_PROGRESS_VIEW_WIDTH);//
// Measure the text if (specMode == MeasureSpec.AT_MOST) {
result = dp2px(getContext(),DEAFUALT_PROGRESS_VIEW_WIDTH);// // Respect AT_MOST value if that was what is called for by measureSpec
if (specMode == MeasureSpec.AT_MOST) { result = Math.min(result, specSize);
// Respect AT_MOST value if that was what is called for by measureSpec }
result = Math.min(result, specSize); }
}
} return result;
}
return result;
} /**
* Determines the height of this view
/** * @param measureSpec A measureSpec packed into an int
* Determines the height of this view * @return The height of the view, honoring constraints from measureSpec
* @param measureSpec A measureSpec packed into an int */
* @return The height of the view, honoring constraints from measureSpec private int measureHeight(int measureSpec) {
*/ int result = 0;
private int measureHeight(int measureSpec) { int specMode = MeasureSpec.getMode(measureSpec);
int result = 0; int specSize = MeasureSpec.getSize(measureSpec);
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec); if (specMode == MeasureSpec.EXACTLY) {
// We were told how big to be
if (specMode == MeasureSpec.EXACTLY) { result = specSize;
// We were told how big to be } else {
result = specSize; // Measure the text (beware: ascent is a negative number)
} else { //此处高度为走完的进度高度和未走完的机大以及文字的高度的最大值
// Measure the text (beware: ascent is a negative number) int textHeight = (int) (mPaint.descent() - mPaint.ascent());//得到字的高度有二种方式,第一种是React,第二种这个
//此处高度为走完的进度高度和未走完的机大以及文字的高度的最大值 result = Math.max(textHeight,Math.max(HorizontalProgresReachHeight,HorizontalProgresUnReachHeight)) + getPaddingTop()
int textHeight = (int) (mPaint.descent() - mPaint.ascent());//得到字的高度有二种方式,第一种是React,第二种这个 + getPaddingBottom();
result = Math.max(textHeight,Math.max(HorizontalProgresReachHeight,HorizontalProgresUnReachHeight)) + getPaddingTop() if (specMode == MeasureSpec.AT_MOST) {
+ getPaddingBottom(); // Respect AT_MOST value if that was what is called for by measureSpec
if (specMode == MeasureSpec.AT_MOST) { result = Math.min(result, specSize);
// Respect AT_MOST value if that was what is called for by measureSpec }
result = Math.min(result, specSize); }
} return result;
} }
return result;
}
}
}

@ -1,209 +1,206 @@
package com.sbw.atrue.Order.Activity; package practice.csy.com.customprogress.view;
import android.content.Context; import android.content.Context;
import android.content.res.TypedArray; import android.content.res.TypedArray;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.graphics.Paint; import android.graphics.Paint;
import android.graphics.Path; import android.graphics.Path;
import android.graphics.RectF; import android.graphics.RectF;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.TypedValue; import android.util.TypedValue;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import com.sbw.atrue.Order.R; import practice.csy.com.customprogress.R;
//import practice.csy.com.customprogress.R; /**
* Created by user on 2017-8-8.
/** * <p>
* Created by user on 2017-8-8. * ProgressBar
* <p> * <p>
* ProgressBar *
* <p> */
*
*/ public class CustomHorizontalProgresNoNum extends ProgressBar {
public class CustomHorizontalProgresNoNum extends ProgressBar { //默认值
private static final int DEAFUALT_PROGRESS_UNREACH_HEIGHH = 10;//dp
//默认值 protected static final int DEAFUALT_PROGRESS_UNREACH_CORLOR = 0xFFD3D6DA;
private static final int DEAFUALT_PROGRESS_UNREACH_HEIGHH = 10;//dp private static final int DEAFUALT_PROGRESS_REACH_HEIGHH = 10;//dp
protected static final int DEAFUALT_PROGRESS_UNREACH_CORLOR = 0xFFD3D6DA; protected static final int DEAFUALT_PROGRESS_REACH_CORLOR = 0xFFFC00D1;
private static final int DEAFUALT_PROGRESS_REACH_HEIGHH = 10;//dp protected static final int DEAFUALT_PROGRESS_TEXT_SIZE = 10;//sp
protected static final int DEAFUALT_PROGRESS_REACH_CORLOR = 0xFFFC00D1; protected static final int DEAFUALT_PROGRESS_TEXT_CORLOR = 0xFFD3D6DA;
protected static final int DEAFUALT_PROGRESS_TEXT_SIZE = 10;//sp protected static final int DEAFUALT_PROGRESS_TEXT_OFFSET = 10;//dp
protected static final int DEAFUALT_PROGRESS_TEXT_CORLOR = 0xFFD3D6DA; private static final int DEAFUALT_PROGRESS_VIEW_WIDTH = 200;//进度条默认宽度
protected static final int DEAFUALT_PROGRESS_TEXT_OFFSET = 10;//dp
private static final int DEAFUALT_PROGRESS_VIEW_WIDTH = 200;//进度条默认宽度 protected int HorizontalProgresUnReachColor;//不能用static修饰,不然多个View会共用此属性
private int HorizontalProgresUnReachHeight;
protected int HorizontalProgresUnReachColor;//不能用static修饰,不然多个View会共用此属性 protected int HorizontalProgresReachColor;
private int HorizontalProgresUnReachHeight; private int HorizontalProgresReachHeight;
protected int HorizontalProgresReachColor; protected int HorizontalProgresTextColor;
private int HorizontalProgresReachHeight; protected int HorizontalProgresTextSize;
protected int HorizontalProgresTextColor; protected int HorizontalProgresTextOffset;
protected int HorizontalProgresTextSize;
protected int HorizontalProgresTextOffset; protected Paint mPaint = new Paint();
//protected float[] rids = {15.0f, 15.0f, 15.0f, 15.0f, 0.0f, 0.0f, 0.0f, 0.0f,}; 上面圆角
protected Paint mPaint = new Paint();
//protected float[] rids = {15.0f, 15.0f, 15.0f, 15.0f, 0.0f, 0.0f, 0.0f, 0.0f,}; 上面圆角 protected Path path;
protected float[] rids = {15.0f, 15.0f, 15.0f, 15.0f, 15.0f, 15.0f, 15.0f, 15.0f,};//四个角都圆角
protected Path path; public CustomHorizontalProgresNoNum(Context context) {
protected float[] rids = {15.0f, 15.0f, 15.0f, 15.0f, 15.0f, 15.0f, 15.0f, 15.0f,};//四个角都圆角 this(context, null);
public CustomHorizontalProgresNoNum(Context context) { }
this(context, null);
} public CustomHorizontalProgresNoNum(Context context, AttributeSet attrs) {
this(context, attrs, 0);
public CustomHorizontalProgresNoNum(Context context, AttributeSet attrs) { }
this(context, attrs, 0);
} public CustomHorizontalProgresNoNum(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
public CustomHorizontalProgresNoNum(Context context, AttributeSet attrs, int defStyleAttr) { getStyleabletAttr(attrs);
super(context, attrs, defStyleAttr); mPaint.setTextSize(HorizontalProgresTextSize);//设置画笔文字大小,便于后面测量文字宽高
getStyleabletAttr(attrs); mPaint.setColor(HorizontalProgresTextColor);
mPaint.setTextSize(HorizontalProgresTextSize);//设置画笔文字大小,便于后面测量文字宽高 path = new Path();
mPaint.setColor(HorizontalProgresTextColor);
path = new Path(); }
}
/**
*
/** */
* protected void getStyleabletAttr(AttributeSet attrs) {
*/ TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.CustomHorizontalProgresStyle);
protected void getStyleabletAttr(AttributeSet attrs) { HorizontalProgresUnReachColor = typedArray.getColor(R.styleable.CustomHorizontalProgresStyle_HorizontalProgresUnReachColor, DEAFUALT_PROGRESS_UNREACH_CORLOR);
TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.CustomHorizontalProgresStyle); HorizontalProgresReachColor = typedArray.getColor(R.styleable.CustomHorizontalProgresStyle_HorizontalProgresReachColor, DEAFUALT_PROGRESS_REACH_CORLOR);
HorizontalProgresUnReachColor = typedArray.getColor(R.styleable.CustomHorizontalProgresStyle_HorizontalProgresUnReachColor, DEAFUALT_PROGRESS_UNREACH_CORLOR); //将sp、dp统一转换为sp
HorizontalProgresReachColor = typedArray.getColor(R.styleable.CustomHorizontalProgresStyle_HorizontalProgresReachColor, DEAFUALT_PROGRESS_REACH_CORLOR); HorizontalProgresReachHeight = (int) typedArray.getDimension(R.styleable.CustomHorizontalProgresStyle_HorizontalProgresReachHeight, dp2px(getContext(), DEAFUALT_PROGRESS_REACH_HEIGHH));
//将sp、dp统一转换为sp HorizontalProgresUnReachHeight = (int) typedArray.getDimension(R.styleable.CustomHorizontalProgresStyle_HorizontalProgresUnReachHeight, dp2px(getContext(), DEAFUALT_PROGRESS_UNREACH_HEIGHH));
HorizontalProgresReachHeight = (int) typedArray.getDimension(R.styleable.CustomHorizontalProgresStyle_HorizontalProgresReachHeight, dp2px(getContext(), DEAFUALT_PROGRESS_REACH_HEIGHH)); HorizontalProgresTextColor = typedArray.getColor(R.styleable.CustomHorizontalProgresStyle_HorizontalProgresTextColor, DEAFUALT_PROGRESS_TEXT_CORLOR);
HorizontalProgresUnReachHeight = (int) typedArray.getDimension(R.styleable.CustomHorizontalProgresStyle_HorizontalProgresUnReachHeight, dp2px(getContext(), DEAFUALT_PROGRESS_UNREACH_HEIGHH)); HorizontalProgresTextSize = (int) typedArray.getDimension(R.styleable.CustomHorizontalProgresStyle_HorizontalProgresTextSize, sp2px(getContext(), DEAFUALT_PROGRESS_TEXT_SIZE));
HorizontalProgresTextColor = typedArray.getColor(R.styleable.CustomHorizontalProgresStyle_HorizontalProgresTextColor, DEAFUALT_PROGRESS_TEXT_CORLOR); HorizontalProgresTextOffset = (int) typedArray.getDimension(R.styleable.CustomHorizontalProgresStyle_HorizontalProgresTextOffset, DEAFUALT_PROGRESS_TEXT_OFFSET);
HorizontalProgresTextSize = (int) typedArray.getDimension(R.styleable.CustomHorizontalProgresStyle_HorizontalProgresTextSize, sp2px(getContext(), DEAFUALT_PROGRESS_TEXT_SIZE)); typedArray.recycle();//记得加这句
HorizontalProgresTextOffset = (int) typedArray.getDimension(R.styleable.CustomHorizontalProgresStyle_HorizontalProgresTextOffset, DEAFUALT_PROGRESS_TEXT_OFFSET); }
typedArray.recycle();//记得加这句
} @Override
protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
@Override super.onMeasure(widthMeasureSpec, heightMeasureSpec);
protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width = measureWidth(widthMeasureSpec);//计算宽高
super.onMeasure(widthMeasureSpec, heightMeasureSpec); int height = measureHeight(heightMeasureSpec);
int width = measureWidth(widthMeasureSpec);//计算宽高 setMeasuredDimension(width, height);//设置宽高
int height = measureHeight(heightMeasureSpec); }
setMeasuredDimension(width, height);//设置宽高
} @Override
protected synchronized void onDraw(Canvas canvas) {
@Override super.onDraw(canvas);
protected synchronized void onDraw(Canvas canvas) {
super.onDraw(canvas); canvas.save();//save、restore 图层的保存和回滚相关的方法 详见 http://blog.csdn.net/tianjian4592/article/details/45234419
canvas.translate(0, getHeight() / 2);//移动图层到垂直居中位置
canvas.save();//save、restore 图层的保存和回滚相关的方法 详见 http://blog.csdn.net/tianjian4592/article/details/45234419
canvas.translate(0, getHeight() / 2);//移动图层到垂直居中位置
float radio = getProgress() * 1.0f / getMax();
float realWidth = getWidth() - getPaddingLeft() - getPaddingRight();//实际宽度减去文字宽度
float radio = getProgress() * 1.0f / getMax(); float progressX = radio * realWidth;
float realWidth = getWidth() - getPaddingLeft() - getPaddingRight();//实际宽度减去文字宽度
float progressX = radio * realWidth; /*向路径中添加圆角矩形。radii数组定义圆角矩形的四个圆角的x,y半径。radii长度必须为8*/
path.addRoundRect(new RectF(getPaddingLeft(),getPaddingTop()-HorizontalProgresReachHeight/2,realWidth,HorizontalProgresReachHeight/2), rids, Path.Direction.CW);
/*向路径中添加圆角矩形。radii数组定义圆角矩形的四个圆角的x,y半径。radii长度必须为8*/ canvas.clipPath(path);
path.addRoundRect(new RectF(getPaddingLeft(),getPaddingTop()-HorizontalProgresReachHeight/2,realWidth,HorizontalProgresReachHeight/2), rids, Path.Direction.CW);
canvas.clipPath(path); //绘制走完的进度线
mPaint.setColor(HorizontalProgresReachColor);
//绘制走完的进度线 mPaint.setStrokeWidth(HorizontalProgresReachHeight);
mPaint.setColor(HorizontalProgresReachColor); //canvas.drawLine(getPaddingLeft(), getPaddingTop(), progressX, getPaddingTop(), mPaint);//直角 垂直在同一高度 float startY, float stopY 一样
mPaint.setStrokeWidth(HorizontalProgresReachHeight); RectF mRectF = new RectF(getPaddingLeft(),getPaddingTop()-HorizontalProgresReachHeight/2,(int)progressX,HorizontalProgresReachHeight/2);//圆角 int left, int top, int right, int bottom
//canvas.drawLine(getPaddingLeft(), getPaddingTop(), progressX, getPaddingTop(), mPaint);//直角 垂直在同一高度 float startY, float stopY 一样 canvas.drawRoundRect(mRectF,0,0,mPaint);//圆角矩形
RectF mRectF = new RectF(getPaddingLeft(),getPaddingTop()-HorizontalProgresReachHeight/2,(int)progressX,HorizontalProgresReachHeight/2);//圆角 int left, int top, int right, int bottom
canvas.drawRoundRect(mRectF,0,0,mPaint);//圆角矩形 //绘制未做走完的进度
if (progressX < getWidth() - getPaddingLeft() - getPaddingRight()) {//进度走完了,不再画未走完的
//绘制未做走完的进度 mPaint.setColor(HorizontalProgresUnReachColor);
if (progressX < getWidth() - getPaddingLeft() - getPaddingRight()) {//进度走完了,不再画未走完的 mPaint.setStrokeWidth(HorizontalProgresUnReachHeight);
mPaint.setColor(HorizontalProgresUnReachColor); //canvas.drawLine(progressX, getPaddingTop(), getWidth() - getPaddingLeft() - getPaddingRight(), getPaddingTop(), mPaint);//垂直在同一高度 float startY, float stopY 一样
mPaint.setStrokeWidth(HorizontalProgresUnReachHeight); RectF mRectF2 = new RectF(progressX - 15 ,getPaddingTop()-HorizontalProgresUnReachHeight/2,realWidth,HorizontalProgresUnReachHeight/2);//圆角 int left, int top, int right, int bottom
//canvas.drawLine(progressX, getPaddingTop(), getWidth() - getPaddingLeft() - getPaddingRight(), getPaddingTop(), mPaint);//垂直在同一高度 float startY, float stopY 一样 canvas.drawRoundRect(mRectF2,0,0,mPaint);//圆角矩形
RectF mRectF2 = new RectF(progressX - 15 ,getPaddingTop()-HorizontalProgresUnReachHeight/2,realWidth,HorizontalProgresUnReachHeight/2);//圆角 int left, int top, int right, int bottom }
canvas.drawRoundRect(mRectF2,0,0,mPaint);//圆角矩形 canvas.restore();
}
canvas.restore();
}
} /**
* dppx
/** *
* dppx * @param context
* * @param dpVal
* @param context * @return
* @param dpVal */
* @return public static int dp2px(Context context, float dpVal) {
*/ return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
public static int dp2px(Context context, float dpVal) { dpVal, context.getResources().getDisplayMetrics());
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, }
dpVal, context.getResources().getDisplayMetrics());
} /**
* sppx
/** *
* sppx * @param context
* * @param spVal
* @param context * @return
* @param spVal */
* @return public static int sp2px(Context context, float spVal) {
*/ return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
public static int sp2px(Context context, float spVal) { spVal, context.getResources().getDisplayMetrics());
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, }
spVal, context.getResources().getDisplayMetrics());
}
/**
* Determines the width of this view
/** *
* Determines the width of this view * @param measureSpec A measureSpec packed into an int
* * @return The width of the view, honoring constraints from measureSpec
* @param measureSpec A measureSpec packed into an int */
* @return The width of the view, honoring constraints from measureSpec protected int measureWidth(int measureSpec) {
*/ int result = 0;
protected int measureWidth(int measureSpec) { int specMode = MeasureSpec.getMode(measureSpec);
int result = 0; int specSize = MeasureSpec.getSize(measureSpec);
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec); if (specMode == MeasureSpec.EXACTLY) {
// We were told how big to be
if (specMode == MeasureSpec.EXACTLY) { result = specSize;
// We were told how big to be } else {
result = specSize; // Measure the text
} else { result = dp2px(getContext(), DEAFUALT_PROGRESS_VIEW_WIDTH);//
// Measure the text if (specMode == MeasureSpec.AT_MOST) {
result = dp2px(getContext(), DEAFUALT_PROGRESS_VIEW_WIDTH);// // Respect AT_MOST value if that was what is called for by measureSpec
if (specMode == MeasureSpec.AT_MOST) { result = Math.min(result, specSize);
// Respect AT_MOST value if that was what is called for by measureSpec }
result = Math.min(result, specSize); }
}
} return result;
}
return result;
} /**
* Determines the height of this view
/** *
* Determines the height of this view * @param measureSpec A measureSpec packed into an int
* * @return The height of the view, honoring constraints from measureSpec
* @param measureSpec A measureSpec packed into an int */
* @return The height of the view, honoring constraints from measureSpec protected int measureHeight(int measureSpec) {
*/ int result = 0;
protected int measureHeight(int measureSpec) { int specMode = MeasureSpec.getMode(measureSpec);
int result = 0; int specSize = MeasureSpec.getSize(measureSpec);
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec); if (specMode == MeasureSpec.EXACTLY) {
// We were told how big to be
if (specMode == MeasureSpec.EXACTLY) { result = specSize;
// We were told how big to be } else {
result = specSize; // Measure the text (beware: ascent is a negative number)
} else { //此处高度为走完的进度高度和未走完的机大以及文字的高度的最大值
// Measure the text (beware: ascent is a negative number) int textHeight = (int) (mPaint.descent() - mPaint.ascent());//得到字的高度有二种方式,第一种是React,第二种这个
//此处高度为走完的进度高度和未走完的机大以及文字的高度的最大值 result = Math.max(textHeight, Math.max(HorizontalProgresReachHeight, HorizontalProgresUnReachHeight)) + getPaddingTop()
int textHeight = (int) (mPaint.descent() - mPaint.ascent());//得到字的高度有二种方式,第一种是React,第二种这个 + getPaddingBottom();
result = Math.max(textHeight, Math.max(HorizontalProgresReachHeight, HorizontalProgresUnReachHeight)) + getPaddingTop() if (specMode == MeasureSpec.AT_MOST) {
+ getPaddingBottom(); // Respect AT_MOST value if that was what is called for by measureSpec
if (specMode == MeasureSpec.AT_MOST) { result = Math.min(result, specSize);
// Respect AT_MOST value if that was what is called for by measureSpec }
result = Math.min(result, specSize); }
} return result;
} }
return result;
}
}
}

@ -1,200 +1,197 @@
package com.sbw.atrue.Order.Activity; package practice.csy.com.customprogress.view;
import android.content.Context; import android.content.Context;
import android.content.res.TypedArray; import android.content.res.TypedArray;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.graphics.Paint; import android.graphics.Paint;
import android.graphics.RectF; import android.graphics.RectF;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.TypedValue; import android.util.TypedValue;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import com.sbw.atrue.Order.R; import practice.csy.com.customprogress.R;
//import practice.csy.com.customprogress.R; /**
* Created by user on 2017-8-8.
/** *
* Created by user on 2017-8-8. * ProgressBar
* *
* ProgressBar *
* */
*
*/ public class CustomHorizontalProgresWithNum extends ProgressBar{
public class CustomHorizontalProgresWithNum extends ProgressBar{ //默认值
private static final int DEAFUALT_PROGRESS_UNREACH_HEIGHH = 10;//dp
//默认值 protected static final int DEAFUALT_PROGRESS_UNREACH_CORLOR = 0xFFD3D6DA;
private static final int DEAFUALT_PROGRESS_UNREACH_HEIGHH = 10;//dp protected static final int DEAFUALT_PROGRESS_REACH_HEIGHH = 10;//dp
protected static final int DEAFUALT_PROGRESS_UNREACH_CORLOR = 0xFFD3D6DA; protected static final int DEAFUALT_PROGRESS_REACH_CORLOR = 0xFFFC00D1;
protected static final int DEAFUALT_PROGRESS_REACH_HEIGHH = 10;//dp protected static final int DEAFUALT_PROGRESS_TEXT_SIZE = 10;//sp
protected static final int DEAFUALT_PROGRESS_REACH_CORLOR = 0xFFFC00D1; protected static final int DEAFUALT_PROGRESS_TEXT_CORLOR = 0xFFD3D6DA;
protected static final int DEAFUALT_PROGRESS_TEXT_SIZE = 10;//sp protected static final int DEAFUALT_PROGRESS_TEXT_OFFSET = 10;//dp
protected static final int DEAFUALT_PROGRESS_TEXT_CORLOR = 0xFFD3D6DA; protected static final int DEAFUALT_PROGRESS_VIEW_WIDTH = 200;//进度条默认宽度
protected static final int DEAFUALT_PROGRESS_TEXT_OFFSET = 10;//dp
protected static final int DEAFUALT_PROGRESS_VIEW_WIDTH = 200;//进度条默认宽度 protected int HorizontalProgresUnReachColor;//不能用static修饰,不然多个View会共用此属性
private int HorizontalProgresUnReachHeight;
protected int HorizontalProgresUnReachColor;//不能用static修饰,不然多个View会共用此属性 protected int HorizontalProgresReachColor;
private int HorizontalProgresUnReachHeight; private int HorizontalProgresReachHeight;
protected int HorizontalProgresReachColor; protected int HorizontalProgresTextColor;
private int HorizontalProgresReachHeight; protected int HorizontalProgresTextSize;
protected int HorizontalProgresTextColor; protected int HorizontalProgresTextOffset;
protected int HorizontalProgresTextSize;
protected int HorizontalProgresTextOffset; protected Paint mPaint = new Paint();
public CustomHorizontalProgresWithNum(Context context) {
protected Paint mPaint = new Paint(); this(context,null);
public CustomHorizontalProgresWithNum(Context context) { }
this(context,null);
} public CustomHorizontalProgresWithNum(Context context, AttributeSet attrs) {
this(context,attrs,0);
public CustomHorizontalProgresWithNum(Context context, AttributeSet attrs) { }
this(context,attrs,0);
} public CustomHorizontalProgresWithNum(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
public CustomHorizontalProgresWithNum(Context context, AttributeSet attrs, int defStyleAttr) { getStyleabletAttr(attrs);
super(context, attrs, defStyleAttr); mPaint.setTextSize(HorizontalProgresTextSize);//设置画笔文字大小,便于后面测量文字宽高
getStyleabletAttr(attrs); mPaint.setColor(HorizontalProgresTextColor);
mPaint.setTextSize(HorizontalProgresTextSize);//设置画笔文字大小,便于后面测量文字宽高 }
mPaint.setColor(HorizontalProgresTextColor);
}
/**
*
/** */
* protected void getStyleabletAttr(AttributeSet attrs) {
*/ TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.CustomHorizontalProgresStyle);
protected void getStyleabletAttr(AttributeSet attrs) { HorizontalProgresUnReachColor = typedArray.getColor(R.styleable.CustomHorizontalProgresStyle_HorizontalProgresUnReachColor,DEAFUALT_PROGRESS_UNREACH_CORLOR);
TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.CustomHorizontalProgresStyle); HorizontalProgresReachColor = typedArray.getColor(R.styleable.CustomHorizontalProgresStyle_HorizontalProgresReachColor,DEAFUALT_PROGRESS_REACH_CORLOR);
HorizontalProgresUnReachColor = typedArray.getColor(R.styleable.CustomHorizontalProgresStyle_HorizontalProgresUnReachColor,DEAFUALT_PROGRESS_UNREACH_CORLOR); //将sp、dp统一转换为sp
HorizontalProgresReachColor = typedArray.getColor(R.styleable.CustomHorizontalProgresStyle_HorizontalProgresReachColor,DEAFUALT_PROGRESS_REACH_CORLOR); HorizontalProgresReachHeight = (int) typedArray.getDimension(R.styleable.CustomHorizontalProgresStyle_HorizontalProgresReachHeight,dp2px(getContext(),DEAFUALT_PROGRESS_REACH_HEIGHH));
//将sp、dp统一转换为sp HorizontalProgresUnReachHeight = (int) typedArray.getDimension(R.styleable.CustomHorizontalProgresStyle_HorizontalProgresUnReachHeight,dp2px(getContext(),DEAFUALT_PROGRESS_UNREACH_HEIGHH));
HorizontalProgresReachHeight = (int) typedArray.getDimension(R.styleable.CustomHorizontalProgresStyle_HorizontalProgresReachHeight,dp2px(getContext(),DEAFUALT_PROGRESS_REACH_HEIGHH)); HorizontalProgresTextColor = typedArray.getColor(R.styleable.CustomHorizontalProgresStyle_HorizontalProgresTextColor,DEAFUALT_PROGRESS_TEXT_CORLOR);
HorizontalProgresUnReachHeight = (int) typedArray.getDimension(R.styleable.CustomHorizontalProgresStyle_HorizontalProgresUnReachHeight,dp2px(getContext(),DEAFUALT_PROGRESS_UNREACH_HEIGHH)); HorizontalProgresTextSize = (int) typedArray.getDimension(R.styleable.CustomHorizontalProgresStyle_HorizontalProgresTextSize,sp2px(getContext(),DEAFUALT_PROGRESS_TEXT_SIZE));
HorizontalProgresTextColor = typedArray.getColor(R.styleable.CustomHorizontalProgresStyle_HorizontalProgresTextColor,DEAFUALT_PROGRESS_TEXT_CORLOR); HorizontalProgresTextOffset = (int) typedArray.getDimension(R.styleable.CustomHorizontalProgresStyle_HorizontalProgresTextOffset,DEAFUALT_PROGRESS_TEXT_OFFSET);
HorizontalProgresTextSize = (int) typedArray.getDimension(R.styleable.CustomHorizontalProgresStyle_HorizontalProgresTextSize,sp2px(getContext(),DEAFUALT_PROGRESS_TEXT_SIZE)); typedArray.recycle();//记得加这句
HorizontalProgresTextOffset = (int) typedArray.getDimension(R.styleable.CustomHorizontalProgresStyle_HorizontalProgresTextOffset,DEAFUALT_PROGRESS_TEXT_OFFSET); }
typedArray.recycle();//记得加这句
} @Override
protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
@Override super.onMeasure(widthMeasureSpec, heightMeasureSpec);
protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width = measureWidth(widthMeasureSpec);//计算宽高
super.onMeasure(widthMeasureSpec, heightMeasureSpec); int height = measureHeight(heightMeasureSpec);
int width = measureWidth(widthMeasureSpec);//计算宽高 setMeasuredDimension(width,height);//设置宽高
int height = measureHeight(heightMeasureSpec); }
setMeasuredDimension(width,height);//设置宽高
} @Override
protected synchronized void onDraw(Canvas canvas) {
@Override super.onDraw(canvas);
protected synchronized void onDraw(Canvas canvas) {
super.onDraw(canvas); canvas.save();//save、restore 图层的保存和回滚相关的方法 详见 http://blog.csdn.net/tianjian4592/article/details/45234419
canvas.translate(0,getHeight()/2);//移动图层到垂直居中位置
canvas.save();//save、restore 图层的保存和回滚相关的方法 详见 http://blog.csdn.net/tianjian4592/article/details/45234419 float radio = getProgress()*1.0f/getMax();
canvas.translate(0,getHeight()/2);//移动图层到垂直居中位置 int textWidth = (int) mPaint.measureText(getProgress()+"%");//The width of the text
float radio = getProgress()*1.0f/getMax(); float realWidth = getWidth() - getPaddingLeft() - getPaddingRight() - textWidth - HorizontalProgresTextOffset;//实际宽度减去文字宽度
int textWidth = (int) mPaint.measureText(getProgress()+"%");//The width of the text float progressX = radio * realWidth ;
float realWidth = getWidth() - getPaddingLeft() - getPaddingRight() - textWidth - HorizontalProgresTextOffset;//实际宽度减去文字宽度 //绘制走完的进度线
float progressX = radio * realWidth ; mPaint.setColor(HorizontalProgresReachColor);
//绘制走完的进度线 mPaint.setStrokeWidth(HorizontalProgresReachHeight);
mPaint.setColor(HorizontalProgresReachColor); //canvas.drawLine(getPaddingLeft(),getPaddingTop(),progressX,getPaddingTop(),mPaint);//直角 垂直在同一高度 float startY, float stopY 一样
mPaint.setStrokeWidth(HorizontalProgresReachHeight); RectF mRectF = new RectF(getPaddingLeft(),getPaddingTop()-HorizontalProgresReachHeight/2,(int)progressX,HorizontalProgresReachHeight/2);//圆角 int left, int top, int right, int bottom
//canvas.drawLine(getPaddingLeft(),getPaddingTop(),progressX,getPaddingTop(),mPaint);//直角 垂直在同一高度 float startY, float stopY 一样 canvas.drawRoundRect(mRectF,15,15,mPaint);//圆角矩形
RectF mRectF = new RectF(getPaddingLeft(),getPaddingTop()-HorizontalProgresReachHeight/2,(int)progressX,HorizontalProgresReachHeight/2);//圆角 int left, int top, int right, int bottom //绘制进度
canvas.drawRoundRect(mRectF,15,15,mPaint);//圆角矩形 mPaint.setColor(HorizontalProgresTextColor);
//绘制进度 mPaint.setTextSize(HorizontalProgresTextSize);
mPaint.setColor(HorizontalProgresTextColor); int y = (int) -((mPaint.descent() + mPaint.ascent())/2);//文字居中
mPaint.setTextSize(HorizontalProgresTextSize); canvas.drawText(getProgress()+"%",progressX + HorizontalProgresTextOffset/2,getPaddingTop() + y,mPaint);
int y = (int) -((mPaint.descent() + mPaint.ascent())/2);//文字居中 //绘制未做走完的进度
canvas.drawText(getProgress()+"%",progressX + HorizontalProgresTextOffset/2,getPaddingTop() + y,mPaint); if (progressX + HorizontalProgresTextOffset + textWidth < getWidth() - getPaddingLeft() - getPaddingRight()){//进度走完了,不再画未走完的
//绘制未做走完的进度 mPaint.setColor(HorizontalProgresUnReachColor);
if (progressX + HorizontalProgresTextOffset + textWidth < getWidth() - getPaddingLeft() - getPaddingRight()){//进度走完了,不再画未走完的 mPaint.setStrokeWidth(HorizontalProgresUnReachHeight);
mPaint.setColor(HorizontalProgresUnReachColor); //canvas.drawLine(getPaddingLeft()+progressX + HorizontalProgresTextOffset + textWidth,getPaddingTop(),getWidth() - getPaddingLeft() - getPaddingRight() ,getPaddingTop(),mPaint);//垂直在同一高度 float startY, float stopY 一样
mPaint.setStrokeWidth(HorizontalProgresUnReachHeight); RectF mRectF2 = new RectF(progressX + HorizontalProgresTextOffset + textWidth,getPaddingTop()-HorizontalProgresUnReachHeight/2,realWidth,HorizontalProgresUnReachHeight/2);//圆角 int left, int top, int right, int bottom
//canvas.drawLine(getPaddingLeft()+progressX + HorizontalProgresTextOffset + textWidth,getPaddingTop(),getWidth() - getPaddingLeft() - getPaddingRight() ,getPaddingTop(),mPaint);//垂直在同一高度 float startY, float stopY 一样 canvas.drawRoundRect(mRectF2,15,15,mPaint);//圆角矩形
RectF mRectF2 = new RectF(progressX + HorizontalProgresTextOffset + textWidth,getPaddingTop()-HorizontalProgresUnReachHeight/2,realWidth,HorizontalProgresUnReachHeight/2);//圆角 int left, int top, int right, int bottom }
canvas.drawRoundRect(mRectF2,15,15,mPaint);//圆角矩形 canvas.restore();
} }
canvas.restore();
} /**
* dppx
/** *
* dppx * @param context
* * @param dpVal
* @param context * @return
* @param dpVal */
* @return public static int dp2px(Context context, float dpVal)
*/ {
public static int dp2px(Context context, float dpVal) return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
{ dpVal, context.getResources().getDisplayMetrics());
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, }
dpVal, context.getResources().getDisplayMetrics());
} /**
* sppx
/** *
* sppx * @param context
* * @param spVal
* @param context * @return
* @param spVal */
* @return public static int sp2px(Context context, float spVal)
*/ {
public static int sp2px(Context context, float spVal) return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
{ spVal, context.getResources().getDisplayMetrics());
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, }
spVal, context.getResources().getDisplayMetrics());
}
/**
* Determines the width of this view
/** * @param measureSpec A measureSpec packed into an int
* Determines the width of this view * @return The width of the view, honoring constraints from measureSpec
* @param measureSpec A measureSpec packed into an int */
* @return The width of the view, honoring constraints from measureSpec protected int measureWidth(int measureSpec) {
*/ int result = 0;
protected int measureWidth(int measureSpec) { int specMode = MeasureSpec.getMode(measureSpec);
int result = 0; int specSize = MeasureSpec.getSize(measureSpec);
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec); if (specMode == MeasureSpec.EXACTLY) {
// We were told how big to be
if (specMode == MeasureSpec.EXACTLY) { result = specSize;
// We were told how big to be } else {
result = specSize; // Measure the text
} else { result = dp2px(getContext(),DEAFUALT_PROGRESS_VIEW_WIDTH);//
// Measure the text if (specMode == MeasureSpec.AT_MOST) {
result = dp2px(getContext(),DEAFUALT_PROGRESS_VIEW_WIDTH);// // Respect AT_MOST value if that was what is called for by measureSpec
if (specMode == MeasureSpec.AT_MOST) { result = Math.min(result, specSize);
// Respect AT_MOST value if that was what is called for by measureSpec }
result = Math.min(result, specSize); }
}
} return result;
}
return result;
} /**
* Determines the height of this view
/** * @param measureSpec A measureSpec packed into an int
* Determines the height of this view * @return The height of the view, honoring constraints from measureSpec
* @param measureSpec A measureSpec packed into an int */
* @return The height of the view, honoring constraints from measureSpec protected int measureHeight(int measureSpec) {
*/ int result = 0;
protected int measureHeight(int measureSpec) { int specMode = MeasureSpec.getMode(measureSpec);
int result = 0; int specSize = MeasureSpec.getSize(measureSpec);
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec); if (specMode == MeasureSpec.EXACTLY) {
// We were told how big to be
if (specMode == MeasureSpec.EXACTLY) { result = specSize;
// We were told how big to be } else {
result = specSize; // Measure the text (beware: ascent is a negative number)
} else { //此处高度为走完的进度高度和未走完的机大以及文字的高度的最大值
// Measure the text (beware: ascent is a negative number) int textHeight = (int) (mPaint.descent() - mPaint.ascent());//得到字的高度有二种方式,第一种是React,第二种这个
//此处高度为走完的进度高度和未走完的机大以及文字的高度的最大值 result = Math.max(textHeight,Math.max(HorizontalProgresReachHeight,HorizontalProgresUnReachHeight)) + getPaddingTop()
int textHeight = (int) (mPaint.descent() - mPaint.ascent());//得到字的高度有二种方式,第一种是React,第二种这个 + getPaddingBottom();
result = Math.max(textHeight,Math.max(HorizontalProgresReachHeight,HorizontalProgresUnReachHeight)) + getPaddingTop() if (specMode == MeasureSpec.AT_MOST) {
+ getPaddingBottom(); // Respect AT_MOST value if that was what is called for by measureSpec
if (specMode == MeasureSpec.AT_MOST) { result = Math.min(result, specSize);
// Respect AT_MOST value if that was what is called for by measureSpec }
result = Math.min(result, specSize); }
} return result;
} }
return result;
}
}
}

@ -0,0 +1,2 @@
# liyuxuan_gitProject

@ -1,98 +1,98 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:background="#ffffff" android:background="#ffffff"
tools:context="com.sbw.atrue.Order.Activity.horizonta_main"> tools:context="practice.csy.com.customprogress.MainActivity">
<LinearLayout <LinearLayout
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical" > android:orientation="vertical" >
<TextView <TextView
android:layout_width="100dp" android:layout_width="100dp"
android:layout_height="20dp" android:layout_height="20dp"
android:text="蛋白质" android:text="蛋白质"
android:textColor="#36513e" android:textColor="#36513e"
android:layout_marginTop="20dp" android:layout_marginTop="20dp"
></TextView> ></TextView>
<com.sbw.atrue.Order.Activity.CustomHorizontalProgresWithNum <practice.csy.com.customprogress.view.CustomHorizontalProgresNoNum
android:layout_marginTop="0dp" android:layout_marginTop="0dp"
android:paddingLeft="10dp" android:paddingLeft="10dp"
android:paddingRight="10dp" android:paddingRight="10dp"
android:id="@+id/horizontalProgress01" android:id="@+id/horizontalProgress01"
android:progress="0" android:progress="0"
app:HorizontalProgresReachColor="#00ffaa" app:HorizontalProgresReachColor="#00ffaa"
app:HorizontalProgresUnReachColor="#cab0b0" app:HorizontalProgresUnReachColor="#cab0b0"
app:HorizontalProgresTextColor="#00ffaa" app:HorizontalProgresTextColor="#00ffaa"
android:layout_width="300dp" android:layout_width="300dp"
android:layout_height="50dp"/> android:layout_height="50dp"/>
<TextView <TextView
android:layout_width="100dp" android:layout_width="100dp"
android:layout_height="20dp" android:layout_height="20dp"
android:text="糖分" android:text="糖分"
android:textColor="#36513e" android:textColor="#36513e"
android:layout_marginTop="20dp" android:layout_marginTop="20dp"
></TextView> ></TextView>
<com.sbw.atrue.Order.Activity.CustomHorizontalProgresWithNum <practice.csy.com.customprogress.view.CustomHorizontalProgresWithNum
android:layout_marginTop="0dp" android:layout_marginTop="0dp"
android:paddingLeft="10dp" android:paddingLeft="10dp"
android:paddingRight="10dp" android:paddingRight="10dp"
android:id="@+id/horizontalProgress1" android:id="@+id/horizontalProgress1"
android:progress="0" android:progress="0"
app:HorizontalProgresReachColor="#00ffaa" app:HorizontalProgresReachColor="#00ffaa"
app:HorizontalProgresUnReachColor="#cab0b0" app:HorizontalProgresUnReachColor="#cab0b0"
app:HorizontalProgresTextColor="#00ffaa" app:HorizontalProgresTextColor="#00ffaa"
android:layout_width="300dp" android:layout_width="300dp"
android:layout_height="30dp"> android:layout_height="30dp">
</com.sbw.atrue.Order.Activity.CustomHorizontalProgresWithNum> </practice.csy.com.customprogress.view.CustomHorizontalProgresWithNum>
<TextView <TextView
android:layout_width="100dp" android:layout_width="100dp"
android:layout_height="20dp" android:layout_height="20dp"
android:text="维生素" android:text="维生素"
android:textColor="#36513e" android:textColor="#36513e"
android:layout_marginTop="20dp" android:layout_marginTop="20dp"
></TextView> ></TextView>
<com.sbw.atrue.Order.Activity.CustomHorizontalProgresWithNum <practice.csy.com.customprogress.view.CustomHorizontalProgresWithNum
android:paddingLeft="10dp" android:paddingLeft="10dp"
android:paddingRight="10dp" android:paddingRight="10dp"
android:layout_marginTop="20dp" android:layout_marginTop="20dp"
android:id="@+id/horizontalProgress2" android:id="@+id/horizontalProgress2"
android:progress="0" android:progress="0"
android:background="#ffffff" android:background="#ffffff"
app:HorizontalProgresReachColor="#00ffaa" app:HorizontalProgresReachColor="#00ffaa"
app:HorizontalProgresUnReachColor="#cab0b0" app:HorizontalProgresUnReachColor="#cab0b0"
app:HorizontalProgresTextColor="#00ffaa" app:HorizontalProgresTextColor="#00ffaa"
android:layout_width="300dp" android:layout_width="300dp"
android:layout_height="30dp"> android:layout_height="30dp">
</com.sbw.atrue.Order.Activity.CustomHorizontalProgresWithNum> </practice.csy.com.customprogress.view.CustomHorizontalProgresWithNum>
<TextView <TextView
android:layout_width="100dp" android:layout_width="100dp"
android:layout_height="20dp" android:layout_height="20dp"
android:text="其他" android:text="其他"
android:textColor="#36513e" android:textColor="#36513e"
android:layout_marginTop="20dp" android:layout_marginTop="20dp"
></TextView> ></TextView>
<com.sbw.atrue.Order.Activity.CustomHorizontalProgresWithNum <practice.csy.com.customprogress.view.CustomHorizontalProgresWithNum
android:paddingLeft="10dp" android:paddingLeft="10dp"
android:paddingRight="10dp" android:paddingRight="10dp"
android:layout_marginTop="20dp" android:layout_marginTop="20dp"
android:id="@+id/horizontalProgress3" android:id="@+id/horizontalProgress3"
android:progress="0" android:progress="0"
app:HorizontalProgresReachColor="#00ffaa" app:HorizontalProgresReachColor="#00ffaa"
app:HorizontalProgresUnReachColor="#cab0b0" app:HorizontalProgresUnReachColor="#cab0b0"
app:HorizontalProgresUnReachHeight="10dp" app:HorizontalProgresUnReachHeight="10dp"
app:HorizontalProgresReachHeight="10dp" app:HorizontalProgresReachHeight="10dp"
app:HorizontalProgresTextColor="#00FEA9" app:HorizontalProgresTextColor="#00FEA9"
android:layout_width="300dp" android:layout_width="300dp"
android:layout_height="30dp"> android:layout_height="30dp">
</com.sbw.atrue.Order.Activity.CustomHorizontalProgresWithNum> </practice.csy.com.customprogress.view.CustomHorizontalProgresWithNum>
</LinearLayout> </LinearLayout>
</ScrollView> </ScrollView>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 304 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 77 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 148 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 37 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 45 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 132 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 46 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 270 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 49 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 34 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 38 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 31 KiB

Binary file not shown.

@ -1,2 +0,0 @@
#Fri Oct 21 23:04:16 GMT+08:00 2022
gradle.version=6.4.1

Binary file not shown.

File diff suppressed because it is too large Load Diff

Binary file not shown.

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save