YCProgress自定義百分比進度條

目錄介紹

  • 1.本庫優點亮點
  • 2.使用介紹
    • 2.1 圓環百分比進度條
    • 2.2 直線百分比進度條
    • 2.3 仿殺毒類型百分比進度條
  • 3.注意要點
  • 4.效果展現
  • 5.其餘介紹

1.本庫優點亮點

  • 圓環百分比進度條
    • 簡便且小巧,支持設置多種屬性。能夠設置內圓和外圓的顏色,設置圓環的邊緣寬度。
    • 支持設置倒計時總時間,能夠調用start開始倒計時,也能夠調用stop暫停倒計時,也能夠自定義設置進度
  • 仿殺毒類型百分比進度條
    • 支持設置多種類型,好比設置百分比+單位類型,或者設置空類型【也就是不顯示中間百分比】
    • 支持設置進度條的顏色,未更新的進度條顏色;設置百分比文字大小,顏色;支持設置單位等多種屬性
    • 支持容許多線程訪問,對於設置setProgress,添加synchronized關鍵字修飾。設置進度progress,若是小於0或者大於100,則拋異常。避免開發者使用形成其餘問題。
  • 針對進度條,對於設置color顏色的方法,增長了註解@ColorInt,限制開發者調用color資源
  • 使用註解代替了枚舉,針對設置枚舉的方法,使用註解限制開發者調用時傳入的類型。具體可見代碼案例!
  • 註釋十分詳細,做爲開源的lib庫,我以爲要讓使用者一目瞭然。方便調用同時,知道每一個方法的做用。
  • 代碼量少,若是想學習並深刻自定義控件,能夠從簡單開始。這個項目就很符合!
  • 項目地址:https://github.com/yangchong211/YCProgress

2.使用介紹

  • 集成庫:compile 'cn.yc:YCProgressLib:1.2.6'

2.1 圓環百分比進度條

  • 在佈局中android

    //也能夠設置佈局中的attr屬性
    <com.ns.yc.ycprogresslib.CircleProgressbar
        android:id="@+id/pb_1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="進度條" />
  • 使用方法git

    //設置類型
    pb_1.setProgressType(ProgressBarUtils.ProgressType.COUNT);
    //設置圓形的填充顏色
    pb_1.setInCircleColor(getResources().getColor(R.color.redTab));
    //設置外部輪廓的顏色
    pb_1.setOutLineColor(getResources().getColor(R.color.grayLine));
    //設置進度監聽
    pb_1.setCountdownProgressListener(1, progressListener);
    //設置外部輪廓的顏色
    pb_1.setOutLineWidth(2);
    //設置進度條線的寬度
    pb_1.setProgressLineWidth(5);
    //設置進度
    pb_1.setProgress(60);
    //設置倒計時總時間
    pb_1.setTimeMillis(3000);
    //設置進度條顏色
    pb_1.setProgressColor(getResources().getColor(R.color.colorPrimary));
    
    //開始
    pb_1.start();
    //暫停
    pb_1.stop();
    //從新開始
    pb_1.reStart();

2.2 直線百分比進度條

  • 在佈局中github

    <com.ns.yc.ycprogresslib.NumberProgressbar
        android:id="@+id/bar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    
    <com.ns.yc.ycprogresslib.NumberProgressbar
        android:id="@+id/bar2"
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:progress_max="100"
        app:progress_reached_bar_height="3dp"
        app:progress_unreached_bar_height="3dp"
        app:progress_reached_color="@color/colorPrimary"
        app:progress_unreached_color="@color/gray3"
        app:progress_text_size="14sp"
        app:progress_text_color="@color/colorAccent"
        app:progress_text_visibility="visible"/>
  • 代碼調用面試

    bar1 = (NumberProgressbar) findViewById(R.id.bar1);
    //設置倒計時總時間
    bar1.setTimeMillis(10000);
    //設置最大進度條的值
    bar1.setMax(100);
    //設置進度條文本的顏色
    bar1.setProgressTextColor(this.getResources().getColor(R.color.colorAccent));
    //設置進度條文本的大小
    bar1.setProgressTextSize(ProgressBarUtils.sp2px(this,14));
    //設置百分比文字內容是否可見
    bar1.setNumberTextVisibility(ProgressBarUtils.NumberTextVisibility.Visible);
    //設置百分比進度條的高度
    bar1.setReachedBarHeight(10);
    //設置未更新百分比進度條的高度
    bar1.setUnreachedBarHeight(10);
    //設置百分比進度條的顏色
    bar1.setReachedBarColor(this.getResources().getColor(R.color.redTab));
    //設置未更新百分比進度條的顏色
    bar1.setUnreachedBarColor(this.getResources().getColor(R.color.blackText2));
    //設置百分比進度條的監聽
    bar1.setOnProgressBarListener(new OnNumberProgressListener() {
        @Override
        public void onProgressChange(int current, int max) {
    
        }
    });
    
    //開始
    bar1.start();
    //暫停
    bar1.stop();

2.3 仿殺毒類型百分比進度條

  • 佈局代碼
    <com.ns.yc.ycprogresslib.RingProgressBar
        android:id="@+id/bar_percent"
        android:layout_width="100dp"
        android:layout_height="100dp"/>
  • 如何調用
    bar_percent = (RingProgressBar) findViewById(R.id.bar_percent);
    //設置進度
    bar_percent.setProgress(0);
    //設置更新進度條顏色
    bar_percent.setDotColor(this.getResources().getColor(R.color.colorAccent));
    //設置未更新部分的進度條顏色
    bar_percent.setDotBgColor(this.getResources().getColor(R.color.blackText));
    //設置百分比文字顏色
    bar_percent.setPercentTextColor(this.getResources().getColor(R.color.blackText1));
    //設置百分比文字大小
    bar_percent.setPercentTextSize(ProgressBarUtils.dp2px(this,16.0f));
    //設置展現的類型
    bar_percent.setShowMode(ProgressBarUtils.RingShowMode.SHOW_MODE_PERCENT);
    //設置單位的文字內容
    bar_percent.setUnitText("%");
    //設置單位的文字大小
    bar_percent.setUnitTextSize(ProgressBarUtils.dp2px(this,16.0f));
    //設置單位的文字顏色
    bar_percent.setUnitTextColor(this.getResources().getColor(R.color.blackText1));
  • 能夠設置多種類型
    • 第一種:百分比+單位【支持本身設置單位,好比設置%,或者設置毫秒s等】
    • 第二種:空顯示模式【也就是不顯示中間的部分】

3.注意要點

  • 3.1 不管是圓環進度條仍是直線進度條,在調用setProgress設置進度時,增長了驗證進度的功能。由於若是設置值超過100或者小於0,該方法就起做用呢!
    /**
     * 驗證進度。
     *
     * @param progress      你要驗證的進度值。
     * @return              返回真正的進度值。
     */
    private int validateProgress(int progress) {
        if (progress > 100){
            progress = 100;
        } else if (progress < 0){
            progress = 0;
        }
        return progress;
    }
  • 3.2 針對CircleProgressbar和NumberProgressbar自定義控件,若是調用start方法開始循環執行setProgress,程序意外銷燬,則注意:
    /**
     * 當自定義控件銷燬時,則調用該方法
     */
    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        stop();
    }
  • 項目地址:https://github.com/yangchong211/YCProgress

4.效果展現

  • image
    image
    image
    image
    image
    image

5.其餘介紹

版本更新說明

  • v1.0.0 更新於2016/2/10 做用於投資界下載更新進度條,學習自定義控件
  • v1.1.1 更新於2016/8/12 針對圓環進度條,添加自定義attr屬性
  • v1.1.2 更新於2017/3/10 針對圓環進度條添加設置倒計時總時間,start和stop方法
  • v1.1.3 更新於2017/5/27 針對設置進度的方法,增長校驗,不能小於0或者大於100
  • v1.2.5 更新於2018年8月24日 添加了直線百分比進度條,針對部分方法添加註解
  • v1.2.6 更新於2018年11月30日 添加了詳細的註釋
  • v1.2.7 更新於2018/12/3 更新targetSdkVersion版本是27
  • 關於直線百分比進度條參考了代碼家NumberProgressBar項目:https://github.com/daimajia/NumberProgressBar

關於博客彙總連接

其餘推薦

  • 博客筆記大彙總【15年10月到至今】,包括Java基礎及深刻知識點,Android技術博客,Python學習筆記等等,還包括平時開發中遇到的bug彙總,固然也在工做之餘收集了大量的面試題,長期更新維護而且修正,持續完善……開源的文件是markdown格式的!同時也開源了生活博客,從12年起,積累共計47篇[近20萬字],轉載請註明出處,謝謝!
  • 連接地址:https://github.com/yangchong211/YCBlogs
  • 若是以爲好,能夠star一下,謝謝!固然也歡迎提出建議,萬事起於忽微,量變引發質變!

關於LICENSE

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
相關文章
相關標籤/搜索