使用



<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:bqt="http://schemas.android.com/apk/res/com.bqt.myview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
        <com.bqt.myview.HorizontalProgressBarWithNumber
            android:id="@+id/id_progressbar01"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:progress="10" />
        <com.bqt.myview.HorizontalProgressBarWithNumber
            android:layout_width="300dp"
            android:layout_height="10dp"
            android:layout_margin="5dp"
            android:progress="10"
            bqt:progress_reached_bar_height="10dp"
            bqt:progress_text_color="#ff2903FC"
            bqt:progress_unreached_color="#ffBCB4E8" />
        <com.bqt.myview.HorizontalProgressBarWithNumber
            android:layout_width="200dp"
            android:layout_height="30dp"
            android:layout_margin="5dp"
            android:background="#000"
            android:progress="30"
            bqt:progress_text_color="#ff0"
            bqt:progress_text_size="30dp"
            bqt:progress_unreached_bar_height="10dp"
            bqt:progress_unreached_color="#0f0" />
        <com.bqt.myview.HorizontalProgressBarWithNumber
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="#2000"
            android:progress="10"
            bqt:progress_text_color="#ffF53B03"
            bqt:progress_text_size="15sp"
            bqt:progress_unreached_color="#fff" />
        <com.bqt.myview.RoundProgressBarWidthNumber
            android:id="@+id/id_progress02"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_margin="5dp"
            android:progress="10" />
        <com.bqt.myview.RoundProgressBarWidthNumber
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="#fff"
            android:progress="30"
            bqt:progress_radius="50dp"
            bqt:progress_text_color="#ffF53B03"
            bqt:progress_text_size="20sp" />
        <com.bqt.myview.RoundProgressBarWidthNumber
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="#300f"
            android:progress="50"
            bqt:progress_text_color="#00f"
            bqt:progress_text_size="25sp"
            bqt:progress_unreached_color="#3000" />
        <com.bqt.myview.RoundProgressBarWidthNumber
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="#30f0"
            android:progress="70"
            bqt:progress_radius="15dp"
            bqt:progress_reached_color="#f00"
            bqt:progress_text_color="#000"
            bqt:progress_unreached_color="#0f0" />
    </LinearLayout>
</ScrollView>

水平pb

public class HorizontalProgressBarWithNumber extends ProgressBar {
    private static final int DEFAULT_TEXT_SIZE = 10;
    private static final int DEFAULT_TEXT_COLOR = 0XFFFC00D1;
    private static final int DEFAULT_COLOR_UNREACHED_COLOR = 0xFFd3d6da;
    private static final int DEFAULT_HEIGHT_REACHED_PROGRESS_BAR = 2;
    private static final int DEFAULT_HEIGHT_UNREACHED_PROGRESS_BAR = 2;
    private static final int DEFAULT_SIZE_TEXT_OFFSET = 10;
    protected Paint mPaint = new Paint();
    protected int mTextColor = DEFAULT_TEXT_COLOR;
    protected int mTextSize = sp2px(DEFAULT_TEXT_SIZE);
    protected int mTextOffset = dp2px(DEFAULT_SIZE_TEXT_OFFSET);
    protected int mReachedPBHeight = dp2px(DEFAULT_HEIGHT_REACHED_PROGRESS_BAR);
    protected int mReachedBarColor = DEFAULT_TEXT_COLOR;
    protected int mUnReachedBarColor = DEFAULT_COLOR_UNREACHED_COLOR;
    protected int mUnReachedPBHeight = dp2px(DEFAULT_HEIGHT_UNREACHED_PROGRESS_BAR);
    protected int mRealWidth;
    /**是否显示文字*/
    protected boolean mIfDrawText = true;
    public HorizontalProgressBarWithNumber(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public HorizontalProgressBarWithNumber(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        TypedArray attributes = getContext().obtainStyledAttributes(attrs, R.styleable.HorizontalPB);
        mTextColor = attributes.getColor(R.styleable.HorizontalPB_progress_text_color, DEFAULT_TEXT_COLOR);
        mTextSize = (int) attributes.getDimension(R.styleable.HorizontalPB_progress_text_size, mTextSize);
        mReachedBarColor = attributes.getColor(R.styleable.HorizontalPB_progress_reached_color, mTextColor);
        mUnReachedBarColor = attributes.getColor(R.styleable.HorizontalPB_progress_unreached_color, DEFAULT_COLOR_UNREACHED_COLOR);
        mReachedPBHeight = (int) attributes.getDimension(R.styleable.HorizontalPB_progress_reached_bar_height, mReachedPBHeight);
        mUnReachedPBHeight = (int) attributes.getDimension(R.styleable.HorizontalPB_progress_unreached_bar_height, mUnReachedPBHeight);
        mTextOffset = (int) attributes.getDimension(R.styleable.HorizontalPB_progress_text_offset, mTextOffset);
        mIfDrawText = attributes.getBoolean(R.styleable.HorizontalPB_progress_text_visibility, true);
        attributes.recycle();
        mPaint.setTextSize(mTextSize);
        mPaint.setColor(mTextColor);
    }
    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = MeasureSpec.getSize(widthMeasureSpec);//宽没法使用wrap_content,要不然谁也不知道到底该设为多少
        int height = measureHeight(heightMeasureSpec);
        setMeasuredDimension(width, height);
        mRealWidth = getMeasuredWidth() - getPaddingRight() - getPaddingLeft();
    }
    private int measureHeight(int measureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        } else {
            float textHeight = (mPaint.descent() - mPaint.ascent());
            result = (int) (getPaddingTop() + getPaddingBottom() + Math.max(Math.max(mReachedPBHeight, mUnReachedPBHeight), Math.abs(textHeight)));
            if (specMode == MeasureSpec.AT_MOST) {
                result = Math.min(result, specSize);
            }
        }
        return result;
    }
    @Override
    protected synchronized void onDraw(Canvas canvas) {
        canvas.save();//先保存目前画纸的位置,画完后调用restore方法返回到刚才保存的位置
        canvas.translate(getPaddingLeft(), getHeight() / 2);//移动画笔
        boolean noNeedBg = false;
        float radio = getProgress() * 1.0f / getMax();
        float progressPosX = (int) (mRealWidth * radio);
        String text = getProgress() + "%";
        // mPaint.getTextBounds(text, 0, text.length(), mTextBound);
        float textWidth = mPaint.measureText(text);
        float textHeight = (mPaint.descent() + mPaint.ascent()) / 2;
        if (progressPosX + textWidth > mRealWidth) {
            progressPosX = mRealWidth - textWidth;
            noNeedBg = true;
        }
        // draw reached bar
        float endX = progressPosX - mTextOffset / 2;
        if (endX > 0) {
            mPaint.setColor(mReachedBarColor);
            mPaint.setStrokeWidth(mReachedPBHeight);
            canvas.drawLine(0, 0, endX, 0, mPaint);
        }
        // draw progress bar,measure text bound
        if (mIfDrawText) {
            mPaint.setColor(mTextColor);
            canvas.drawText(text, progressPosX, -textHeight, mPaint);
        }
        // draw unreached bar
        if (!noNeedBg) {
            float start = progressPosX + mTextOffset / 2 + textWidth;
            mPaint.setColor(mUnReachedBarColor);
            mPaint.setStrokeWidth(mUnReachedPBHeight);
            canvas.drawLine(start, 0, mRealWidth, 0, mPaint);
        }
        canvas.restore();//返回到刚才保存的位置
    }
    protected int dp2px(int dpVal) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpVal, getResources().getDisplayMetrics());
    }
    protected int sp2px(int spVal) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, spVal, getResources().getDisplayMetrics());
    }
}

圆形pb

public class RoundProgressBarWidthNumber extends HorizontalProgressBarWithNumber {
    private int mRadius = dp2px(30);
    private int mMaxPaintWidth;
    public RoundProgressBarWidthNumber(Context context) {
        this(context, null);
    }
    public RoundProgressBarWidthNumber(Context context, AttributeSet attrs) {
        super(context, attrs);
        mReachedPBHeight = (int) (mUnReachedPBHeight * 2.5f);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.RoundPB);
        mRadius = (int) ta.getDimension(R.styleable.RoundPB_progress_radius, mRadius);
        ta.recycle();
        mPaint.setStyle(Style.STROKE);
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setStrokeCap(Cap.ROUND);
    }
    @Override
    //这里默认在布局中padding值要么不设置,要么全部设置
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        mMaxPaintWidth = Math.max(mReachedPBHeight, mUnReachedPBHeight);
        int expect = mRadius * 2 + mMaxPaintWidth + getPaddingLeft() + getPaddingRight();
        int width = resolveSize(expect, widthMeasureSpec);
        int height = resolveSize(expect, heightMeasureSpec);
        int realWidth = Math.min(width, height);
        mRadius = (realWidth - getPaddingLeft() - getPaddingRight() - mMaxPaintWidth) / 2;
        setMeasuredDimension(realWidth, realWidth);
    }
    @Override
    protected synchronized void onDraw(Canvas canvas) {
        String text = getProgress() + "%";
        float textWidth = mPaint.measureText(text);
        float textHeight = (mPaint.descent() + mPaint.ascent()) / 2;
        canvas.save();
        canvas.translate(getPaddingLeft() + mMaxPaintWidth / 2, getPaddingTop() + mMaxPaintWidth / 2);
        mPaint.setStyle(Style.STROKE);
        // draw unreaded bar
        mPaint.setColor(mUnReachedBarColor);
        mPaint.setStrokeWidth(mUnReachedPBHeight);
        canvas.drawCircle(mRadius, mRadius, mRadius, mPaint);
        // draw reached bar
        mPaint.setColor(mReachedBarColor);
        mPaint.setStrokeWidth(mReachedPBHeight);
        float sweepAngle = getProgress() * 1.0f / getMax() * 360;
        canvas.drawArc(new RectF(0, 0, mRadius * 2, mRadius * 2), 0, sweepAngle, false, mPaint);
        // draw text
        mPaint.setStyle(Style.FILL);
        canvas.drawText(text, mRadius - textWidth / 2, mRadius - textHeight, mPaint);
        canvas.restore();
    }
}

自定义属性

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="HorizontalPB">
        <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="boolean">
        </attr>
    </declare-styleable>
    <declare-styleable name="RoundPB">
        <attr name="progress_radius" format="dimension" />
    </declare-styleable>
</resources>

最新文章

  1. 分布式系统设计权衡之CAP
  2. HTMl5结构元素:article
  3. mvc 4 razor语法讲解和使用
  4. KMPlayer
  5. UCOS-信号量(学习笔记)
  6. 在ump系统的那半个多月-jqGrid
  7. [NOIP1998]最大数
  8. Ubuntu安装Microsoft Windows Fonts微软字体库
  9. poj 3764 The xor-longest Path(字典树)
  10. window.open打开新页面居中
  11. .NET反射应用
  12. 贝塞尔曲线控件 for .NET (EN)
  13. 学习前端笔记1(HTML)
  14. 使用 shell 在多服务器上批量操作
  15. day 7-2 multiprocessing开启多进程
  16. 自学Linux Shell1.1-Linux初识
  17. js文件改变之后浏览器缓存问题怎么解决?
  18. Car 加油
  19. python process,queue
  20. 第四次ScrumMeeting博客

热门文章

  1. 武汉科技大学ACM :1005: 一二三
  2. js中的apply call 操作小结(参考自网络)
  3. 1.3.1 switch 语句中的 String
  4. dede仿站笔记
  5. 《python基础教程》笔记之 其它语句1
  6. 使用minidwep-gtk-PJ-wifi教程中文版
  7. ALTIUM 10 过孔设置开窗、不开窗
  8. 玩转Google开源C++单元测试框架Google Test系列(转载)
  9. 使用activeMQ实现jms
  10. rsyslog 直接kill进程,在重新启动会全部发送日志