添加代码
@Override
protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthVal=MeasureSpec.getSize(widthMeasureSpec);
int height=measureHeight(heightMeasureSpec);
setMeasuredDimension(widthVal,height);
mRealWidth=getMeasuredWidth()-getPaddingLeft()-getPaddingRight();
}
private int measureHeight(int heightMeasureSpec) {
int result;
int mode= MeasureSpec.getMode(heightMeasureSpec);
int size=MeasureSpec.getSize(heightMeasureSpec);
if(mode==MeasureSpec.EXACTLY)
{
result= size;
}else {
int textHeight= (int) (mPaint.descent()-mPaint.ascent());
result=getPaddingTop()+getPaddingBottom()
+Math.max(Math.max(mReachHeight,mUnReachHeight),
Math.abs(textHeight));
if(mode==MeasureSpec.AT_MOST)
{
result=Math.min(result,size);
}
}
return result;
}
@Override
protected synchronized void onDraw(Canvas canvas) {
canvas.save();
canvas.translate(getPaddingLeft(),getHeight()/2);
boolean noNeedUnreach=false;
float radio=getProgress()*1.0f/getMax();
canvas.restore();
}
完整代码
package myapplication4.xt.com.myapplication4;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.ProgressBar;
/**
* Created by TONG on 2017/3/23.
*/
public class HorizontalProgressbarWithProgress extends ProgressBar{
private static final int DEFAULT_TEXT_SIZE=10;//sp
private static final int DEFAULT_TEXT_COLOR=0xFFFC00D1;
private static final int DEFAULT_COLOR_UNREACH=0xFFD3D6DA;
private static final int DEFAULT_HEIGHT_UNREACH=2;
private static final int DEFAULT_COLOR_REACH=DEFAULT_TEXT_COLOR;
private static final int DEFAULT_HEIGHT_REACH=2;//dp
private static final int DEFAULT_TEXT_OFFSET=10;//dp
private int mTextSize=sp2px(DEFAULT_TEXT_SIZE);
private int mTextColor=DEFAULT_TEXT_COLOR;
private int mUnReachColor=DEFAULT_COLOR_UNREACH;
private int mUnReachHeight=dp2px(DEFAULT_HEIGHT_UNREACH);
private int mReachColor=DEFAULT_COLOR_REACH;
private int mReachHeight=dp2px(DEFAULT_HEIGHT_REACH);
private int mTextOffset=dp2px(DEFAULT_TEXT_OFFSET);
private Paint mPaint=new Paint();
private int mRealWidth;
public HorizontalProgressbarWithProgress(Context context) {
this(context,null);
}
public HorizontalProgressbarWithProgress(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public HorizontalProgressbarWithProgress(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
obtainStyledAttrs(attrs);
}
/**
* 获取自定义属性
* @param attrs
*/
private void obtainStyledAttrs(AttributeSet attrs) {
TypedArray ta=getContext().obtainStyledAttributes
(attrs,R.styleable.HorizontalProgressbarWithProgress);
mTextSize= (int) ta.getDimension
(R.styleable.HorizontalProgressbarWithProgress_progress_text_size,mTextSize);
mTextColor=ta.getColor
(R.styleable.HorizontalProgressbarWithProgress_progress_text_color,mTextColor);
mTextOffset= (int) ta.getDimension
(R.styleable.HorizontalProgressbarWithProgress_progress_text_offset,mTextOffset);
mUnReachColor=ta.getColor
(R.styleable.HorizontalProgressbarWithProgress_progress_unreach_color,mUnReachColor);
mUnReachHeight= (int) ta.getDimension
(R.styleable.HorizontalProgressbarWithProgress_progress_unreach_height,mUnReachHeight);
mReachColor=ta.getColor
(R.styleable.HorizontalProgressbarWithProgress_progress_reach_color,mReachColor);
mReachHeight= (int) ta.getDimension
(R.styleable.HorizontalProgressbarWithProgress_progress_reach_height,mReachHeight);
ta.recycle();
mPaint.setTextSize(mTextSize);
}
@Override
protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthVal=MeasureSpec.getSize(widthMeasureSpec);
int height=measureHeight(heightMeasureSpec);
setMeasuredDimension(widthVal,height);
mRealWidth=getMeasuredWidth()-getPaddingLeft()-getPaddingRight();
}
private int measureHeight(int heightMeasureSpec) {
int result;
int mode= MeasureSpec.getMode(heightMeasureSpec);
int size=MeasureSpec.getSize(heightMeasureSpec);
if(mode==MeasureSpec.EXACTLY)
{
result= size;
}else {
int textHeight= (int) (mPaint.descent()-mPaint.ascent());
result=getPaddingTop()+getPaddingBottom()
+Math.max(Math.max(mReachHeight,mUnReachHeight),
Math.abs(textHeight));
if(mode==MeasureSpec.AT_MOST)
{
result=Math.min(result,size);
}
}
return result;
}
@Override
protected synchronized void onDraw(Canvas canvas) {
canvas.save();
canvas.translate(getPaddingLeft(),getHeight()/2);
boolean noNeedUnreach=false;
float radio=getProgress()*1.0f/getMax();
canvas.restore();
}
private int dp2px(int dpval){
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dpval,
getResources().getDisplayMetrics());
}
private int sp2px(int spValue){
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,spValue,
getResources().getDisplayMetrics());
}
}