学习篇---progressbar

跟着hyman大神学自定义progressbar,放在博客上记录一下;


HorizontalProgressBarWithNumber.java
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.widget.ProgressBar;

/**
 * Created by Administrator on 2016/5/19.
 */
public class HorizontalProgressBarWithNumber extends ProgressBar {

    protected static final int DEFAULT_TEXT_SIZE = 10;//默认的字体大小
    protected static final int DEFAULT_TEXT_COLOR = 0XFFFC00D1;//默认的字体颜色
    protected static final int DEFAULT_COLOR_UNREACHED_COLOR = 0xFFd3d6da;//默认progressbar进度没到达的颜色
    protected static final int DEFAULT_HEIGHT_REACHED_PROGRESS_BAR = 2;//默认progressbar进度到达的高度
    protected static final int DEFAULT_HEIGHT_UNREACHED_PROGRESS_BAR = 2;//默认progressbar进度没到达的高度
    protected static final int DEFAULT_SIZE_TEXT_OFFSET = 10;//默认的字体偏移量


    protected int mTextColor = DEFAULT_TEXT_COLOR;
    protected int mTextSize = sp2px(DEFAULT_TEXT_SIZE);
    protected int mTextOffset = dp2px(DEFAULT_SIZE_TEXT_OFFSET);
    protected int mReachedProgressBarHeight = dp2px(DEFAULT_HEIGHT_REACHED_PROGRESS_BAR);
    protected int mReachedBarColor = DEFAULT_TEXT_COLOR;
    protected int mUnReachedBarColor = DEFAULT_COLOR_UNREACHED_COLOR;
    protected int mUnReachedProgressBarHeight = dp2px(DEFAULT_HEIGHT_UNREACHED_PROGRESS_BAR);

    protected Paint mPaint = new Paint();
    protected float realWith = 0;

    /**
     * 单位sp转换为px
     * @param spvalue
     * @return
     */
    public int sp2px(int spvalue) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, spvalue, getResources().getDisplayMetrics());
    }

    /**
     * 单位dp转换为px
     * @param dipvalue
     * @return
     */
    public int dp2px(int dipvalue) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dipvalue, getResources().getDisplayMetrics());
    }


    public HorizontalProgressBarWithNumber(Context context) {
        this(context, null);
    }

    public HorizontalProgressBarWithNumber(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HorizontalProgressBarWithNumber(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        obtainAttrs(attrs);
        mPaint.setTextSize(mTextSize);
        mPaint.setColor(mTextColor);
        setMax(100);
    }

    /**
     * 获取自定义属性
     * @param attrs
     */
    public void obtainAttrs(AttributeSet attrs) {
        TypedArray attributes = getContext().obtainStyledAttributes(attrs, R.styleable.HorizontalProgressBarWithNumber);

        mTextColor = attributes
                .getColor(
                        R.styleable.HorizontalProgressBarWithNumber_progress_text_color,
                        DEFAULT_TEXT_COLOR);
        mTextSize = (int) attributes.getDimension(
                R.styleable.HorizontalProgressBarWithNumber_progress_text_size,
                mTextSize);

        mReachedBarColor = attributes
                .getColor(
                        R.styleable.HorizontalProgressBarWithNumber_progress_reached_color,
                        mTextColor);
        mUnReachedBarColor = attributes
                .getColor(
                        R.styleable.HorizontalProgressBarWithNumber_progress_unreached_color,
                        DEFAULT_COLOR_UNREACHED_COLOR);
        mReachedProgressBarHeight = (int) attributes
                .getDimension(
                        R.styleable.HorizontalProgressBarWithNumber_progress_reached_bar_height,
                        mReachedProgressBarHeight);
        mUnReachedProgressBarHeight = (int) attributes
                .getDimension(
                        R.styleable.HorizontalProgressBarWithNumber_progress_unreached_bar_height,
                        mUnReachedProgressBarHeight);
        mTextOffset = (int) attributes
                .getDimension(
                        R.styleable.HorizontalProgressBarWithNumber_progress_text_offset,
                        mTextOffset);
        attributes.recycle();
    }


    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int resultHeight = 0;
        int withSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        if (heightMode == MeasureSpec.EXACTLY) {
            resultHeight = heightSize;
        } else {
            int textHeight = (int) (mPaint.descent() - mPaint.ascent());//获取文本的高度
            resultHeight = getPaddingTop() + getPaddingBottom() + Math.max(Math.max(Math.abs(textHeight), mUnReachedProgressBarHeight), mReachedProgressBarHeight);
            if (heightMode == MeasureSpec.AT_MOST) {
                resultHeight = Math.min(heightSize, resultHeight);
            }
        }
        setMeasuredDimension(withSize, resultHeight);

        Log.e("TAG", realWith + "");

    }

    @Override
    protected synchronized void onDraw(Canvas canvas) {
        canvas.save();
        canvas.translate(getPaddingLeft(), getMeasuredHeight() / 2f);
        String text = getProgress() + "%";
        float textWith = mPaint.measureText(text);//获取文本的宽度
        float textHeight = (mPaint.descent() + mPaint.ascent()) / 2f;//获取文本的高度
        realWith = getMeasuredWidth() - getPaddingLeft() - getPaddingRight() - textWith - mTextOffset;
        float reachWith = realWith * (getProgress() * 1f) / getMax();
        boolean isNeedGo = true;//记录进度是否未满
        if (reachWith > realWith) {
            isNeedGo = false;
            reachWith = realWith;
        }
        //画进度到达的线
        if (reachWith > 0) {
            mPaint.setColor(mReachedBarColor);
            mPaint.setStrokeWidth(mReachedProgressBarHeight*2f);
            canvas.drawLine(0, 0, reachWith, 0, mPaint);
        }
        //绘制文本
        mPaint.setColor(mTextColor);
        mPaint.setTextSize(mTextSize);
        canvas.drawText(text, reachWith + mTextOffset / 2f, -textHeight, mPaint);
        //画进度未到达的线
        if(isNeedGo){
            mPaint.setColor(mUnReachedBarColor);
            mPaint.setStrokeWidth(mUnReachedProgressBarHeight);
            canvas.drawLine(reachWith+mTextOffset+textWith,0,getMeasuredWidth() - getPaddingLeft() - getPaddingRight(),0,mPaint);
        }

        canvas.restore();
    }
}
attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="HorizontalProgressBarWithNumber">
        <attr name="progress_unreached_color" format="color" />
        <attr name="progress_reached_color" format="color" />
        <attr name="progress_reached_bar_height" format="dimension" />
        <attr name="progress_unreached_bar_height" format="dimension" />
        <attr name="progress_text_size" format="dimension" />
        <attr name="progress_text_color" format="color" />
        <attr name="progress_text_offset" format="dimension" />
        <!--<attr name="progress_text_visibility" format="enum">-->
        <!--<enum name="visible" value="0" />-->
        <!--<enum name="invisible" value="1" />-->
        <!--</attr>-->
    </declare-styleable>
    <declare-styleable name="CirCleProgressBarWithNum">
        <attr name="Radius" format="dimension" />
    </declare-styleable>
</resources>
CirCleProgressBarWithNum.java
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;


/**
 * Created by Administrator on 2016/5/20.
 */
public class CirCleProgressBarWithNum extends HorizontalProgressBarWithNumber {
    public static final int DEFAULT_RADIUS=50;

    public float radius=dp2px(50);
    public float maxWith;
    public int expect;
    public int result;
    public Paint paint;

    public CirCleProgressBarWithNum(Context context) {
        this(context,null);
    }

    public CirCleProgressBarWithNum(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CirCleProgressBarWithNum(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
       TypedArray typedArray=getContext().obtainStyledAttributes(attrs, new int[]{R.styleable.CirCleProgressBarWithNum_Radius});
        radius= (int) typedArray.getDimension(R.styleable.HorizontalProgressBarWithNumber_progress_text_color,radius);
        paint=new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setTextSize(mTextSize);
        mReachedProgressBarHeight*=2;
    }

    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        maxWith=Math.max(mReachedProgressBarHeight,mUnReachedProgressBarHeight);
        expect = (int) (maxWith+radius*2+getPaddingLeft()+getPaddingRight());
        int with=resolveSize(expect,widthMeasureSpec);
        int height=resolveSize(expect,heightMeasureSpec);
        result=Math.max(with,height);
        setMeasuredDimension(result,result);
        setMax(100);
        radius=(result-getPaddingLeft()-getPaddingRight()-maxWith)/2f;
    }

    @Override
    protected synchronized void onDraw(Canvas canvas) {
        String text=getProgress()+"%";
        float textWith=paint.measureText(text);
        float textHeight=(paint.descent()+paint.ascent())/2f;
        canvas.save();
        canvas.translate(getPaddingLeft()+maxWith/2f,getPaddingTop()+maxWith/2f);

        paint.setStyle(Paint.Style.STROKE);

        paint.setColor(mUnReachedBarColor);
        paint.setStrokeWidth(mUnReachedProgressBarHeight);
        canvas.drawCircle(radius,radius,radius,paint);

        paint.setColor(mReachedBarColor);
        paint.setStrokeWidth(mReachedProgressBarHeight);
        float dgree=getProgress()*1f/getMax()*360;
        canvas.drawArc(new RectF(0,0,radius*2,radius*2),0,dgree,false,paint);

        paint.setColor(mTextColor);
        paint.setTextSize(mTextSize);
        paint.setStyle(Paint.Style.FILL);
        canvas.drawText(text,radius-textWith/2f,radius-textHeight/2f,paint);

        canvas.restore();



    }
}






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值