android ProgressBar定製模式,自訂模式動畫檔案,androidprogressbar

來源:互聯網
上載者:User

android ProgressBar定製模式,自訂模式動畫檔案,androidprogressbar
1.indeterminate mode | 沒有具體進度的進度條

1.1 定製動畫檔案
[html] view plaincopy
  1. <ProgressBar  
  2.   android:layout_width="wrap_content"  
  3.   android:layout_height="wrap_content"  
  4.   android:indeterminateDrawable="@drawable/progress_my_style"  
  5.   style="?android:attr/progressBarStyle"  
  6. />  


註:style="?android:attr/progressBarStyle" 這是預設樣式 ,可換改

step2: 在drawable檔案夾下建立progress_my_style.xml檔案:內容可如下:

[html] view plaincopy
  1. <animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"  
  2.   android:drawable="@drawable/spinner_color"  
  3.   android:pivotX="50%"  
  4.   android:pivotY="50%"  
  5.   />  



注意:
1. android:drawable="@drawable/spinner_color" 這裡,你需要在drawable下建立spinner_color.png圖片(自己畫成你所想到的效果(漸層))

2. progress_my_style.xml檔案內容參考至D:\andrirod\android-sdk-windows\platforms\android-7\data\res\drawable\progress_medium_white.xml檔案內容, android:framesCountandroid:frameDuratiion是frameworks內部的屬性,無法直接使用,所以省略)
以上是在XML中直接定義,下面是我在代碼裡直接定義的:
[java] view plaincopy
  1. mProgressBar.setIndeterminateDrawable(AppConfig.getResources().getDrawable(R.drawable.style_common_processbar));  


style_common_processbar.xml還是需要定義在xml裡的
[html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <animated-rotate   
  3.     android:drawable="@drawable/icon"  
  4.     android:pivotX="50.0%"   
  5.     android:pivotY="50.0%"   
  6.     xmlns:android="http://schemas.android.com/apk/res/android">      
  7. </animated-rotate>  

結果是就是用圖片icon旋轉來表示進度,圖片製作參考 android的 drawable_hdpi 下的 spinner_black_48.png

1.2 定製顏色和形狀
利用shape來定製顏色和形狀
[java] view plaincopy
  1. // 定製模式  
  2. mProgressBar.setIndeterminateDrawable(AppConfig.getResources().getDrawable(R.drawable.style_common_processbar));  

style_common_processbar.xml
[html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <rotate   
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:duration="30"   
  5.     android:fromDegrees="0.0"   
  6.     android:toDegrees="360.0"   
  7.     android:pivotX="50.0%"   
  8.     android:pivotY="50.0%"   
  9.     android:repeatCount="infinite">  
  10.     <shape   
  11.         android:shape="ring"   
  12.         android:innerRadiusRatio="3.2"   
  13.         android:thicknessRatio="5.333"   
  14.         android:useLevel="false">  
  15.         <size   
  16.             android:height="16.0dip"   
  17.             android:width="16.0dip" />  
  18.         <gradient   
  19.             android:startColor="#4cffffff"   
  20.             android:endColor="#ffff0000"   
  21.             android:useLevel="false"   
  22.             android:type="sweep"   
  23.             android:centerY="0.5"   
  24.             android:centerColor="#4cffffff" />  
  25.     </shape>  
  26. </rotate>  

通過修改
android:shape=["rectangle" | "oval" | "line" | "ring"] 來修改形狀
gradient來修改顏色屬性 

2.progress mode | 有百分比進度的進度條
代碼調用: mProgressBar.setProgressDrawable(AppConfig.getResources().getDrawable(R.drawable.color));
color.xml [html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <layer-list xmlns:android="http://schemas.android.com/apk/res/android">  
  3.         <item android:id="@android:id/background" android:drawable="@drawable/bg" />  
  4.         <item android:id="@android:id/secondaryProgress" android:drawable="@drawable/secondary" />  
  5.         <item android:id="@android:id/progress" android:drawable="@drawable/progress" />  
  6. </layer-list>  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.