Android踩坑日记:自定义水平和圆形ProgressBar样式
自定義水平和圓形ProgressBar樣式
1.自定義水平ProgressBar樣式
- ProgressBar分為兩種,我們能明確看到進度,不確定的就是不清楚、不確定一個操作需要多長時間來完成,這個時候就需要用的不確定的ProgressBar了。
- ProgressBar(Horizontal 才有,無進度的沒有)有兩個進度,一個是android:progress,另一個是android:secondaryProgress。后者主要是為緩存需要所涉及的,比如在看網絡視頻時候都會有一個緩存的進度條以及還要一個播放的進度,在這里緩存的進度就可以是android:secondaryProgress,而播放進度就是android:progress,有了secondProgress,可以很方便定制ProgressBar。
1.ProgressBar的樣式設定其實有兩種方式,在API文檔中說明的方式如下:
- Widget.ProgressBar.Horizontal
- Widget.ProgressBar.Small
- Widget.ProgressBar.Large
- Widget.ProgressBar.Inverse
- Widget.ProgressBar.Small.Inverse
- Widget.ProgressBar.Large.Inverse
使用的時候可以這樣:style=”@android:style/Widget.ProgressBar.Horizontal”
另外還有一種方式就是使用系統的attr,下面的方式是系統的style:
- style=”?android:attr/progressBarStyle”
- style=”?android:attr/progressBarStyleHorizontal”
- style=”?android:attr/progressBarStyleInverse”
- style=”?android:attr/progressBarStyleLarge”
- style=”?android:attr/progressBarStyleLargeInverse”
- style=”?android:attr/progressBarStyleSmall”
- style=”?android:attr/progressBarStyleSmallInverse”
- style=”?android:attr/progressBarStyleSmallTitle”
比如:
<ProgressBar android:id="@+id/progressBar"style="@android:style/Widget.ProgressBar.Horizontal"android:layout_width="match_parent"android:layout_height="wrap_content" /> ProgressBarandroid:id="@+id/progressBar"style="?android:attr/progressBarStyleHorizontal"android:layout_width="match_parent"android:layout_height="wrap_content" />我們去看看style=”@android:style/Widget.ProgressBar.Horizontal” 的源碼
<style name="Widget.ProgressBar.Horizontal"><item name="indeterminateOnly">false</item><!-- 進度條的背景,progress ,secondaryProgress 的顏色--><item name="progressDrawable">@drawable/progress_horizontal</item><item name="indeterminateDrawable">@drawable/progress_indeterminate_horizontal</item><item name="minHeight">20dip</item><item name="maxHeight">20dip</item><item name="mirrorForRtl">true</item></style>下面看@android:drawable/progress_horizontal的源碼
<?xml version="1.0" encoding="utf-8"?> <!-- Copyright (C) 2008 The Android Open Source ProjectLicensed 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 athttp://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, softwaredistributed 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 andlimitations under the License. --><layer-list xmlns:android="http://schemas.android.com/apk/res/android"><!--背景色--><item android:id="@android:id/background"><shape><corners android:radius="5dip" /><gradient android:startColor="#ff9d9e9d"android:centerColor="#ff5a5d5a"android:centerY="0.75"android:endColor="#ff747674"android:angle="270"/></shape></item><!--第二進度條顏色--><item android:id="@android:id/secondaryProgress"><clip><shape><corners android:radius="5dip" /><gradient android:startColor="#80ffd300"android:centerColor="#80ffb600"android:centerY="0.75"android:endColor="#a0ffcb00"android:angle="270"/></shape></clip></item><!--第一進度條顏色--><item android:id="@android:id/progress"><clip><shape><corners android:radius="5dip" /><gradient android:startColor="#ffffd300"android:centerColor="#ffffb600"android:centerY="0.75"android:endColor="#ffffcb00"android:angle="270"/></shape></clip></item> </layer-list>三個條目,對應了background,progress,secondaryProgress。所以只需要修改對應項就可以了,如下在drawable文件中創建一個 progressbar_horizontal_custom.xml
所以其實我們要修改樣式的話只需要修改android:progressDrawable”對應的資源就可以
video_view_bottom_progressbar_background.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"android:height="2dp"android:gravity="center"><shape><size android:height="2dp"/><corners android:radius="1dp" /><solid android:color="#dcdcdc"/></shape></item><item android:id="@android:id/secondaryProgress"android:height="2dp"android:gravity="center"><clip ><shape><size android:height="2dp"/><corners android:radius="5dp" /><solid android:color="@android:color/darker_gray"/><!--darker_gray--></shape></clip></item><item android:id="@android:id/progress"android:height="2dp"android:gravity="center"><clip ><shape><size android:height="2dp"/><corners android:radius="5dp" /><solid android:color="#FF455B"/></shape></clip></item> </layer-list>在布局文件xml中使用:
<ProgressBarandroid:id="@+id/pb_video_bottom_progress"style="@android:style/Widget.ProgressBar.Horizontal"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_gravity="bottom"android:max="100"android:maxHeight="2dp"android:minHeight="2dp"android:progressDrawable="@drawable/video_view_bottom_progressbar_background"android:visibility="visible" />總結:
1. 使用style=”@android:style/Widget.ProgressBar.Horizontal”
2. 重新自自定android:progressDrawable
2.自定義圓形ProgressBar樣式
比如
我們先看Widget.Progress的源碼:
<style name="Widget.ProgressBar.Small"><!--圓形背景--><item name="indeterminateDrawable">@drawable/progress_small_white</item><item name="minWidth">16dip</item><item name="maxWidth">16dip</item><item name="minHeight">16dip</item><item name="maxHeight">16dip</item></style>@drawable/progress_small_white的源碼:
<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"android:drawable="@drawable/spinner_white_48"android:pivotX="50%"android:pivotY="50%"android:framesCount="12"android:frameDuration="100" />發現其實就是一個不斷重復的旋轉動畫是spinner_white_48的圖片,所以我們只需要自定義name=”indeterminateDrawable”的屬性,自定義自己的drawable使用animated-rotate 標簽即可
總結:
1.修改indeterminateDrawable屬性,編寫animated-rotate 標簽的drawable,替換系統的
總結
以上是生活随笔為你收集整理的Android踩坑日记:自定义水平和圆形ProgressBar样式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android踩坑日记:Okhttp设置
- 下一篇: Android踩坑日记:Recycler