Android開發(fā)一款優(yōu)美的加載控件

最近抽了點時間學習了Android自定義view,然后花了大概幾天時間搞了個自己的一個款開源纳决。讓我們先來看看效果:

總體效果

本開源主要實現(xiàn)了一款精美腿准、優(yōu)雅的加載控件。她目前有兩種類型:弧形加載(CircleProgressView)和水平加載(HorizontalProgressView)悍缠,同時,你可以為她設(shè)置顏色漸變效果耐量。

傳送門地址:https://github.com/Moosphan/Material-ProgressView

歡迎大家獻上寶貴的star和issue飞蚓,我將繼續(xù)努力完善它,也歡迎大家和我一起來優(yōu)化它的功能廊蜒,剛學習自定義view玷坠,有很多處理不當?shù)牡胤较M赋鰜恚x謝劲藐,后續(xù)將提升它的定制性和動畫等效果八堡。

再來看看細節(jié)的效果圖:

細節(jié)效果

下面我們來看看該如何使用這款控件。

快速開始

  • Gradle:

    build.gradle:

    compile 'com.moos:Material-ProgressView:1.0.4'
    

    ?

  • Maven:

    <dependency>
      <groupId>com.moos</groupId>
      <artifactId>Material-ProgressView</artifactId>
      <version>1.0.4</version>
      <type>pom</type>
    </dependency>
    
  • 通過xml文件來設(shè)置:

    <com.moos.library.CircleProgressView
            android:id="@+id/progressView_circle"
            android:layout_width="240dp"
            android:layout_height="240dp"
            android:layout_marginTop="12dp"
            app:start_progress="0"
            app:end_progress="60"
            app:start_color="@color/purple_end"
            app:end_color="@color/purple_start"
            app:circleBroken="true"
            app:isTracked="true"
            app:track_width="26dp"/>
    
    <com.moos.library.HorizontalProgressView
          android:id="@+id/progressView_horizontal"
          android:layout_width="320dp"
          android:layout_height="100dp"
          android:layout_marginBottom="40dp"
          android:layout_marginTop="36dp"
          app:start_color="@color/red_start"
          app:end_color="@color/red_end"
          app:track_width="12dp"
          app:end_progress="60"
          app:progressTextColor="#696969"
          app:corner_radius="12dp"
          app:isTracked="true"
          app:trackColor="#f4f4f4"/>
    

    ?

  • 通過java代碼動態(tài)初始化屬性:

    
            //CircleProgressView
            CircleProgressView circleProgressView = (CircleProgressView) view.findViewById(R.id.progressView_circle);
            circleProgressView.setStartProgress(0);
            circleProgressView.setEndProgress(80);
            circleProgressView.setStartColor(Color.parseColor("#FF8F5D"));
            circleProgressView.setEndColor(Color.parseColor("#F54EA2"));
            circleProgressView.setCircleBroken(true);
            circleProgressView.setTrackWidth(20);
            circleProgressView.setProgressDuration(2000);
            circleProgressView.setTrackEnabled(true);
            circleProgressView.setFillEnabled(false);
            circleProgressView.startProgressAnimation();
           
            //HorizontalProgressView
            HorizontalProgressView circleProgressView = (HorizontalProgressView)              view.findViewById(R.id.progressView_horizontal);
            horizontalProgressView.setStartProgress(0);
            horizontalProgressView.setEndProgress(80);
            horizontalProgressView.setStartColor(Color.parseColor("#FF8F5D"));
            horizontalProgressView.setEndColor(Color.parseColor("#F54EA2"));
            horizontalProgressView.setTrackWidth(30);
            horizontalProgressView.setProgressDuration(2000);
            horizontalProgressView.setTrackEnabled(true);
            horizontalProgressView.setProgressCornerRadius(20);
            horizontalProgressView.setProgressTextPaddingBottom(12);
            horizontalProgressView.startProgressAnimation();
    
    
  • 此外聘芜,如果你想要讓水平加載控件上方的百分比數(shù)值跟隨其運動兄渺,可以這樣設(shè)置:

    1. 通過xml設(shè)置:
            app:textMovedEnable="true"
    
    1. 通過java代碼:
            horizontalProgressView.setProgressTextMoved(true);
    

    然后你會看到如下效果:
    ?

    文字跟隨
  • 如果想要根據(jù)給定值來動態(tài)設(shè)置加載控件的進度值(如加載進度和上傳進度等),可以通過方法setProgress汰现,如:

    
    ......
          //上傳或者下載的回調(diào)
          @Override
          public void onDownloading(float progress) {
              // 由于是動態(tài)設(shè)置進度值挂谍,無需再調(diào)用`startProgressAnimation`方法來啟動進度的刷新了
              horizontalProgressView.setProgress(progress);
          }
    
    ......
    

相關(guān)文檔

  • 控件屬性
  1. 共有屬性:

    Attribute Description
    start_progress 起始進度
    end_progress 終止進度
    start_color 漸變效果的起始顏色
    end_color 漸變效果的終止顏色
    isTracked 是否顯示軌跡背景
    track_width 進度條的寬度(邊界寬度)
    trackColor 軌跡背景的顏色
    progressTextVisibility 是否顯示進度值文本
    progressTextColor 進度值的顏色
    progressTextSize 進度值的文本字體大小
    progressDuration 動畫時長
    animateType 動畫類型(可以參考屬性動畫的TimeInterpolator)
  2. CircleProgressView的特有屬性:

    Attribute Description
    isFilled 是否內(nèi)部填充
    circleBroken 是選擇圓形還是弧形進度條
  3. HorizontalProgressView的特有屬性:

    Attribute Description
    corner_radius 圓角半徑
    text_padding_bottom 文字距離view的padding
    textMovedEnable 設(shè)置進度值是否跟隨控件動畫移動
  • 回調(diào)

        /**
         * 你可以利用該回調(diào)來定制自己的progress值的UI展示
         */
    
        circleProgressView.setProgressViewUpdateListener(new CircleProgressView.CircleProgressUpdateListener() {
        
        @Override
        public void onCircleProgressStart(View view) {
    
        }
    
        @Override
        public void onCircleProgressUpdate(View view,float progress) {
     
            int progressInt = (int) progress;
            textView.setText(String.valueOf(progress)+"%");
        }
    
        @Override
        public void onCircleProgressFinished(View view) {
    
        }
    });
    
    horizontalProgressView.setProgressViewUpdateListener(new HorizontalProgressView.HorizontalProgressUpdateListener() {
        
        @Override
        public void onHorizontalProgressStart(View view) {
    
        }
    
        @Override
        public void onHorizontalProgressUpdate(View view,float progress) {
         
            int progressInt = (int) progress;
            textView.setText(String.valueOf(progress)+"%");
        }
    
        @Override
        public void onHorizontalProgressFinished(View view) {
    
        }
    });
    
  • Methods

    //共有的方法
    void setAnimateType(@AnimateType int type);
    void setStartProgress(float startProgress);
    void setEndProgress(float endProgress);
    void setStartColor(@ColorInt int startColor);
    void setEndColor(@ColorInt int endColor);
    void setTrackWidth(int width);
    void setProgressTextSize(int size);
    void setProgressTextColor(@ColorInt int textColor);
    void setProgressDuration(int duration);
    void setTrackEnabled(boolean trackAble);
    void setTrackColor(@ColorInt int color);
    void setProgressTextVisibility(boolean visibility);
    void startProgressAnimation();
    void stopProgressAnimation();
    
    //CircleProgressView的特有方法
    void setCircleBroken(boolean isBroken);
    void setFillEnabled(boolean fillEnabled);
    
    //HorizontalProgressView的特有方法
    void setProgressCornerRadius(int radius);
    void setProgressTextPaddingBottom(int offset);
    
    

demo下載地址

ProgressView-sample.apk

更新記錄

  • V1.0.1(2018-03-16):為加載控件提供進度回調(diào)
  • V1.0.2(2018-03-22):為水平進度條提供進度值的跟隨動畫
  • V1.0.3(2018-04-12):增加弧形加載控件的刻度效果
  • V1.0.4(2018-05-03):支持動態(tài)設(shè)置加載控件的進度值(下載/上傳進度等)

特別感謝

HenCoder

VisualCC

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市瞎饲,隨后出現(xiàn)的幾起案子口叙,更是在濱河造成了極大的恐慌,老刑警劉巖嗅战,帶你破解...
    沈念sama閱讀 221,198評論 6 514
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件妄田,死亡現(xiàn)場離奇詭異,居然都是意外死亡驮捍,警方通過查閱死者的電腦和手機疟呐,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,334評論 3 398
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來东且,“玉大人启具,你說我怎么就攤上這事∩河荆” “怎么了鲁冯?”我有些...
    開封第一講書人閱讀 167,643評論 0 360
  • 文/不壞的土叔 我叫張陵拷沸,是天一觀的道長。 經(jīng)常有香客問我薯演,道長撞芍,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,495評論 1 296
  • 正文 為了忘掉前任涣仿,我火速辦了婚禮,結(jié)果婚禮上示惊,老公的妹妹穿的比我還像新娘好港。我一直安慰自己,他們只是感情好米罚,可當我...
    茶點故事閱讀 68,502評論 6 397
  • 文/花漫 我一把揭開白布钧汹。 她就那樣靜靜地躺著,像睡著了一般录择。 火紅的嫁衣襯著肌膚如雪拔莱。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,156評論 1 308
  • 那天隘竭,我揣著相機與錄音塘秦,去河邊找鬼。 笑死动看,一個胖子當著我的面吹牛尊剔,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播菱皆,決...
    沈念sama閱讀 40,743評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼须误,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了仇轻?” 一聲冷哼從身側(cè)響起京痢,我...
    開封第一講書人閱讀 39,659評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎篷店,沒想到半個月后祭椰,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,200評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡疲陕,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,282評論 3 340
  • 正文 我和宋清朗相戀三年吭产,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片鸭轮。...
    茶點故事閱讀 40,424評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡臣淤,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出窃爷,到底是詐尸還是另有隱情邑蒋,我是刑警寧澤姓蜂,帶...
    沈念sama閱讀 36,107評論 5 349
  • 正文 年R本政府宣布,位于F島的核電站医吊,受9級特大地震影響钱慢,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜卿堂,卻給世界環(huán)境...
    茶點故事閱讀 41,789評論 3 333
  • 文/蒙蒙 一束莫、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧草描,春花似錦览绿、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,264評論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至逛绵,卻和暖如春怀各,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背术浪。 一陣腳步聲響...
    開封第一講書人閱讀 33,390評論 1 271
  • 我被黑心中介騙來泰國打工瓢对, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人胰苏。 一個月前我還...
    沈念sama閱讀 48,798評論 3 376
  • 正文 我出身青樓沥曹,卻偏偏與公主長得像,于是被迫代替她去往敵國和親碟联。 傳聞我的和親對象是個殘疾皇子妓美,可洞房花燭夜當晚...
    茶點故事閱讀 45,435評論 2 359

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