public class GacProgressBar extends View {
private int maxWidth;
private int maxHeight;
private Paint textPaint;
private int mBackgroudColor;
private int mForegroundColor;
private int textColor;
private int textSize;
private float mPercent;
private String mDescriptionText;
ValueAnimator animator;
private int currentWidth;
public GacProgressBar(Context context) {
super(context);
init();
}
public GacProgressBar(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
public GacProgressBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init(){
if(mBackgroudColor == 0){
mBackgroudColor = Color.parseColor("#eeeeee");
}
if(mForegroundColor == 0){
mForegroundColor = Color.parseColor("#feaaaa");
}
textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
textColor = Color.parseColor("#000000");
mDescriptionText = "剩余20%";
textSize = 40;
mPercent = 0.2f;
}
private void setBackgroundPaint(){
textPaint.setColor(mBackgroudColor);
textPaint.setStyle(Paint.Style.FILL);
}
private void setForegroundPaint(){
textPaint.setColor(mForegroundColor);
textPaint.setStyle(Paint.Style.FILL);
}
private void setAnimation(){
animator = ValueAnimator.ofFloat(0,1);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float value = (float)animation.getAnimatedValue();
currentWidth =(int)(maxWidth* value*mPercent);
invalidate();
}
});
animator.setDuration(1000);
animator.start();
}
private void setTextPaint(){
textPaint.setColor(textColor);
textPaint.setTextSize(textSize);
}
public void setProgressBarConfig(ProgressBarConfig config){
mDescriptionText = config.getDescriptionText();
if(config.getForegroundColor() == 0){
mForegroundColor = Color.parseColor("#feaaaa");
}else{
mForegroundColor = convertColor(config.getForegroundColor());
}
if(config.getBackgroudColor() == 0){
mBackgroudColor = Color.parseColor("#eeeeee");
}else{
mBackgroudColor = convertColor(config.getBackgroudColor());
}
mPercent = config.getPercent();
if(config.getTextColor() == 0){
textColor = Color.parseColor("000000");
}else{
textColor = convertColor(config.getTextColor());
}
if(config.getTextSize() == 0){
textSize = convertsize(16);
}else{
textSize = convertsize(config.getTextSize());
}
if(config.isAnimation()){
setAnimation();
}else{
invalidate();
Log.e("maxWidth","maxWidth:"+maxWidth+" curWidth:"+currentWidth+" percent:"+mPercent);
}
}
private int convertColor(int color){
return ContextCompat.getColor(getContext(), color);
}
private int convertsize(int size){
return (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, size,
getResources().getDisplayMetrics());
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
maxWidth = measureWidth(widthMeasureSpec);
maxHeight = measureHeight(heightMeasureSpec);
setMeasuredDimension(maxWidth, maxHeight);
currentWidth = (int)(maxWidth *mPercent);
}
private int measureWidth(int measureSpec) {
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
int result = 500;
if (specMode == MeasureSpec.AT_MOST) {
result = specSize;
} else if (specMode == MeasureSpec.EXACTLY) {
result = specSize;
}
return result;
}
private int measureHeight(int measureSpec) {
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
int result = 500;
if (specMode == MeasureSpec.AT_MOST) {
result = specSize;
} else if (specMode == MeasureSpec.EXACTLY) {
result = specSize;
}
return result;
}
@Override
protected void onDraw(Canvas canvas) {
drawBackground(canvas);
drawForeground(canvas);
drawText(canvas);
}
private void drawBackground(Canvas canvas){
setBackgroundPaint();
canvas.drawRect(0,0,maxWidth,maxHeight,textPaint);
}
private void drawForeground(Canvas canvas){
setForegroundPaint();
canvas.drawRect(0,0,currentWidth,maxHeight,textPaint);
}
private void drawText(Canvas canvas){
if(TextUtils.isEmpty(mDescriptionText)){
return;
}
setTextPaint();
canvas.drawText(mDescriptionText,40,maxHeight,textPaint);
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
if(animator != null){
animator.cancel();
}
}
}