自定义进度条

来源:互联网 发布:网络作家富豪榜 2016 编辑:程序博客网 时间:2024/05/01 11:17

转自慕课网的HyMan大神的,对其作了少许修改。

package com.bsi.customprogressdemo;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 ALEX on 2016/10/4. */public class HorizontalProgress extends ProgressBar {    private static final int DEFAULT_UNREACH_COLOR = 0xFFFF0000;    private static final int DEFAULT_UNREACH_HEIGHT = 2;    private static final int DEFAULT_REACH_COLOR = 0xFF00FF00;    private static final int DEFAULT_REACH_HEIGHT = 2;    private static final int DEFAULT_TEXT_SIZE = 15;    private static final int DEFAULT_TEXT_COLOR = 0XFF0000FF;    private static final int DEFAULT_TEXT_OFFSET = 10;    private int mTextColor = DEFAULT_TEXT_COLOR;    private int mTextSize = sp2px(DEFAULT_TEXT_SIZE);    private int mUnreachColor = DEFAULT_UNREACH_COLOR;    private int mUnreachHeight = dp2px(DEFAULT_UNREACH_HEIGHT);    private int mReachColor = DEFAULT_REACH_COLOR;    private int mReachHeight = dp2px(DEFAULT_REACH_HEIGHT);    private int mTextOffset = dp2px(DEFAULT_TEXT_OFFSET);    private Paint mPaint = new Paint();    private int mRealWidth;//控件的宽度    public HorizontalProgress(Context context) {        this(context, null);    }    public HorizontalProgress(Context context, AttributeSet attrs) {        this(context, attrs, 0);    }    public HorizontalProgress(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        obtainStyledAttrs(attrs);    }    private void obtainStyledAttrs(AttributeSet attrs) {        TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.HorizontalProgress);        mReachHeight = (int) ta.getDimension(R.styleable.HorizontalProgress_progress_reach_height, mReachHeight);        mReachColor = ta.getColor(R.styleable.HorizontalProgress_progress_reach_color, mReachColor);        mReachHeight = (int) ta.getDimension(R.styleable.HorizontalProgress_progress_unreach_height, mReachHeight);        mReachColor = ta.getColor(R.styleable.HorizontalProgress_progress_unreach_color, mReachColor);        mTextSize = (int) ta.getDimension(R.styleable.HorizontalProgress_text_size, mTextSize);        mTextOffset = (int) ta.getDimension(R.styleable.HorizontalProgress_text_offset, mTextOffset);        mTextColor = ta.getColor(R.styleable.HorizontalProgress_text_color, mTextColor);        ta.recycle();        mPaint.setTextSize(mTextSize);    }    @Override    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        int widthSize = MeasureSpec.getSize(widthMeasureSpec);        int heightSize = measureHeight(heightMeasureSpec);        setMeasuredDimension(widthSize, heightSize);        mRealWidth = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();    }    private int measureHeight(int heightMeasureSpec) {        int result = 0;        int mode = MeasureSpec.getMode(heightMeasureSpec);        int size = MeasureSpec.getSize(heightMeasureSpec);        switch (mode) {            case MeasureSpec.UNSPECIFIED:                break;            case MeasureSpec.AT_MOST:                int textHeight = (int) (mPaint.descent() - mPaint.ascent());                int maxHeight = getPaddingTop() + getPaddingBottom() + Math.max(Math.max(mReachHeight, Math.abs(textHeight)), mUnreachHeight);                result = Math.min(maxHeight, size);                break;            case MeasureSpec.EXACTLY:                result = size;                break;        }        return result;    }    @Override    protected synchronized void onDraw(Canvas canvas) {        canvas.save();        canvas.translate(getPaddingLeft(), getHeight() / 2);        boolean noNeedUnreach = false;        String text = getProgress() + "%";        int textWidth = (int) mPaint.measureText(text);        float radio = getProgress() * 1.0f / getMax();//左边占整个进度的总长度        float progressX = radio * (mRealWidth - textWidth - mTextOffset);//获取当前进度的长度        if (progressX >( mRealWidth - textWidth - mTextOffset)) {            noNeedUnreach = true;            progressX = mRealWidth - textWidth - mTextOffset;        }        float endX = progressX - mTextOffset / 2;        Log.i("TAG", "endX:" + endX);        mPaint.setColor(mReachColor);        mPaint.setStrokeWidth(mReachHeight);        canvas.drawLine(0, 0, progressX, 0, mPaint);        //绘制文本        mPaint.setColor(mTextColor);        int y = (int) (-(mPaint.descent() + mPaint.ascent()) / 2);        canvas.drawText(text, progressX + mTextOffset/2, y, mPaint);//参数1:要绘制的文本,参数2文字的X轴坐标,参数3文字基线处的y轴坐标        //drawable unreach bar        if (!noNeedUnreach) {            float start = progressX + mTextOffset + textWidth;            mPaint.setColor(mUnreachColor);            mPaint.setStrokeWidth(mUnreachHeight);            canvas.drawLine(start, 0, mRealWidth, 0, mPaint);        }        canvas.restore();    }    private int dp2px(int dpValue) {        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, dpValue, getResources().getDisplayMetrics());    }    private int sp2px(int spValue) {        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, spValue, getResources().getDisplayMetrics());    }}

attrs.xml文件:

<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="HorizontalProgress">        <attr name="progress_unreach_color" format="color"/>        <attr name="progress_unreach_height" format="dimension"/>        <attr name="progress_reach_color" format="color"/>        <attr name="progress_reach_height" format="dimension"/>        <attr name="text_color" format="color"/>        <attr name="text_size" format="dimension"/>        <attr name="text_offset" format="dimension"/>        </declare-styleable></resources>

这里写图片描述

0 0
原创粉丝点击