使用



<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>

自定义控件 进度条 ProgressBar-2的更多相关文章

  1. Android 自学之进度条ProgressBar

    进度条(ProgressBar)也是UI界面中的一种非常使用的组件,通常用于向用户显示某个耗时完成的百分比.因此进度条可以动态的显示进度,因此避免长时间地执行某个耗时操作时,让用户感觉程序失去了响应, ...

  2. WPF的进度条progressbar,运行时间elapse time和等待spinner的实现

    今天用.NET 4.5中的TPL的特性做了个小例子,实现了WPF的进度条progressbar,运行时间elapse time和等待spinner. 先上图吧.   这个例子包含4个实现,分别是同步版 ...

  3. android圆形进度条ProgressBar颜色设置

    花样android Progressbar http://www.eoeandroid.com/thread-1081-1-1.html http://www.cnblogs.com/xirihanl ...

  4. Android-SpinKit 进度条 (ProgressBar)

    项目地址: https://github.com/ybq/Android-SpinKit 类别: 进度条 (ProgressBar) 打分: ★★★★★ 更新: 2016-03-28 11:17 大小 ...

  5. 进度条ProgressBar

    在本节中,作者只写出了进度条的各种样式,包括圆形.条形,还有自定义的条形,我想如果能让条形进度条走满后再继续从零开始,于是我加入了一个条件语句.作者的代码中需要学习的是handler在主线程和子线程中 ...

  6. WPF 进度条ProgressBar

    今天研究了一下wpf的进度条ProgressBar 1.传统ProgressBar WPF进度条ProgressBar 这个控件,如果直接写到循环里,会死掉,界面会卡死,不会有进度.需要把进度条放到单 ...

  7. Xamarin XAML语言教程构建进度条ProgressBar

    Xamarin XAML语言教程构建进度条ProgressBar Xamarin XAML语言教程构建进度条ProgressBar,ProgressBar被称为进度条,它类似于没有滑块的滑块控件.进度 ...

  8. Android零基础入门第51节:进度条ProgressBar

    原文:Android零基础入门第51节:进度条ProgressBar 不知不觉这已经是第51期了,在前面50期我们学了Android开发中使用频率非常高的一些UI组件,当然这些组件还不足够完成所有AP ...

  9. (四十一)c#Winform自定义控件-进度条

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  10. (四十二)c#Winform自定义控件-进度条扩展

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

随机推荐

  1. 商务智能(BI)技术

    以下内容仅为兴趣爱好. 商务智能技术是将数据仓库.联机分析处理(OLAP)和数据挖掘等结合起来应用到商业活动中,从不同的数据源收集数据,经过抽取(Extract).转换(Transform)和加载(L ...

  2. Swift - 34 - 闭包的基础语法

    //: Playground - noun: a place where people can play import UIKit // 初始化一个整数数组 var arr = [1, 3, 5, 7 ...

  3. LINQ动态查询类--[DynamicLinqExpressions]

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.L ...

  4. 【转】Debug和Release区别

    VC下Debug和Release区别 最近写代码过程中,发现 Debug 下运行正常,Release 下就会出现问题,百思不得其解,而Release 下又无法进行调试,于是只能采用printf方式逐步 ...

  5. HDU 4614 (13年多校第二场1004)裸线段树

    题意:给你N个花瓶,编号是0  到 N - 1 ,初始状态花瓶是空的,每个花瓶最多插一朵花. 然后有2个操作. 操作1,a b c ,往在a位置后面(包括a)插b朵花,输出插入的首位置和末位置. 操作 ...

  6. 工作流activiti-01个人小结

    最近公司需要开发工作流 使用的是activiti  个人拿着官方的User Guide 摸索了好几天 现做个小结: 对公司的小型OA开发一般用到如下几点: 1.依据客户业务得到业务流程图  可以是xm ...

  7. C语言+ODBC+SQL 操作(向SQL里面添加数据)

    为了节省时间,我就引用上一节的数据库的表和C语言的结构体数组,在结构体数组中添加数据,清空数据库数据. 第一步查询:SQLBindParameter函数的用法. SQLRETURN SQLBindPa ...

  8. oldboy第三天学习

    一.上课体验及感受 额第三天了.身心疲惫.上课一脸懵逼,是最标准的诠释.三个星期的疲惫感,更深了.很难,但是还要继续 写好作业.吸收知识. 二.三级列表 menu = { '北京':{ '海淀':{ ...

  9. Android中开发Service

    Service的开发分为两个步骤:定义Service和配置Service1.定义Service定义一个Service子类继承于Service2.配置Service在AndroidManifest.xm ...

  10. 51单片机I/O口使用经验

    按常规,在51端口(P1.P2.P3)某位用作输入时,必须先向对应的锁存器写入1,使FET截止.一般情况是这样,也有例外.所谓IO口内部与电源相连的上拉电阻而非一常规线性电阻,实质上,该电阻是由两个场 ...