android 自定義進度條 seekbar

水平進度條  自定義實現原理


要自定義 就要知道原生如何實現 進入ProgressBar裏面 

發現  * <pre>
 * &lt;ProgressBar
 *     style="@android:style/Widget.ProgressBar.Horizontal"
 *     ... /&gt;</pre>

很多這種註釋 在開始的位置  

同時 你可以分析 ProgressBar的 水平 圓形 等等是根據style來展示不同的 所以我們基本可以確定 style裏面有我們想要的 

進去 android_sdk\platforms\android-18\data\res\values 裏面的styles.xml 查找progressbar 

用搜索就行 

可以看到這些




既然 這樣 我們就可以自己定義一個自己的在 values裏面的styles.xml加上自己的樣式

這裏把這段 複製過去 改成如下



 <!-- 自定義水平進度條 -->
   

[html] view plaincopy
  1. <style name="my_pb_hor" parent="@android:style/Widget.ProgressBar.Horizontal">  
  2.       <item name="android:progressDrawable">@drawable/progress_hor_slef</item>  
  3.   </style>  


這個是必須要加的 不然 在佈局文件引用 系統不認識 這個就是繼承  承認這個是 水平進度條可以用

parent="@android:style/Widget.ProgressBar.Horizontal"



這個是我們想要的  再看原生的
[html] view plaincopy
  1. android:progressDrawable  

在原生的drawable文件夾裏面 找到 複製出來一份 用everything 很快能找到


然後再 自己的工程的 drawable裏面新建一個自己的(一開始複製系統的 然後自己修改) 這個就是水平進度條的 顯示效果


系統如下

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <layer-list xmlns:android="http://schemas.android.com/apk/res/android">  
  4.       
  5.     <item android:id="@android:id/background">  
  6.         <shape>  
  7.             <corners android:radius="5dip" />  
  8.             <gradient  
  9.                     android:startColor="#ff9d9e9d"  
  10.                     android:centerColor="#ff5a5d5a"  
  11.                     android:centerY="0.75"  
  12.                     android:endColor="#ff747674"  
  13.                     android:angle="270"  
  14.             />  
  15.         </shape>  
  16.     </item>  
  17.       
  18.     <item android:id="@android:id/secondaryProgress">  
  19.         <clip>  
  20.             <shape>  
  21.                 <corners android:radius="5dip" />  
  22.                 <gradient  
  23.                         android:startColor="#80ffd300"  
  24.                         android:centerColor="#80ffb600"  
  25.                         android:centerY="0.75"  
  26.                         android:endColor="#a0ffcb00"  
  27.                         android:angle="270"  
  28.                 />  
  29.             </shape>  
  30.         </clip>  
  31.     </item>  
  32.       
  33.     <item android:id="@android:id/progress">  
  34.         <clip>  
  35.             <shape>  
  36.                 <corners android:radius="5dip" />  
  37.                 <gradient  
  38.                         android:startColor="#ffffd300"  
  39.                         android:centerColor="#ffffb600"  
  40.                         android:centerY="0.75"  
  41.                         android:endColor="#ffffcb00"  
  42.                         android:angle="270"  
  43.                 />  
  44.             </shape>  
  45.         </clip>  
  46.     </item>  
  47.       
  48. </layer-list>  

改成自己的

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <layer-list xmlns:android="http://schemas.android.com/apk/res/android">  
  4.       
  5.     <item  
  6.         android:id="@android:id/background"  
  7.         android:drawable="@drawable/black"/>  
  8.     <item  
  9.         android:id="@android:id/progress"  
  10.         android:drawable="@drawable/green"/>  
  11.       
  12.         <item android:id="@android:id/secondaryProgress">  
  13.         <clip>  
  14.             <shape>  
  15.                 <corners android:radius="5dip" />  
  16.                 <gradient  
  17.                         android:startColor="#80ffd300"  
  18.                         android:centerColor="#80ffb600"  
  19.                         android:centerY="0.75"  
  20.                         android:endColor="#a0ffcb00"  
  21.                         android:angle="270"  
  22.                 />  
  23.             </shape>  
  24.         </clip>  
  25.     </item>  
  26.       
  27. </layer-list>  



然後這樣基本 自定一個樣式 用於進度條水平的

在主佈局文件使用 如下  只是改變style而已 

現在看看效果

[html] view plaincopy
  1. <ProgressBar  
  2.                 android:id="@+id/progressBar1"  
  3.                 style="@style/my_pb_hor"  
  4.                 android:layout_width="fill_parent"  
  5.                 android:layout_height="wrap_content"  
  6.                 android:layout_marginLeft="10dp"  
  7.                 android:layout_marginRight="10dp"   
  8.                 android:progress="20"  
  9.                 android:secondaryProgress="50"  
  10.                 />  


效果如下




自定義SeekBar  一樣的原理 步驟實現 再來一次

1:找到原生的

[html] view plaincopy
  1. <style name="Widget.SeekBar">  
  2.         <item name="android:indeterminateOnly">false</item>  
  3.         <item name="android:progressDrawable">@android:drawable/progress_horizontal</item>  
  4.         <item name="android:indeterminateDrawable">@android:drawable/progress_horizontal</item>  
  5.         <item name="android:minHeight">20dip</item>  
  6.         <item name="android:maxHeight">20dip</item>  
  7.         <item name="android:thumb">@android:drawable/seek_thumb</item>  
  8.         <item name="android:thumbOffset">8dip</item>  
  9.         <item name="android:focusable">true</item>  
  10.         <item name="android:mirrorForRtl">true</item>  
  11.     </style>  

2:在values裏面的styles裏面改成自己的 

parent必須的 系統識別標誌

[html] view plaincopy
  1. <style name="my_seekbar_self" parent="@android:style/Widget.SeekBar">  
  2.         <item name="android:progressDrawable">@drawable/progress_horizontal_self</item>  
  3.          <item name="android:thumb">@drawable/seek_thumb_self</item>  
  4.         <item name="android:thumbOffset">8dip</item>  
  5.         <item name="android:focusable">true</item>  
  6.     </style>  

3:在drawable裏面模擬系統的創建
[html] view plaincopy
  1. android:progressDrawable    <pre name="code" class="html">android:thumb  這是滑動塊  




progressDrawable


[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <layer-list xmlns:android="http://schemas.android.com/apk/res/android">  
  4.     <item  
  5.         android:id="@android:id/background"  
  6.         android:drawable="@drawable/black"/>  
  7.     <item  
  8.         android:id="@android:id/progress"  
  9.         android:drawable="@drawable/green"/>  
  10.       
  11.     <item android:id="@android:id/secondaryProgress">  
  12.         <clip>  
  13.             <shape>  
  14.                 <corners android:radius="5dip" />  
  15.                 <gradient  
  16.                         android:startColor="#80ffd300"  
  17.                         android:centerColor="#80ffb600"  
  18.                         android:centerY="0.75"  
  19.                         android:endColor="#a0ffcb00"  
  20.                         android:angle="270"  
  21.                 />  
  22.             </shape>  
  23.         </clip>  
  24.     </item>  
  25.       
  26. </layer-list>  

thumb

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.    <selector xmlns:android="http://schemas.android.com/apk/res/android"  
  3.        >  
  4.     <!-- 按下狀態 -->  
  5.     <item android:state_pressed="true"  
  6.         android:drawable="@drawable/thumb_dn" />  
  7.   
  8.     <!-- 焦點狀態 -->  
  9.     <item android:state_focused="true"  
  10.         android:drawable="@drawable/thumb_up" />  
  11.       
  12.     <!-- 默認狀態 -->  
  13.     <item android:drawable="@drawable/thumb_up" />    
  14.       
  15. </selector>   


4:主佈局裏面調用

[html] view plaincopy
  1. <SeekBar  
  2.           style="@style/my_seekbar_self"  
  3.           android:layout_width="match_parent"  
  4.           android:layout_height="wrap_content"  
  5.           android:layout_alignParentLeft="true"  
  6.           android:layout_below="@+id/progressBar2"  
  7.           android:max="100"  
  8.           android:padding="2dp"  
  9.           android:progress="20" />  


效果



用到的圖片 網上找的 






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章