activity_main.xml模拟器不显示?
package com.view;
import com.example.progressbar.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v4.content.res.TypedArrayUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.ProgressBar;
public class HorPBWithProgress extends ProgressBar {
private static final int DEFAULT_TEXT_SIZE = 10;
private static final int DEFAULT_TEXT_COLOR = 0xFFC00D1;
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_REACHE = 2;
private static final int DEFAULT_TEXT_OFFSET = 10;
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 = dp2px(DEFAULT_COLOR_REACH);
private int mReachHeight = dp2px(DEFAULT_HEIGHT_REACHE);
private int mTextOffset = dp2px(DEFAULT_TEXT_OFFSET);
private Paint mPaint = new Paint();
private int mRealWidth;
public HorPBWithProgress(Context context) {
this(context, null);
}
public HorPBWithProgress(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public HorPBWithProgress(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
obtainStyledAttrs(attrs);
}
private void obtainStyledAttrs(AttributeSet attrs) {
// TODO Auto-generated method stub
TypedArray ta = getContext().obtainStyledAttributes(attrs,
R.styleable.HorPBWithProgress);
mTextSize = (int) ta.getDimension(
R.styleable.HorPBWithProgress_progress_text_size, mTextSize);
mTextColor = ta.getColor(
R.styleable.HorPBWithProgress_progress_text_color, mTextColor);
mTextOffset = (int) ta
.getDimension(
R.styleable.HorPBWithProgress_progress_text_offset,
mTextOffset);
mUnReachColor = ta.getColor(
R.styleable.HorPBWithProgress_progress_unreach_color,
mUnReachColor);
mUnReachHeight = (int) ta.getDimension(
R.styleable.HorPBWithProgress_progress_unreach_height,
mUnReachHeight);
mReachHeight = (int) ta.getDimension(
R.styleable.HorPBWithProgress_progress_reach_height,
mReachHeight);
mReachColor = ta
.getColor(R.styleable.HorPBWithProgress_progress_reach_color,
mReachColor);
ta.recycle();
mPaint.setTextSize(mTextSize);
}
@Override
protected synchronized void onMeasure(int widthMeasureSpec,
int heightMeasureSpec) {
// int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthVal = MeasureSpec.getSize(widthMeasureSpec);
int height = measureHeight(heightMeasureSpec);
setMeasuredDimension(widthVal, height);
mRealWidth=getMeasuredWidth()-getPaddingLeft()-getPaddingRight();
}
private int measureHeight(int heightMeasureSpec) {
int result=0;
int mode=measureHeight(heightMeasureSpec);
int size=MeasureSpec.getSize(heightMeasureSpec);
if(mode==MeasureSpec.EXACTLY)
{
result=size;
}else
{
int textHeigh=(int) (mPaint.descent()-mPaint.ascent());
result=getPaddingTop()+getPaddingBottom()+Math.max(Math.max(mReachHeight,mUnReachHeight),Math.abs(textHeigh));
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 noNeedUnRech=false;
String text=getProgress()+"%";
int textWidth=(int)mPaint.measureText(text);
float radio=getProgress()*1.0f/getMax();
float progressX=radio*mRealWidth;
if(progressX+textWidth>mRealWidth){
progressX=mRealWidth-textWidth;
noNeedUnRech=true;
}
float endX=progressX-mTextOffset/2;
if(endX>0)
{
mPaint.setColor(mReachColor);
mPaint.setStrokeWidth(mReachHeight);
canvas.drawLine(0, 0, endX, 0, mPaint);
}
mPaint.setColor(mTextColor);
int y=(int) (-(mPaint.descent()+mPaint.ascent())/2);
canvas.drawText(text, progressX, y, mPaint);
if(!noNeedUnRech)
{
float start=progressX+mTextOffset/2+textWidth;
mPaint.setColor(mUnReachColor);
mPaint.setStrokeWidth(mUnReachHeight);
canvas.drawLine(start, 0, mRealWidth, 0, mPaint);
}
canvas.restore();
}
private int dp2px(int dpVal) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
dpVal, getResources().getDisplayMetrics());
}
private int sp2px(int spVal) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
spVal, getResources().getDisplayMetrics());
}
}