Android-做個性化的進度條

?1.案例效果圖

progress[1]

2.準備素材

?? image

????????? progress1?????????????????????????????? progress2

progress1.png(78*78)????????????? progress2.png(78*78)

3.原理

采用一張圖片作為ProgressBar的背景圖片(一般采用顏色比較淺的)。另一張是進度條的圖片(一般采用顏色比較深的圖片)。進度在滾動時:進度圖片逐步顯示,背景圖片逐步隱藏,達到上面的效果。

4.靈感來自Android控件提供的源碼

4.1 默認帶進度的進度條,如下圖

[html] view plaincopyprint?在CODE上查看代碼片派生到我的代碼片
  1. <ProgressBar??
  2. ???android:id="@+id/progressBar2"??
  3. ???style="@android:style/Widget.ProgressBar.Horizontal"??
  4. ???android:layout_width="268dp"??
  5. ????android:layout_height="wrap_content"??
  6. ???android:progress="45"?/>???
 <ProgressBarandroid:id="@+id/progressBar2"style="@android:style/Widget.ProgressBar.Horizontal"android:layout_width="268dp"android:layout_height="wrap_content"android:progress="45" /> 

image

注意:關鍵是style屬性在起作用

4.2 找到樣式定義的位置

???? 鼠標放在style屬性值上,按下Ctrl鍵,出現超鏈接,點擊超鏈接跳轉到樣式的定義位置

????????? image

???? 樣式定義的內容如下?????????

image

重點研究:

android:progressDrawable:進度條的樣式

@android:drawable/progress_horizontal:樣式定義的文件

在android-sdk-windows\platforms\android-14\data\res目下搜索progress_horizontal.xml文件,搜索結果如下:

??? image

打開progress_horizontal.xml文件,內容如下

[html] view plaincopyprint?在CODE上查看代碼片派生到我的代碼片
  1. <layer-listxmlns:androidlayer-listxmlns:android="http://schemas.android.com/apk/res/android">?????
  2. ?????<itemandroid:iditemandroid:id="@android:id/background">??
  3. ????????<shape>??
  4. ?????????????<cornersandroid:radiuscornersandroid:radius="5dip"/>??
  5. ?????????????<gradient??
  6. ?????????????????????android:startColor="#ff9d9e9d"??
  7. ?????????????????????android:centerColor="#ff5a5d5a"??
  8. ?????????????????????android:centerY="0.75"??
  9. ????????????????????android:endColor="#ff747674"??
  10. ?????????????????????android:angle="270"??
  11. ????????????/>??
  12. ?????????</shape>??
  13. ????</item>?????
  14. ?????<itemandroid:iditemandroid:id="@android:id/secondaryProgress">??
  15. ?????????<clip>??
  16. ?????????????<shape>??
  17. ?????????????????<cornersandroid:radiuscornersandroid:radius="5dip"/>??
  18. ?????????????????<gradient??
  19. ?????????????????????????android:startColor="#80ffd300"??
  20. ????????????????????????android:centerColor="#80ffb600"??
  21. ?????????????????????????android:centerY="0.75"??
  22. ????????????????????????android:endColor="#a0ffcb00"??
  23. ?????????????????????????android:angle="270"??
  24. ?????????????????/>??
  25. ????????????</shape>??
  26. ????????</clip>??
  27. ????</item>?????
  28. ????<itemandroid:iditemandroid:id="@android:id/progress">??
  29. ?????????<clip>??
  30. ?????????????<shape>??
  31. ?????????????????<cornersandroid:radiuscornersandroid:radius="5dip"/>??
  32. ?????????????????<gradient??
  33. ?????????????????????????android:startColor="#ffffd300"??
  34. ?????????????????????????android:centerColor="#ffffb600"??
  35. ?????????????????????????android:centerY="0.75"??
  36. ????????????????????????android:endColor="#ffffcb00"??
  37. ?????????????????????????android:angle="270"??
  38. ?????????????????/>??
  39. ?????????????</shape>??
  40. ?????????</clip>??
  41. ?????</item>??
  42. ?</layer-list>??
<layer-listxmlns:android="http://schemas.android.com/apk/res/android">   <itemandroid:id="@android:id/background"><shape><cornersandroid:radius="5dip"/><gradientandroid:startColor="#ff9d9e9d"android:centerColor="#ff5a5d5a"android:centerY="0.75"android:endColor="#ff747674"android:angle="270"/></shape></item>   <itemandroid:id="@android:id/secondaryProgress"><clip><shape><cornersandroid:radius="5dip"/><gradientandroid:startColor="#80ffd300"android:centerColor="#80ffb600"android:centerY="0.75"android:endColor="#a0ffcb00"android:angle="270"/></shape></clip></item>   <itemandroid:id="@android:id/progress"><clip><shape><cornersandroid:radius="5dip"/><gradientandroid:startColor="#ffffd300"android:centerColor="#ffffb600"android:centerY="0.75"android:endColor="#ffffcb00"android:angle="270"/></shape></clip></item></layer-list>

釋義:

???? <item android:id="@android:id/background">:定義進度條的背景樣式

???? <item android:id="@android:id/secondaryProgress">:輔助進度條的樣式

??? <item android:id="@android:id/progress">:進度條的樣式

思考:如果我想做垂直進度條,怎么辦了?

關鍵在clip元素的屬性上做修改

<clipandroid:clipOrientation="vertical"    定義滾動的方向 vertical為垂直方向android:drawable="@drawable/progress1" 定義進度的圖片 android:gravity="bottom" > 定義進度的開始位置 </clip>

?

5.定義樣式文件progress_vertical.xml

image

progress_vertical.xml文件代碼如下

[html] view plaincopyprint?在CODE上查看代碼片派生到我的代碼片
  1. <?xmlversionxmlversion="1.0"encoding="utf-8"?>??
  2. <layer-listxmlns:androidlayer-listxmlns:android="http://schemas.android.com/apk/res/android">??
  3. ?????<itemandroid:iditemandroid:id="@android:id/progress">??
  4. ????????<clip??
  5. ????????????android:clipOrientation="vertical"??
  6. ????????android:drawable="@drawable/progress1"??
  7. ?????????android:gravity="bottom">??
  8. ??????</clip>??
  9. ?</item>??
  10. </layer-list>??
<?xmlversion="1.0"encoding="utf-8"?>
<layer-listxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:id="@android:id/progress"><clipandroid:clipOrientation="vertical"android:drawable="@drawable/progress1"android:gravity="bottom"></clip></item>
</layer-list>

?

?

6.應用自定義的樣式?

[html] view plaincopyprint?在CODE上查看代碼片派生到我的代碼片
  1. <Button??
  2. ????????android:id="@+id/btStart"??
  3. ????????android:layout_width="fill_parent"??
  4. ????????android:layout_height="wrap_content"??
  5. ????????android:layout_marginTop="150dp"??
  6. ????????android:text="開始"/>??
  7. ???
  8. ????<ProgressBar??
  9. ????????android:id="@+id/pbPic"??
  10. ????????style="@android:style/Widget.ProgressBar.Horizontal"??
  11. ????????android:layout_width="50dp"??
  12. ????????android:layout_height="68dp"??
  13. ????????android:background="@drawable/progress2"??
  14. ????????android:max="100"??
  15. ????????android:progress="0"??
  16. ????????android:progressDrawable="@drawable/progress_vertical"?/>????<!--?在此屬性上應用?-->??
  17. ???
  18. ????<TextView??
  19. ????????android:id="@+id/txtProgress"??
  20. ????????android:layout_width="wrap_content"??
  21. ????????android:layout_height="wrap_content"/>??
<Buttonandroid:id="@+id/btStart"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginTop="150dp"android:text="開始"/><ProgressBarandroid:id="@+id/pbPic"style="@android:style/Widget.ProgressBar.Horizontal"android:layout_width="50dp"android:layout_height="68dp"android:background="@drawable/progress2"android:max="100"android:progress="0"android:progressDrawable="@drawable/progress_vertical" />    <!-- 在此屬性上應用 --><TextViewandroid:id="@+id/txtProgress"android:layout_width="wrap_content"android:layout_height="wrap_content"/>

?

7.點擊按鈕模擬進度滾動的效果

[java] view plaincopyprint?在CODE上查看代碼片派生到我的代碼片
  1. <span?style="color:#333333;">public?class??ProgressActivity?extends?Activity?{???????
  2. ??????ProgressBar?pb?=?null;??
  3. ??????TextView?txtProgress;??
  4. ??????Handler?handler?=?new?Handler();??
  5. ??????@Override??
  6. ??????publicvoid?onCreate(Bundle?savedInstanceState)?{??
  7. ???????????super.onCreate(savedInstanceState);??
  8. ???????????setContentView(R.layout.main);??
  9. ???????????System.out.println("主題="?+?getTheme()?+?"");??
  10. ???????????pb?=?(ProgressBar)?findViewById(R.id.pbPic);??
  11. ???????????Button?btnStart?=?(Button)?findViewById(R.id.btStart);//按鈕??
  12. ???????????txtProgress?=?(TextView)?findViewById(R.id.txtProgress);//顯示進度??
  13. ???????????btnStart.setOnClickListener(new?OnClickListener()?{//按鈕點擊事件??
  14. ?????????????????publicvoid?onClick(View?v)?{??
  15. ??????????????????????new?Thread(new?Runnable()?{//創建并啟動線程,使用線程執行模擬的任務??
  16. ???????????????????????????????????????publicvoid?run()?{??
  17. ?????????????????????????????????????????????for?(inti?=?0;?i?<?100;?i++)?{?//循環100遍??
  18. ??????????????????????????????????????????????????try?{??
  19. ????????????????????????????????????????????????????????handler.post(new?Runnable()?{?//更新界面的數據??
  20. ??????????????????????????????????????????????????????????????publicvoid?run()?{??
  21. ???????????????????????????????????????????????????????????????????pb.incrementProgressBy(1);//增加進度??
  22. ???????????????????????????????????????????????????????????????????txtProgress.setText(pb.getProgress()?+?"%");//顯示完成的進度??
  23. ??????????????????????????????????????????????????????????????}??
  24. ????????????????????????????????????????????????????????});??
  25. ????????????????????????????????????????????????????????Thread.sleep(100);??
  26. ??????????????????????????????????????????????????}?catch?(InterruptedException?e)?{??
  27. ???
  28. ??????????????????????????????????????????????????}??
  29. ?????????????????????????????????????????????}??
  30. ???????????????????????????????????????}??
  31. ??????????????????????????????????}).start();??
  32. ?????????????????}??
  33. ???????????});??
  34. ??????}</span>??
  35. }??
<span style="color:#333333;">public class  ProgressActivity extends Activity {     ProgressBar pb = null;TextView txtProgress;Handler handler = new Handler();@Overridepublicvoid onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);System.out.println("主題=" + getTheme() + "");pb = (ProgressBar) findViewById(R.id.pbPic);Button btnStart = (Button) findViewById(R.id.btStart);//按鈕txtProgress = (TextView) findViewById(R.id.txtProgress);//顯示進度btnStart.setOnClickListener(new OnClickListener() {//按鈕點擊事件publicvoid onClick(View v) {new Thread(new Runnable() {//創建并啟動線程,使用線程執行模擬的任務publicvoid run() {for (inti = 0; i < 100; i++) { //循環100遍try {handler.post(new Runnable() { //更新界面的數據publicvoid run() {pb.incrementProgressBy(1);//增加進度txtProgress.setText(pb.getProgress() + "%");//顯示完成的進度}});Thread.sleep(100);} catch (InterruptedException e) {}}}}).start();}});}</span>
}

?

?

轉載于:https://www.cnblogs.com/hudabing/p/4571113.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/374956.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/374956.shtml
英文地址,請注明出處:http://en.pswp.cn/news/374956.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

匯編小記16/3/27

最后更新2016-03-27 21:05:06 [address]與[bx] [address] 在debug中mov ax,[0] 等價于mov ax,ds:[0] [0]表示內存偏移地址 但是在masm匯編解釋器中&#xff0c;mov ax,[0] 等價于mov ax,0 [0]表示常量0 [bx] mov ax,[bx] 表示 bx存放的數據為一個偏移地址&#xff0c;段…

ConcurrentLinkedHashMap v 1.0.1發布

大家好&#xff0c;我們發布了并發LinkedHashMap實現的1.0.1版本。 在最新版本中&#xff0c;已進行了一些較小的修改&#xff0c;以在多個線程遍歷映射的元素時提高性能。 最新版本還引入了可插拔驅逐策略。 當然&#xff0c;您可以實現自定義逐出策略&#xff0c;也可以將它…

BOMbing The System

roy g bivFebruary 2011 [Back to index] [Comments (0)] What is a BOM? Why should we care? Great, can we do that? Okay, lets do it! Unicode in files Greets to friendly people (A-Z) What is a BOM? Its not the thing that explodes. Thats a BOMB. Heh. BO…

鳥哥的linux私房菜學習筆記 ---第7章-2

1,文件內容查閱的命令: cat ,tac nl,more, less,head,tail ,od 文件的查閱參數,顯示行號如何顯示行號 nl 中的所有參數都是關于如何顯示行號的 這里面less的功能更多,更靈活 :空格 下一頁 pageup上一頁 pagedown 下一頁 /string 字符串查詢 ?string 反向字符串查詢 man的命…

HDU - 4497 GCD and LCM

題意&#xff1a;給出三個數的gcd,lcm&#xff0c;求這三個數的全部的可能 思路 &#xff1a;設x,y,z的gcd為d&#xff0c;那么設xd*a&#xff0c;yd*b&#xff0c;zd*c。a&#xff0c;b。c肯定是互質的。那么lcmd*a*b*c,所以我們能夠得到a*b*clcm/gcdans,將ans分解因數后&…

Java Lambda語法替代

關于lambda-dev郵件列表的討論已經開始解決lambdas /函數文字的Java語言語法應該是什么樣的問題。 讓我們看一個稍微平凡的例子&#xff0c;然后嘗試弄清楚問題。 Perl的人有一個很好的例子&#xff0c;說明以某種功能性的方式使用函數引用–他們稱其為Schwartzian變換&#xf…

淺析SMC技術

今天讓我們來看Win32ASM里面的高級一點的技術——SMC&#xff08;當當當當……&#xff09;&#xff01;&#xff01;&#xff01;SMC是什么意思&#xff1f;它的英文名叫“Self Modifying Code”&#xff0c;顧名思義&#xff0c;就是“代碼自修改”&#xff08;&#xff1f;&…

JAVA基礎--程序是順序執行的

class Testa {public static void main(String[] args) {String aa"aaa";String bb"bbb"aa;aa"cccc";System.out.println(bb);} } 輸出的是 “bbbaaa class Testa {public static void main(String[] args) {String aa"aaa";String …

Spring MVC攔截器示例

我以為是時候看看Spring的MVC攔截器機制了&#xff0c;這種機制已經存在了很多年&#xff0c;并且是一個非常有用的工具。 Spring Interceptor會按照提示進行操作&#xff1a;在傳入的HTTP請求到達您的Spring MVC控制器類之前對其進行攔截&#xff0c;或者相反&#xff0c;在其…

Android 調用系統的分享[完美實現同一時候分享圖片和文字]

android 系統的分享功能 private void share(String content, Uri uri){Intent shareIntent new Intent(Intent.ACTION_SEND); if(uri!null){//uri 是圖片的地址shareIntent.putExtra(Intent.EXTRA_STREAM, uri);shareIntent.setType("image/*"); //當用戶選擇短信時…

團隊行為守則—如果你們由我來領導

&#xfeff;&#xfeff;如果你是在我領導的團隊里&#xff0c;有幾個額外的事情我要告訴你。我深信這些行為守則是一個高效團隊的潤滑劑&#xff0c;我并不只是要求別人這樣做&#xff0c;我自己也嚴格恪守。 只有三樣事&#xff1a; 問&#xff1a;如果你對任務不清楚&#…

做短,但做對!

編寫簡潔&#xff0c;優雅&#xff0c;清晰的代碼一直是開發人員的艱巨任務。 您的同事不僅會感謝您&#xff0c;而且您會驚訝地發現&#xff0c;不斷期待著重構解決方案以更少的代碼完成更多&#xff08;或至少相同&#xff09;的工作是多么令人興奮。 曾經有人說好的程序員是…

math

莫比烏斯反演&#xff1a; $F(n) \sum\limits_{d|n} {f(d)} \Leftrightarrow \sum\limits_{d|n} {\mu (d)F(\frac{n}{d})} $ 其中 ${\mu (d)}$為莫比烏斯函數: 若$d$等于0 , 則${\mu (d)}$1 若$d {p_1}{p_2}{p_3}...{p_k}$ , ${p_i}$為互異質數&#xff0c;則${\mu (d)}$${( …

(筆試題)二進制1的個數相同的距離最小數

題目&#xff1a; 輸入&#xff1a;整數A輸出&#xff1a;整數B條件&#xff1a;A和B的二進制1的個數相同&#xff0c;且A和B之間的距離|A-B|最小。思路&#xff1a; 題目沒有說明整數類型&#xff0c;這里認為是帶符號的整數&#xff0c;即區分正負數。 根據題意&#xff0c;A…

Java Swing –日期選擇器對話框

房子里有Swing開發人員嗎&#xff1f; 對于使用Swing的用戶來說&#xff0c;這是一個GUI組件&#xff0c;可能會對您的UI編碼工作有所幫助。 我們的JCG合作伙伴之一提供了日期選擇器小部件。 一探究竟&#xff1a; Java Swing –日期選擇器對話框以選擇日期 翻譯自: https://…

Casperjs中fill提交表單遇到的問題

1.if you access internet with proxy please add --ignore-ssl-errorstrue --ssl-protocolany 2.casper.then* and casper.wait* 都是異步執行的 他們的調用&#xff0c;都是按堆棧中的順序來執行&#xff1b;也就是說&#xff0c;其他同步執行的函數&#xff0c;…

Xuggler視頻處理簡介

注意&#xff1a;這是我們的“ Xuggler開發教程 ”系列的一部分。 隨著互聯網上視頻的爆炸式增長&#xff0c;開發人員經常需要在其應用程序中操縱視頻內容。 Xuggler是Java開發人員的免費開放源代碼庫&#xff0c;可用于實時解壓縮&#xff0c;處理和壓縮錄制的視頻或實時視頻…

軟件測試中條件覆蓋,路徑覆蓋,語句覆蓋,分支覆蓋的區別

轉&#xff1a;軟件測試中條件覆蓋&#xff0c;路徑覆蓋&#xff0c;語句覆蓋&#xff0c;分支覆蓋的區別 舉個例子吧 if A and B then Action1 if C or D then Action2 語句覆蓋最弱&#xff0c;只需要讓程序中的語句都執行一遍即可 …

Spring_講解

http://s&#xff0c;i&#xff0c;s&#xff0c;h&#xff0c;u&#xff0c;o&#xff0c;k.com/forum/blogPost/list/6174.html轉載于:https://www.cnblogs.com/gisblogs/p/4579162.html

使用Spring AspectJ和Maven進行面向方面的編程

Spring框架附帶AOP支持。 實際上&#xff0c;如Spring參考文檔中所述 &#xff0c; “ Spring的關鍵組件之一是AOP框架。 盡管Spring IoC容器不依賴于AOP&#xff0c;這意味著您不需要使用AOP&#xff0c;但AOP是對Spring IoC的補充&#xff0c;以提供功能強大的中間件解決方案…