android 自定义长条进度条_android 自定义进度条

package com.sample.util;

importandroid.annotation.SuppressLint;importandroid.content.Context;importandroid.content.res.TypedArray;importandroid.graphics.Canvas;importandroid.graphics.Paint;importandroid.graphics.Rect;importandroid.graphics.RectF;importandroid.util.AttributeSet;importandroid.util.Log;importandroid.view.View;importcom.gj.gateway.R;public class HProgressBar extendsView {private final String TAG = "HProgressBar";private int mProgress_outline_color = 0xFFFFFFFF;//外边框颜色

private int mProgress_color = 0xFFFFFFFF; //进度条颜色

private int mProgress_circle_color = 0xFFD1F4DE; //圆圈颜色

private int mProgress_text_color = 0xFFFFFFFF; //进度字体颜色

private int mProgress_text_bg_color = 0x50FFFFFF; //进度背景颜色

private float mProgress_circle_height = 56;//56;//圆圈高度

private float mProgress_height = 40;//25;//progress高度

private float mProgress_bar_height = 30; //progress进度条高度

private float mProgress_text_height = 35; //进度文字高度

private float mProgress_text_paddingH = 25; //进度文字左右padding

private float mProgress_text_paddingV = 0; //进度文字上下pading

private float mProgress_text_size = 32; //进度文字字体大小

private float mMaxProgress = 100;private volatile float mProgress_progress_bar = 0;private intmProgressWidth;privatePaint mOutLinePaint;privatePaint mProgressPaint;privatePaint mCirClePaint;privatePaint mTextPain;privatePaint mTextBgPain;publicHProgressBar(Context context) {super(context);

Log.d(TAG,"HProgressBar: 1");

}publicHProgressBar(Context context, AttributeSet attrs) {super(context, attrs);

Log.d(TAG,"HProgressBar: 2");

initDefStyleAttr(attrs);

mOutLinePaint= newPaint();

mOutLinePaint.setColor(mProgress_outline_color);

mOutLinePaint.setAntiAlias(true);

mOutLinePaint.setStrokeWidth(2);

mOutLinePaint.setStyle(Paint.Style.STROKE);//设置空心

mProgressPaint= newPaint();

mProgressPaint.setColor(mProgress_color);

mProgressPaint.setAntiAlias(true);

mCirClePaint= newPaint();

mCirClePaint.setColor(mProgress_circle_color);

mCirClePaint.setAntiAlias(true);

mTextBgPain= newPaint();

mTextBgPain.setColor(mProgress_text_bg_color);

mTextBgPain.setAntiAlias(true);

mTextPain= newPaint();

mTextPain.setColor(mProgress_text_color);

mTextPain.setAntiAlias(true);

mTextPain.setTextSize(mProgress_text_size);

mTextPain.setTextAlign(Paint.Align.CENTER);

mTextPain.setTextSize(mProgress_text_size);

}public HProgressBar(Context context, AttributeSet attrs, intdefStyleAttr) {super(context, attrs, defStyleAttr);

Log.d(TAG,"HProgressBar: 3");

}public HProgressBar(Context context, AttributeSet attrs, int defStyleAttr, intdefStyleRes) {super(context, attrs, defStyleAttr, defStyleRes);

Log.d(TAG,"HProgressBar: 4");

}private voidinitDefStyleAttr(AttributeSet attrs) {final TypedArray attributes =getContext().obtainStyledAttributes(attrs, R.styleable.HProgress);

mProgress_outline_color=attributes.getColor(R.styleable.HProgress_hProgress_outline_color, mProgress_outline_color);

mProgress_color=attributes.getColor(R.styleable.HProgress_hProgress_color, mProgress_color);

mProgress_circle_color=attributes.getColor(R.styleable.HProgress_hProgress_circle_color, mProgress_circle_color);

mProgress_text_color=attributes.getColor(R.styleable.HProgress_hProgress_text_color, mProgress_text_color);

mProgress_text_bg_color=attributes.getColor(R.styleable.HProgress_hProgress_text_bg_color, mProgress_text_bg_color);

mProgress_circle_height= (int) attributes.getDimension(R.styleable.HProgress_hProgress_circle_height, mProgress_circle_height);

mProgress_height= (int) attributes.getDimension(R.styleable.HProgress_hProgress_height, mProgress_height);

mProgress_bar_height= (int) attributes.getDimension(R.styleable.HProgress_hProgress_bar_height, mProgress_bar_height);

mProgress_text_height= (int) attributes.getDimension(R.styleable.HProgress_hProgress_text_height, mProgress_text_height);

mProgress_text_size= (int) attributes.getDimension(R.styleable.HProgress_hProgress_text_size, mProgress_text_size);

mProgress_text_paddingH= (int) attributes.getDimension(R.styleable.HProgress_hProgress_text_paddingH, mProgress_text_paddingH);

mProgress_text_paddingV= (int) attributes.getDimension(R.styleable.HProgress_hProgress_text_paddingV, mProgress_text_paddingV);

mProgress_progress_bar= attributes.getInteger(R.styleable.HProgress_hProgress_progress_bar, (int)mProgress_progress_bar);

mMaxProgress= attributes.getInteger(R.styleable.HProgress_hProgress_maxProgress, (int) mMaxProgress);

}

@Overrideprotected void onMeasure(int widthMeasureSpec, intheightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);

mProgressWidth=MeasureSpec.getSize(widthMeasureSpec);

setMeasuredDimension(mProgressWidth, (int )(mProgress_text_height +mProgress_circle_height));

}

@Overrideprotected voidonDraw(Canvas canvas) {super.onDraw(canvas);

Rect rect= newRect();

mTextPain.getTextBounds(String.valueOf(getProgress()),0, String.valueOf(getProgress()) .length(),rect);int w =rect.width();int h =rect.height();float spaceA = (mProgress_circle_height - mProgress_height) / 2;float spaceB = (mProgress_height - mProgress_bar_height) / 2;float progressMargin = mProgress_circle_height / 2;//长度变化范围等于控件长度减去两边的边距和空隙,由于是圆角进度条,还要减去内进度条的高度,即内进度条两边半圆的长度

float changeRange = mProgressWidth - (progressMargin + spaceB) * 2 -mProgress_bar_height;float startPoint = progressMargin +spaceB;float circleX = startPoint + mProgress_bar_height / 2 + changeRange * getProgress() /mMaxProgress;floattextStartX;floattextEndX;if (circleX - w / 2 - mProgress_text_paddingH < 0) {

textStartX= 0;

textEndX= textStartX + w + mProgress_text_paddingH * 2;

}else if (circleX + w / 2 + mProgress_text_paddingH >=mProgressWidth) {

textStartX= mProgressWidth - mProgress_text_paddingH * 2 -w;

textEndX=mProgressWidth;

}else{

textStartX= circleX - w / 2 -mProgress_text_paddingH;

textEndX= circleX + w / 2 +mProgress_text_paddingH;

}//画数字背景框

RectF rectF2 = newRectF(textStartX,

mProgress_circle_height,

textEndX,

mProgress_circle_height+mProgress_text_height);

canvas.drawRoundRect(rectF2, mProgress_text_height/ 2, mProgress_text_height / 2, mTextBgPain);//画数字进度

Paint.FontMetrics fontMetrics =mTextPain.getFontMetrics();

canvas.drawText((int)getProgress() + "",

(textStartX+ textEndX) / 2,

mProgress_circle_height+ mProgress_text_height / 2 + (fontMetrics.descent - fontMetrics.ascent) / 2 -fontMetrics.descent,

mTextPain);//画progressBar 外边

RectF rectF = new RectF(progressMargin, spaceA,mProgressWidth - progressMargin, mProgress_height +spaceA);

canvas.drawRoundRect(rectF, mProgress_height/ 2, mProgress_height / 2, mOutLinePaint);//画progressBar 内进度条

RectF rectF1 = newRectF(startPoint,

spaceA+spaceB,

startPoint+ mProgress_bar_height + changeRange * getProgress()/mMaxProgress,

mProgress_bar_height+ spaceB +spaceA);

canvas.drawRoundRect(rectF1, mProgress_bar_height/ 2, mProgress_bar_height / 2, mProgressPaint);//画圆

canvas.drawCircle(circleX,

mProgress_circle_height/ 2,

mProgress_circle_height/ 2, mCirClePaint);

}public intgetProgress() {return (int)mProgress_progress_bar;

}public void setProgress(intprogress) {if (progress >mMaxProgress) {throw new RuntimeException("progress mast less than mMaxProgress");

}

mProgress_progress_bar=progress;

postInvalidate();

}public void setmMaxProgress(intmaxProgress) {this.mMaxProgress =maxProgress;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值