package com.example.xuf.lonwinofficauto.view;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import com.example.xuf.lonwinofficauto.R;
/**
* 横向进度条
*/
public class HorizontalProgressBar extends View {
private int max;
private double progress;
private int bgColor;
private int progressColor;
private int padding;
private boolean openGradient;
private int gradientFrom;
private int gradientTo;
private boolean showSecondProgress;
private int secondProgress;
private int secondProgressShape;
private boolean showZeroPoint;
private Paint secondProgressPaint;
private Paint secondGradientPaint;
private Paint progressPaint;
private Paint gradientPaint;
private Paint bgPaint;
private boolean openSecondGradient;
private int secondGradientFrom;
private int secondGradientTo;
private int secondProgressColor;
private int radius;
private boolean drawBorder = false;
private int borderColor;
private int borderWidth;
/**
* 文字百分比的字体大小(sp)
*/
private int paintTextSize;
/**
* 百分比文字的颜色
*/
private int paintTextColor;
/**
* 画中间的百分比文字的画笔
*/
private Paint paintText;
/**
* 要画的文字的宽度
*/
private int textWidth;
/**
* 画文字时底部的坐标
*/
private float textBottomY;
/**
* 得到自定义视图的Y轴中心点
*/
private int viewCenterY;
/**
* 包裹文字的矩形
*/
private Rect rect = new Rect();
/**
* 得到自定义视图的宽度
*/
private int viewWidth;
/**
* 文字总共移动的长度(即从0%到100%文字左侧移动的长度)
*/
private int totalMovedLength;
/**
* Contxt
*/
private Context context;
// private boolean useRectMode = false;
private int showMode = 0;
private Paint borderPaint;
public static enum ShowMode {
ROUND, RECT, ROUND_RECT
}
private String mText;
private OnProgressChangedListener onProgressChangedListener;
public interface OnProgressChangedListener {
void onProgressChanged(HorizontalProgressBar progressBar, int max, double progress);
void onSecondProgressChanged(HorizontalProgressBar progressBar, int max, double progress);
}
public HorizontalProgressBar(Context context) {
super(context);
this.context = context;
init(context, null);
}
public HorizontalProgressBar(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public HorizontalProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
initAttrs(context, attrs);
initPaths();
}
private void initAttrs(Context context, AttributeSet attrs) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.HorizontalProgressBar);
max = a.getInteger(R.styleable.HorizontalProgressBar_zpb_max, 100);
progress = a.getInteger(R.styleable.HorizontalProgressBar_zpb_progress, 0);
bgColor = a.getColor(R.styleable.HorizontalProgressBar_zpb_bg_color, 0xffB8B8B8);
progressColor = a.getColor(R.styleable.HorizontalProgressBar_zpb_pb_color, 0xffFF4081);
secondProgressColor = a.getColor(R.styleable.HorizontalProgressBar_zpb_second_pb_color, 0xffFF4081);
padding = a.getDimensionPixelSize(R.styleable.HorizontalProgressBar_zpb_padding, 0);
showZeroPoint = a.getBoolean(R.styleable.HorizontalProgressBar_zpb_show_zero_point, false);
showSecondProgress = a.getBoolean(R.styleable.HorizontalProgressBar_zpb_show_second_progress, false);
secondProgress = a.getInteger(R.styleable.HorizontalProgressBar_zpb_second_progress, 0);
secondProgressShape = a.getInteger(R.styleable.HorizontalProgressBar_zpb_show_second_point_shape, 0);
openGradient = a.getBoolean(R.styleable.HorizontalProgressBar_zpb_open_gradient, false);
gradientFrom = a.getColor(R.styleable.HorizontalProgressBar_zpb_gradient_from, 0xffFF4081);
paintTextColor = a.getColor(R.styleable.HorizontalProgressBar_zpb_text, 0xffcd5c5c);
gradientTo = a.getColor(R.styleable.HorizontalProgressBar_zpb_gradient_to, 0xffFF4081);
openSecondGradient = a.getBoolean(R.styleable.HorizontalProgressBar_zpb_open_second_gradient, false);
showMode = a.getInt(R.styleable.HorizontalProgressBar_zpb_show_mode, 0);
secondGradientFrom = a.getColor(R.styleable.HorizontalProgressBar_zpb_second_gradient_from, 0xffFF4081);
secondGradientTo = a.getColor(R.styleable.HorizontalProgressBar_zpb_second_gradient_to, 0xffFF4081);
radius = a.getDimensionPixelSize(R.styleable.HorizontalProgressBar_zpb_round_rect_radius, 20);
drawBorder = a.getBoolean(R.styleable.HorizontalProgressBar_zpb_draw_border, false);
borderWidth = a.getDimensionPixelSize(R.styleable.HorizontalProgressBar_zpb_border_width, 1);
paintTextSize = a.getDimensionPixelSize(R.styleable.HorizontalProgressBar_zpb_border_width, 20);
borderColor = a.getColor(R.styleable.HorizontalProgressBar_zpb_border_color, 0xffff001f);
a.recycle();
}
private void initPaths() {
progressPaint = new Paint();
progressPaint.setColor(progressColor);
progressPaint.setStyle(Paint.Style.FILL);
progressPaint.setAntiAlias(true);
secondProgressPaint = new Paint();
secondProgressPaint.setColor(secondProgressColor);
secondProgressPaint.setStyle(Paint.Style.FILL);
secondProgressPaint.setAntiAlias(true);
gradientPaint = new Paint();
gradientPaint.setStyle(Paint.Style.FILL);
gradientPaint.setAntiAlias(true);
secondGradientPaint = new Paint();
secondGradientPaint.setStyle(Paint.Style.FILL);
secondGradientPaint.setAntiAlias(true);
bgPaint = new Paint();
bgPaint.setColor(bgColor);
bgPaint.setStyle(Paint.Style.FILL);
bgPaint.setAntiAlias(true);
borderPaint = new Paint();
borderPaint.setColor(borderColor);
borderPaint.setStyle(Paint.Style.STROKE);
borderPaint.setStrokeWidth(borderWidth);
borderPaint.setAntiAlias(true);
//设置百分比文字的尺寸
// int paintTextSizePx = sp2px(context, paintTextSize);
// 百分比文字画笔的属性
paintText = new Paint();
mText = "Udf32fA";
// paintText.setColor(Color.BLACK);
paintText.setColor(Color.parseColor("#333333"));
paintText.setTextSize(30);
paintText.setAntiAlias(true);
paintText.setTypeface(Typeface.DEFAULT_BOLD);
//获得绘制文本的宽和高
rect = new Rect();
paintText.getTextBounds(mText, 0, mText.length(), rect);
}
/**
* 得到视图等的高度宽度尺寸数据
*/
private void getWidthAndHeight() {
//得到包围文字的矩形的宽高
paintText.getTextBounds("000%", 0, "000%".length(), rect);
textWidth = rect.width();
textBottomY = viewCenterY + rect.height() / 2;
//得到自定义视图的高度
int viewHeight = getMeasuredHeight();
viewWidth = getMeasuredWidth();
viewCenterY = viewHeight / 2;
totalMovedLength = viewWidth - textWidth;
}
@Overr
评论10