Android常用控件之ProgressBar管钳,水平進(jìn)度條

目錄:android.widget.ProgressBar

通過xml的屬性處理達(dá)到進(jìn)度條顯示效果:

xml布局

<ProgressBar
    android:id="@+id/pb_finish_bar"
    android:layout_width="match_parent"
    android:layout_height="5dp"
    android:layout_margin="20dp"
    android:layout_marginTop="2dp"
    android:indeterminateOnly="false"
    android:max="100"
    android:progress="60"
    android:progressDrawable="@drawable/common_progress_bg" />

common_progress_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!--背景-->
    <item android:id="@android:id/background">
        <shape>
            <gradient
                android:angle="270"
                android:centerY="0.75"
                android:endColor="#f0f0f0"
                android:startColor="#f0f0f0" />
            <!--邊線-->
            <stroke
                android:width="0px"
                android:color="#00A43030" />
            <!--定義背景圓角-->
            <corners
                android:bottomLeftRadius="25dp"
                android:bottomRightRadius="25dp"
                android:topLeftRadius="25dp"
                android:topRightRadius="25dp" />
        </shape>
    </item>
    <!--預(yù)加載緩存進(jìn)度-->
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <gradient
                    android:angle="270"
                    android:centerY="0.75"
                    android:endColor="#00FFFFFF"
                    android:startColor="#00FFFFFF" />
                <corners
                    android:bottomLeftRadius="25dp"
                    android:bottomRightRadius="25dp"
                    android:topLeftRadius="25dp"
                    android:topRightRadius="25dp" />
            </shape>
        </clip>
    </item>
    <!--進(jìn)度-->
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <gradient
                    android:angle="270"
                    android:centerY="0.75"
                    android:endColor="#FF276E9C"
                    android:startColor="#FF276E9C" />
                <corners
                    android:bottomLeftRadius="25dp"
                    android:bottomRightRadius="25dp"
                    android:topLeftRadius="25dp"
                    android:topRightRadius="25dp" />
            </shape>
        </clip>
    </item>
</layer-list>

通過自定義view實(shí)現(xiàn)進(jìn)度條效果:

xml布局

<com.jianshu.HorizontalProgressBar
    android:id="@+id/mpb"
    android:layout_width="match_parent"
    android:layout_height="5dp"
    android:layout_marginTop="50dp"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    app:pb_bg_color="#f0f0f0"
    app:pb_max="100"
    app:pb_padding="0dp"
    app:pb_pb_color="#FF00FF"
    app:pb_progress="60" />

attrs.xml

<!--水平進(jìn)度條-->
<declare-styleable name="HorizontalProgressBar">
    <attr name="pb_padding" format="dimension" />
    <attr name="pb_bg_color" format="color|reference" />
    <attr name="pb_pb_color" format="color|reference" />
    <attr name="pb_max" format="integer" />
    <attr name="pb_progress" format="integer" />
</declare-styleable>

代碼中使用:

HorizontalProgressBar mpb = (HorizontalProgressBar) findViewById(R.id.mpb);
//進(jìn)度值
mpb.setProgress(60);
//必須户矢,設(shè)置最大值
mpb.setMax(100);

自定義view:

public class HorizontalProgressBar extends View {

    private int max;
    private int progress;
    private int bgColor;
    private int progressColor;
    private int padding;

    private Paint progressPaint;
    private Paint bgPaint;

    public HorizontalProgressBar(Context context) {
        super(context);
        init(context, null);
    }

    public HorizontalProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public HorizontalProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        initAttrs(context, attrs);
        initPaths();
    }

    private void initAttrs(Context context, AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.HorizontalProgressBar);
        max = a.getInteger(R.styleable.HorizontalProgressBar_pb_max, 100);
        progress = a.getInteger(R.styleable.HorizontalProgressBar_pb_progress, 0);
        bgColor = a.getColor(R.styleable.HorizontalProgressBar_pb_bg_color, 0xff3F51B5);
        progressColor = a.getColor(R.styleable.HorizontalProgressBar_pb_pb_color, 0xffFF4081);
        padding = a.getDimensionPixelSize(R.styleable.HorizontalProgressBar_pb_padding, 2);
        a.recycle();
    }

    private void initPaths() {
        progressPaint = new Paint();
        progressPaint.setColor(progressColor);
        progressPaint.setStyle(Paint.Style.FILL);
        progressPaint.setAntiAlias(true);

        bgPaint = new Paint();
        bgPaint.setColor(bgColor);
        bgPaint.setStyle(Paint.Style.FILL);
        bgPaint.setAntiAlias(true);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawBackground(canvas);
        drawProgress(canvas);
    }

    private void drawProgress(Canvas canvas) {
        int width = getWidth();
        if (width % 2 != 0) {
            //Fix Me
            width = width-1;
        }
        float percent = 0;
        if (max != 0) {
            percent = progress * 1.0f / max;
        }
        int progressHeight = getHeight() - padding * 2;
        if (progressHeight % 2 != 0) {
            progressHeight = progressHeight -1;
        }
        int progressWidth = width - padding * 2 - progressHeight;
        float dx =  progressWidth * percent;
        //left circle
        canvas.drawCircle(padding+progressHeight/2, padding+progressHeight/2, progressHeight/2, progressPaint);
        //right circle
        canvas.drawCircle(padding+progressHeight/2+dx, padding+progressHeight/2, progressHeight/2, progressPaint);
        //middle line
        RectF midRecf = new RectF(padding+progressHeight/2, padding,padding + progressHeight/2 + dx, padding+ progressHeight);
        canvas.drawRect(midRecf, progressPaint);
    }

    private void drawBackground(Canvas canvas) {
        int bgHeight = getHeight();
        if (bgHeight % 2 != 0) {
            bgHeight = bgHeight-1;
        }
        int width = getWidth();
        if (width % 2 != 0) {
            //Fix Me
            width = width-1;
        }

        //left circle
        canvas.drawCircle(bgHeight/2, bgHeight/2, bgHeight/2, bgPaint);
        //right circle
        canvas.drawCircle(width-bgHeight/2, bgHeight/2, bgHeight/2, bgPaint);
        //middle line
        RectF midRecf = new RectF(bgHeight/2, 0, width-bgHeight/2, bgHeight);
        canvas.drawRect(midRecf, bgPaint);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    public int getMax() {
        return max;
    }

    public void setMax(int max) {
        this.max = max;
        invalidate();
    }

    public int getProgress() {
        return progress;
    }

    public void setProgress(int progress) {
        this.progress = progress;
        invalidate();
    }

    public int getBgColor() {
        return bgColor;
    }

    public void setBgColor(int bgColor) {
        this.bgColor = bgColor;
        invalidate();
    }

    public int getProgressColor() {
        return progressColor;
    }

    public void setProgressColor(int progressColor) {
        this.progressColor = progressColor;
        invalidate();
    }

    public int getPadding() {
        return padding;
    }

    public void setPadding(int padding) {
        this.padding = padding;
        invalidate();
    }

    /**
     * get the percentage value of progress and max.
     * @return percentage value
     */
    public int getPercentage() {
        if (max == 0) {
            return 0;
        }
        return (int) (progress*100.0/max);
    }

}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末定拟,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子逗嫡,更是在濱河造成了極大的恐慌青自,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,183評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件驱证,死亡現(xiàn)場離奇詭異延窜,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)抹锄,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,850評論 3 399
  • 文/潘曉璐 我一進(jìn)店門逆瑞,熙熙樓的掌柜王于貴愁眉苦臉地迎上來荠藤,“玉大人,你說我怎么就攤上這事获高」ぃ” “怎么了?”我有些...
    開封第一講書人閱讀 168,766評論 0 361
  • 文/不壞的土叔 我叫張陵念秧,是天一觀的道長淤井。 經(jīng)常有香客問我,道長摊趾,這世上最難降的妖魔是什么币狠? 我笑而不...
    開封第一講書人閱讀 59,854評論 1 299
  • 正文 為了忘掉前任,我火速辦了婚禮砾层,結(jié)果婚禮上漩绵,老公的妹妹穿的比我還像新娘。我一直安慰自己肛炮,他們只是感情好止吐,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,871評論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著侨糟,像睡著了一般祟印。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上粟害,一...
    開封第一講書人閱讀 52,457評論 1 311
  • 那天,我揣著相機(jī)與錄音颤芬,去河邊找鬼悲幅。 笑死,一個(gè)胖子當(dāng)著我的面吹牛站蝠,可吹牛的內(nèi)容都是我干的汰具。 我是一名探鬼主播,決...
    沈念sama閱讀 40,999評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼菱魔,長吁一口氣:“原來是場噩夢啊……” “哼留荔!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起澜倦,我...
    開封第一講書人閱讀 39,914評論 0 277
  • 序言:老撾萬榮一對情侶失蹤聚蝶,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后藻治,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體碘勉,經(jīng)...
    沈念sama閱讀 46,465評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,543評論 3 342
  • 正文 我和宋清朗相戀三年桩卵,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了验靡。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片倍宾。...
    茶點(diǎn)故事閱讀 40,675評論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖胜嗓,靈堂內(nèi)的尸體忽然破棺而出高职,到底是詐尸還是另有隱情,我是刑警寧澤辞州,帶...
    沈念sama閱讀 36,354評論 5 351
  • 正文 年R本政府宣布怔锌,位于F島的核電站,受9級特大地震影響孙技,放射性物質(zhì)發(fā)生泄漏产禾。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,029評論 3 335
  • 文/蒙蒙 一牵啦、第九天 我趴在偏房一處隱蔽的房頂上張望亚情。 院中可真熱鬧,春花似錦哈雏、人聲如沸楞件。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,514評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽土浸。三九已至,卻和暖如春彭羹,著一層夾襖步出監(jiān)牢的瞬間黄伊,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,616評論 1 274
  • 我被黑心中介騙來泰國打工派殷, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留还最,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 49,091評論 3 378
  • 正文 我出身青樓毡惜,卻偏偏與公主長得像拓轻,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子经伙,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,685評論 2 360

推薦閱讀更多精彩內(nèi)容