1、简单好用的进度框,做个笔记,便于以后使用,节约时间。
2、圆形进度框
public class CircleProgressBarView extends View {
private Context mContext;
/**
* 圆心x坐标
*/
private float centerX;
/**
* 圆心y坐标
*/
private float centerY;
/**
* 圆的半径
*/
private float radius;
/**
* 进度
*/
private float mProgress;
/**
* 当前进度
*/
private float currentProgress;
/**
* 圆形进度条底色画笔
*/
private Paint circleBgPaint;
/**
* 圆形进度条进度画笔
*/
private Paint progressPaint;
/**
* 进度条背景颜色
*/
private int circleBgColor = 0xFFe1e5e8;
/**
* 进度条颜色
*/
private int progressColor = 0xFF6376ff;
/**
* 默认圆环的宽度
*/
private int defaultStrokeWidth = 10;
/**
* 圆形背景画笔宽度
*/
private int circleBgStrokeWidth = defaultStrokeWidth;
/**
* 圆形进度画笔宽度
*/
private int progressStrokeWidth = defaultStrokeWidth;
/**
* 扇形所在矩形
*/
private RectF rectF = new RectF();
/**
* 进度动画
*/
private ValueAnimator progressAnimator;
/**
* 动画执行时间
*/
private int duration = 1000;
/**
* 动画延时启动时间
*/
private int startDelay = 500;
private boolean isDrawCenterProgressText;
private int centerProgressTextSize = 10;
private int centerProgressTextColor = Color.BLACK;
private Paint centerProgressTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private ProgressListener progressListener;
public CircleProgressBarView(Context context) {
this(context, null);
}
public CircleProgressBarView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
mContext = context;
getAttr(attrs);
initPaint();
initTextPaint();
}
private void getAttr(AttributeSet attrs) {
TypedArray typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.CircleProgressBarView);
circleBgStrokeWidth = typedArray.getDimensionPixelOffset(R.styleable.CircleProgressBarView_circleBgStrokeWidth, defaultStrokeWidth);
progressStrokeWidth = typedArray.getDimensionPixelOffset(R.styleable.CircleProgressBarView_progressStrokeWidth, defaultStrokeWidth);
circleBgColor = typedArray.getColor(R.styleable.CircleProgressBarView_circleBgColor, circleBgColor);
progressColor = typedArray.getColor(R.styleable.CircleProgressBarView_progressColor, progressColor);
duration = typedArray.getColor(R.styleable.CircleProgressBarView_circleAnimationDuration, duration);
isDrawCenterProgressText = typedArray.getBoolean(R.styleable.CircleProgressBarView_isDrawCenterProgressText, false);
centerProgressTextColor = typedArray.getColor(R.styleable.CircleProgressBarView_centerProgressTextColor, centerProgressTextColor);
centerProgressTextSize = typedArray.getDimensionPixelOffset(R.styleable.CircleProgressBarView_centerProgressTextSize, sp2px(centerProgressTextSize));
typedArray.recycle();
}
private void initPaint() {
circleBgPaint = getPaint(circleBgStrokeWidth, circleBgColor);
progressPaint = getPaint(progressStrokeWidth, progressColor);
}
private Paint getPaint(int strokeWidth, int color) {
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setStrokeWidth(strokeWidth);
paint.setColor(color);
paint.setAntiAlias(true);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStyle(Paint.Style.STROKE);
return paint;
}
/**
* 初始化文字画笔
*/
private void initTextPaint() {
centerProgressTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
centerProgressTextPaint.setTextSize(centerProgressTextSize);
centerProgressTextPaint.setColor(centerProgressTextColor);
centerProgressTextPaint.setTextAlign(Paint.Align.CENTER);
centerProgressTextPaint.setAntiAlias(true);
}
private void initAnimation() {
progressAnimator = ValueAnimator.ofFloat(0, mProgress);
progressAnimator.setDuration(duration);
progressAnimator.setStartDelay(startDelay);
progressAnimator.setInterpolator(new LinearInterpolator());
progressAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
float value = (float) valueAnimator.getAnimatedValue();
mProgress = value;
currentProgress = value * 360 / 100;
if (progressListener != null) {
progressListener.currentProgressListener(roundTwo(value));
}
invalidate();
}
});
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
centerX = w / 2;
centerY = h / 2;
radius = Math.min(w, h) / 2 - Math.max(circleBgStrokeWidth, progressStrokeWidth);
rectF.set(centerX - radius,
centerY - radius,
centerX + radius,
centerY + radius);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(centerX, centerY, radius, circleBgPaint);
canvas.drawArc(rectF, 90, currentProgress, false, progressPaint);
if (isDrawCenterProgressText) {
drawCenterProgressText(canvas, (int) mProgress + "%");
}
}
private void drawCenterProgressText(Canvas canvas, String currentProgress) {
Paint.FontMetricsInt fontMetrics = centerProgressTextPaint.getFontMetricsInt();
int baseline = (int) ((rectF.bottom + rectF.top - fontMetrics.bottom - fontMetrics.top) / 2);
//文字绘制到整个布局的中心位置
canvas.drawText(currentProgress, rectF.centerX(), baseline, centerProgressTextPaint);
}
public void startProgressAnimation() {
progressAnimator.start();
}
public void pauseProgressAnimation() {
progressAnimator.pause();
}
public void resumeProgressAnimation() {
progressAnimator.resume();
}
public void stopProgressAnimation() {
progressAnimator.end();
}
/**
* 传入一个进度值,从0到progress动画变化
*
* @param progress
* @return
*/
public CircleProgressBarView setProgressWithAnimation(float progress) {
mProgress = progress;
initAnimation();
return this;
}
/**
* 实时进度,适用于下载进度回调时候之类的场景
*
* @param progress
* @return
*/
public CircleProgressBarView setCurrentProgress(float progress) {
mProgress = progress;
currentProgress = progress * 360 / 100;
invalidate();
return this;
}
public interface ProgressListener {
void currentProgressListener(float currentProgress);
}
public CircleProgressBarView setProgressListener(ProgressListener listener) {
progressListener = listener;
return this;
}
/**
* 将一个小数四舍五入,保留两位小数返回
*
* @param originNum
* @return
*/
public static float roundTwo(float originNum) {
return (float) (Math.round(originNum * 10) / 10.00);
}
/**
* dp 2 px
*
* @param dpVal
*/
protected int dp2px(int dpVal) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
dpVal, getResources().getDisplayMetrics());
}
/**
* sp 2 px
*
* @param spVal
* @return
*/
protected int sp2px(int spVal) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
spVal, getResources().getDisplayMetrics());
}
}
3、直线进度条
/**
* Created by Allen on 2017/5/14.
* <p>
* 自定义水平进度条
*/
public class HorizontalProgressBar extends View {
private Paint bgPaint;
private Paint progressPaint;
private Paint tipPaint;
private Paint textPaint;
private int mWidth;
private int mHeight;
private int mViewHeight;
/**
* 进度
*/
private float mProgress;
/**
* 当前进度
*/
private float currentProgress;
/**
* 进度动画
*/
private ValueAnimator progressAnimator;
/**
* 动画执行时间
*/
private int duration = 1000;
/**
* 动画延时启动时间
*/
private int startDelay = 500;
/**
* 进度条画笔的宽度
*/
private int progressPaintWidth;
/**
* 百分比提示框画笔的宽度
*/
private int tipPaintWidth;
/**
* 百分比提示框的高度
*/
private int tipHeight;
/**
* 百分比提示框的宽度
*/
private int tipWidth;
/**
* 画三角形的path
*/
private Path path = new Path();
/**
* 三角形的高
*/
private int triangleHeight;
/**
* 进度条距离提示框的高度
*/
private int progressMarginTop;
/**
* 进度移动的距离
*/
private float moveDis;
private Rect textRect = new Rect();
private String textString = "0";
/**
* 百分比文字字体大小
*/
private int textPaintSize;
/**
* 进度条背景颜色
*/
private int bgColor = 0xFFe1e5e8;
/**
* 进度条颜色
*/
private int progressColor = 0xFFf66b12;
/**
* 绘制提示框的矩形
*/
private RectF rectF = new RectF();
/**
* 圆角矩形的圆角半径
*/
private int roundRectRadius;
/**
* 进度监听回调
*/
private ProgressListener progressListener;
public HorizontalProgressBar(Context context) {
super(context);
}
public HorizontalProgressBar(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
initPaint();
}
/**
* 初始化画笔宽度及view大小
*/
private void init() {
progressPaintWidth = dp2px(4);
tipHeight = dp2px(15);
tipWidth = dp2px(30);
tipPaintWidth = dp2px(1);
triangleHeight = dp2px(3);
roundRectRadius = dp2px(2);
textPaintSize = sp2px(10);
progressMarginTop = dp2px(8);
//view真实的高度
mViewHeight = tipHeight + tipPaintWidth + triangleHeight + progressPaintWidth + progressMarginTop;
}
/**
* 初始化画笔
*/
private void initPaint() {
bgPaint = getPaint(progressPaintWidth, bgColor, Paint.Style.STROKE);
progressPaint = getPaint(progressPaintWidth, progressColor, Paint.Style.STROKE);
tipPaint = getPaint(tipPaintWidth, progressColor, Paint.Style.FILL);
initTextPaint();
}
/**
* 初始化文字画笔
*/
private void initTextPaint() {
textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
textPaint.setTextSize(textPaintSize);
textPaint.setColor(Color.WHITE);
textPaint.setTextAlign(Paint.Align.CENTER);
textPaint.setAntiAlias(true);
}
/**
* 统一处理paint
*
* @param strokeWidth
* @param color
* @param style
* @return
*/
private Paint getPaint(int strokeWidth, int color, Paint.Style style) {
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setStrokeWidth(strokeWidth);
paint.setColor(color);
paint.setAntiAlias(true);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStyle(style);
return paint;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(measureWidth(widthMode, width), measureHeight(heightMode, height));
}
/**
* 测量宽度
*
* @param mode
* @param width
* @return
*/
private int measureWidth(int mode, int width) {
switch (mode) {
case MeasureSpec.UNSPECIFIED:
case MeasureSpec.AT_MOST:
break;
case MeasureSpec.EXACTLY:
mWidth = width;
break;
}
return mWidth;
}
/**
* 测量高度
*
* @param mode
* @param height
* @return
*/
private int measureHeight(int mode, int height) {
switch (mode) {
case MeasureSpec.UNSPECIFIED:
case MeasureSpec.AT_MOST:
mHeight = mViewHeight;
break;
case MeasureSpec.EXACTLY:
mHeight = height;
break;
}
return mHeight;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawLine(getPaddingLeft(),
tipHeight + progressMarginTop,
getWidth(),
tipHeight + progressMarginTop,
bgPaint);
canvas.drawLine(getPaddingLeft(),
tipHeight + progressMarginTop,
currentProgress,
tipHeight + progressMarginTop,
progressPaint);
drawTipView(canvas);
drawText(canvas, textString);
}
/**
* 绘制进度上边提示百分比的view
*
* @param canvas
*/
private void drawTipView(Canvas canvas) {
drawRoundRect(canvas);
drawTriangle(canvas);
}
/**
* 绘制圆角矩形
*
* @param canvas
*/
private void drawRoundRect(Canvas canvas) {
rectF.set(moveDis, 0, tipWidth + moveDis, tipHeight);
canvas.drawRoundRect(rectF, roundRectRadius, roundRectRadius, tipPaint);
}
/**
* 绘制三角形
*
* @param canvas
*/
private void drawTriangle(Canvas canvas) {
path.moveTo(tipWidth / 2 - triangleHeight + moveDis, tipHeight);
path.lineTo(tipWidth / 2 + moveDis, tipHeight + triangleHeight);
path.lineTo(tipWidth / 2 + triangleHeight + moveDis, tipHeight);
canvas.drawPath(path, tipPaint);
path.reset();
}
/**
* 绘制文字
*
* @param canvas 画布
*/
private void drawText(Canvas canvas, String textString) {
textRect.left = (int) moveDis;
textRect.top = 0;
textRect.right = (int) (tipWidth + moveDis);
textRect.bottom = tipHeight;
Paint.FontMetricsInt fontMetrics = textPaint.getFontMetricsInt();
int baseline = (textRect.bottom + textRect.top - fontMetrics.bottom - fontMetrics.top) / 2;
//文字绘制到整个布局的中心位置
canvas.drawText(textString + "%", textRect.centerX(), baseline, textPaint);
}
/**
* 进度移动动画 通过插值的方式改变移动的距离
*/
private void initAnimation() {
progressAnimator = ValueAnimator.ofFloat(0, mProgress);
progressAnimator.setDuration(duration);
progressAnimator.setStartDelay(startDelay);
progressAnimator.setInterpolator(new LinearInterpolator());
progressAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
float value = (float) valueAnimator.getAnimatedValue();
//进度数值只显示整数,我们自己的需求,可以忽略
textString = formatNum(format2Int(value));
//把当前百分比进度转化成view宽度对应的比例
currentProgress = value * mWidth / 100;
//进度回调方法
if (progressListener != null) {
progressListener.currentProgressListener(value);
}
//移动百分比提示框,只有当前进度到提示框中间位置之后开始移动,
//当进度框移动到最右边的时候停止移动,但是进度条还可以继续移动
//moveDis是tip框移动的距离
if (currentProgress >= (tipWidth / 2) &&
currentProgress <= (mWidth - tipWidth / 2)) {
moveDis = currentProgress - tipWidth / 2;
}
invalidate();
}
});
progressAnimator.start();
}
/**
* 设置进度条带动画效果
*
* @param progress
* @return
*/
public HorizontalProgressBar setProgressWithAnimation(float progress) {
mProgress = progress;
initAnimation();
return this;
}
/**
* 实时显示进度
*
* @param progress
* @return
*/
public HorizontalProgressBar setCurrentProgress(float progress) {
mProgress = progress;
currentProgress = progress * mWidth / 100;
textString = formatNum(format2Int(progress));
invalidate();
return this;
}
/**
* 开启动画
*/
public void startProgressAnimation() {
if (progressAnimator != null &&
!progressAnimator.isRunning() &&
!progressAnimator.isStarted())
progressAnimator.start();
}
/**
* 暂停动画
*/
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public void pauseProgressAnimation() {
if (progressAnimator != null) {
progressAnimator.pause();
}
}
/**
* 恢复动画
*/
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public void resumeProgressAnimation() {
if (progressAnimator != null)
progressAnimator.resume();
}
/**
* 停止动画
*/
public void stopProgressAnimation() {
if (progressAnimator != null) {
progressAnimator.end();
}
}
/**
* 回调接口
*/
public interface ProgressListener {
void currentProgressListener(float currentProgress);
}
/**
* 回调监听事件
*
* @param listener
* @return
*/
public HorizontalProgressBar setProgressListener(ProgressListener listener) {
progressListener = listener;
return this;
}
/**
* 格式化数字(保留两位小数)
*
* @param money
* @return
*/
public static String formatNumTwo(double money) {
DecimalFormat format = new DecimalFormat("0.00");
return format.format(money);
}
/**
* 格式化数字(保留一位小数)
*
* @param money
* @return
*/
public static String formatNum(int money) {
DecimalFormat format = new DecimalFormat("0");
return format.format(money);
}
/**
* dp 2 px
*
* @param dpVal
*/
protected int dp2px(int dpVal) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
dpVal, getResources().getDisplayMetrics());
}
/**
* sp 2 px
*
* @param spVal
* @return
*/
protected int sp2px(int spVal) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
spVal, getResources().getDisplayMetrics());
}
public static int format2Int(double i) {
return (int) i;
}
}
4、直线进度条,文字显示
/**
* Created by Allen on 2017/11/30.
* <p>
* 产品购买进度条
*/
public class ProductProgressBar extends View {
private Paint bgPaint;
private Paint progressPaint;
private Paint textPaint;
private int mWidth;
private int mHeight;
private int mViewHeight;
/**
* 进度
*/
private float mProgress;
//描述文字的高度
private float textHeight;
//描述文字的高度
private float textWidth;
/**
* 当前进度
*/
private float currentProgress;
/**
* 进度动画
*/
private ValueAnimator progressAnimator;
/**
* 动画执行时间
*/
private int duration = 1000;
/**
* 动画延时启动时间
*/
private int startDelay = 500;
/**
* 进度条画笔的宽度
*/
private int progressPaintWidth;
private int progressHeight;
/**
* 进度条距离提示框的高度
*/
private int progressMarginTop;
/**
* 进度移动的距离
*/
private float moveDis;
private Rect textRect = new Rect();
private String textString = "已售0%";
/**
* 百分比文字字体大小
*/
private int textPaintSize;
/**
* 进度条背景颜色
*/
private int bgColor = 0xFFeaeef0;
/**
* 进度条颜色
*/
private int progressColor = 0xFFf66b12;
private RectF bgRectF = new RectF();
private RectF progressRectF = new RectF();
/**
* 圆角矩形的圆角半径
*/
private int roundRectRadius;
/**
* 进度监听回调
*/
private ProgressListener progressListener;
public ProductProgressBar(Context context) {
this(context, null);
}
public ProductProgressBar(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public ProductProgressBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
initPaint();
initTextPaint();
}
/**
* 初始化画笔宽度及view大小
*/
private void init() {
progressPaintWidth = dp2px(1);
progressHeight = dp2px(3);
roundRectRadius = dp2px(3);
textPaintSize = sp2px(10);
textHeight = dp2px(10);
progressMarginTop = dp2px(4);
//view真实的高度
mViewHeight = (int) (textHeight + progressMarginTop + progressPaintWidth * 2 + progressHeight);
}
private void initPaint() {
bgPaint = getPaint(progressPaintWidth, bgColor, Paint.Style.FILL);
progressPaint = getPaint(progressPaintWidth, progressColor, Paint.Style.FILL);
}
/**
* 初始化文字画笔
*/
private void initTextPaint() {
textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
textPaint.setTextSize(textPaintSize);
textPaint.setColor(progressColor);
textPaint.setTextAlign(Paint.Align.CENTER);
textPaint.setAntiAlias(true);
}
/**
* 统一处理paint
*
* @param strokeWidth 画笔宽度
* @param color 颜色
* @param style 风格
* @return paint
*/
private Paint getPaint(int strokeWidth, int color, Paint.Style style) {
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setStrokeWidth(strokeWidth);
paint.setColor(color);
paint.setAntiAlias(true);
paint.setStyle(style);
return paint;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(measureWidth(widthMode, width), measureHeight(heightMode, height));
}
/**
* 测量宽度
*
* @param mode
* @param width
* @return
*/
private int measureWidth(int mode, int width) {
switch (mode) {
case MeasureSpec.UNSPECIFIED:
case MeasureSpec.AT_MOST:
break;
case MeasureSpec.EXACTLY:
mWidth = width;
break;
}
return mWidth;
}
/**
* 测量高度
*
* @param mode
* @param height
* @return
*/
private int measureHeight(int mode, int height) {
switch (mode) {
case MeasureSpec.UNSPECIFIED:
case MeasureSpec.AT_MOST:
mHeight = mViewHeight;
break;
case MeasureSpec.EXACTLY:
mHeight = height;
break;
}
return mHeight;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//绘制文字
drawText(canvas, textString);
//背景
drawBgProgress(canvas);
//进度条
drawProgress(canvas);
}
private void drawBgProgress(Canvas canvas) {
bgRectF.left = 0;
bgRectF.top = textHeight + progressMarginTop;
bgRectF.right = this.getMeasuredWidth();
bgRectF.bottom = bgRectF.top + progressHeight;
canvas.drawRoundRect(bgRectF, roundRectRadius, roundRectRadius, bgPaint);
}
private void drawProgress(Canvas canvas) {
progressRectF.left = 0;
progressRectF.top = textHeight + progressMarginTop;
progressRectF.right = currentProgress;
progressRectF.bottom = progressRectF.top + progressHeight;
canvas.drawRoundRect(progressRectF, roundRectRadius, roundRectRadius, progressPaint);
}
/**
* 绘制文字
*
* @param canvas 画布
*/
private void drawText(Canvas canvas, String textString) {
textRect.left = (int) moveDis;
textRect.top = 0;
textRect.right = (int) (textPaint.measureText(textString) + moveDis);
textRect.bottom = (int) textHeight;
Paint.FontMetricsInt fontMetrics = textPaint.getFontMetricsInt();
int baseline = (textRect.bottom + textRect.top - fontMetrics.bottom - fontMetrics.top) / 2;
//文字绘制到整个布局的中心位置
canvas.drawText(textString, textRect.centerX(), baseline, textPaint);
}
/**
* 进度移动动画 通过插值的方式改变移动的距离
*/
private void initAnimation() {
progressAnimator = ValueAnimator.ofFloat(0, mProgress);
progressAnimator.setDuration(duration);
progressAnimator.setStartDelay(startDelay);
progressAnimator.setInterpolator(new LinearInterpolator());
progressAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
float value = (float) valueAnimator.getAnimatedValue();
textString = "已售" + formatNum((int) value) + "%";
textWidth = textPaint.measureText(textString);
currentProgress = value * mWidth / 100;
if (progressListener != null) {
progressListener.currentProgressListener(value);
}
//移动百分比提示框,只有当前进度到提示框中间位置之后开始移动,当进度框移动到最右边的时候停止移动,但是进度条还可以继续移动
if (currentProgress >= textWidth && currentProgress <= mWidth) {
moveDis = currentProgress - textWidth;
}
invalidate();
}
});
if (!progressAnimator.isStarted()) {
progressAnimator.start();
}
}
/**
* 回调接口
*/
public interface ProgressListener {
void currentProgressListener(float currentProgress);
}
/**
* 回调监听事件
*
* @param listener
* @return
*/
public ProductProgressBar setProgressListener(ProgressListener listener) {
progressListener = listener;
return this;
}
public ProductProgressBar setProgress(float progress) {
mProgress = progress;
initAnimation();
return this;
}
/**
* 格式化数字(保留一位小数)
*
* @param money
* @return
*/
public static String formatNum(int money) {
DecimalFormat format = new DecimalFormat("0");
return format.format(money);
}
/**
* dp 2 px
*
* @param dpVal
*/
protected int dp2px(int dpVal) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
dpVal, getResources().getDisplayMetrics());
}
/**
* sp 2 px
*
* @param spVal
* @return
*/
protected int sp2px(int spVal) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
spVal, getResources().getDisplayMetrics());
}
}
5、使用方法再MainActivity中
public class MainActivity extends AppCompatActivity {
CircleProgressBarView circleProgressBarView;
HorizontalProgressBar horizontalProgressBar;
ProductProgressBar productProgressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
circleProgressBarView = (CircleProgressBarView) findViewById(R.id.circle_progress_view);
circleProgressBarView.setProgressWithAnimation(60);
circleProgressBarView.setProgressListener(new CircleProgressBarView.ProgressListener() {
@Override
public void currentProgressListener(float currentProgress) {
// textView.setText("当前进度:" + currentProgress);
}
});
circleProgressBarView.startProgressAnimation();
horizontalProgressBar = (HorizontalProgressBar) findViewById(R.id.horizontal_progress_view);
horizontalProgressBar.setProgressWithAnimation(60).setProgressListener(new HorizontalProgressBar.ProgressListener() {
@Override
public void currentProgressListener(float currentProgress) {
}
});
horizontalProgressBar.startProgressAnimation();
productProgressBar = (ProductProgressBar) findViewById(R.id.product_progress_view);
productProgressBar.setProgress(60).setProgressListener(new ProductProgressBar.ProgressListener() {
@Override
public void currentProgressListener(float currentProgress) {
Log.e("allen", "currentProgressListener: " + currentProgress);
}
});
}
}
6、布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cpbv="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.qqq.customviewtest.MainActivity">
<com.example.qqq.customviewtest.CircleProgressBarView
android:id="@+id/circle_progress_view"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginTop="20dp"
cpbv:centerProgressTextColor="@color/colorAccent"
cpbv:centerProgressTextSize="20sp"
cpbv:circleBgStrokeWidth="10dp"
cpbv:isDrawCenterProgressText="true"
cpbv:progressStrokeWidth="10dp" />
<com.example.qqq.customviewtest.HorizontalProgressBar
android:id="@+id/horizontal_progress_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp" />
<com.example.qqq.customviewtest.ProductProgressBar
android:id="@+id/product_progress_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp" />
</LinearLayout>
attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="PayPsdInputView">
<attr name="maxCount" format="integer" />
<attr name="circleColor" format="color" />
<attr name="bottomLineColor" format="color" />
<attr name="radius" format="dimension" />
<attr name="divideLineWidth" format="dimension" />
<attr name="divideLineColor" format="color" />
<attr name="rectAngle" format="dimension" />
<attr name="focusedColor" format="color"/>
<attr name="psdType" format="enum">
<enum name="weChat" value="0" />
<enum name="bottomLine" value="1" />
</attr>
</declare-styleable>
<declare-styleable name="CircleProgressBarView">
<attr name="circleBgStrokeWidth" format="dimension" />
<attr name="progressStrokeWidth" format="dimension" />
<attr name="circleBgColor" format="color" />
<attr name="progressColor" format="color" />
<attr name="circleAnimationDuration" format="integer" />
<attr name="isDrawCenterProgressText" format="boolean" />
<attr name="centerProgressTextColor" format="color"/>
<attr name="centerProgressTextSize" format="dimension"/>
</declare-styleable>
<declare-styleable name="RadarWaveView">
<attr name="waveColor" format="color" />
<attr name="waveAmplitude" format="dimension" />
<attr name="waveSpeed" format="float" />
<attr name="waveStartPeriod" format="float" />
<attr name="waveStart" format="boolean" />
<attr name="waveFillTop" format="boolean" />
<attr name="waveFillBottom" format="boolean" />
<attr name="waveFillType" format="enum">
<enum name="top" value="0" />
<enum name="bottom" value="1" />
</attr>
<attr name="waveType" format="enum">
<enum name="sin" value="0" />
<enum name="cos" value="1" />
</attr>
</declare-styleable>
</resources>
先记录这些,以后有好用的炫酷效果再记录,感谢
https://github.com/lygttpod/AndroidCustomView