android ProgressBar 進度條的進度兩端是圓角的方法


轉自 http://www.jianshu.com/p/6e7ea842d5ce

另外工作原理可以參考http://blog.csdn.net/lan603168/article/details/44705425

ProgressBar 自定義的時候可能會遇到一個問題,希望進度條中的進度的兩端都是圓角的(或者進度的末端是圓角的);
如下圖:


progress bar rounder

但是根據自定義的shape 或者是 layer-list卻總是很難做到,幾乎都是被clip成了直角的樣子;


progress bar

;

問題的原因就是如下鏈接中也會有相似的解答:
Android開發中Progress需要兩邊都是圓角怎么辦?


為什么是直角的?原因就是被clip給切了,所以我們不能夠用clip,而要使用scale這個標簽。而上面鏈接給出的解答是定義一個.9的圖片就能滿足要求,由於我們這里是純色的一個進度,所以沒有必要通過再制作一個.9的圖片,而只需要通過同樣的方法引用我們定義的一個shape就可以了;
見代碼:

<ProgressBar    
android:layout_below="@id/text_1" android:layout_marginTop="16dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/progressbar_1" style="?android:attr/progressBarStyleHorizontal" android:max="100" android:progress="40" android:progressDrawable="@drawable/progress_bar_drawable" />

progressDrawable 引用的progress_bar_drawable:

<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@android:id/background"> <shape> <corners android:topRightRadius="20dp" android:bottomRightRadius="20dp" /> <solid android:color="#ED30353E"/> </shape> </item> <item android:id="@android:id/secondaryProgress"> <scale android:scaleWidth="100%"> <shape> <corners android:topRightRadius="20dp" android:bottomRightRadius="20dp"/> <solid android:color="#11ce33"/> </shape> </scale> </item> <item android:id="@android:id/progress"> <!-- <clip> <shape> <corners android:topRightRadius="20dp" android:bottomRightRadius="20dp"/> <solid android:color="#FF009898"/> </shape> </clip> --> <scale android:scaleWidth="100%" android:drawable="@drawable/progress_bar_ct" /> </item> </layer-list>

重點就是這個:

<scale android:scaleWidth="100%" android:drawable="@drawable/progress_bar_ct" />

指定了一個我們自定義的shape:progress_bar_ct.xml

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <!-- solid指定形狀的填充色,只有android:color一個屬性 --> <solid android:color="#FF009898" /> <!-- padding設置內容區域離邊界的間距 --> <!-- corners設置圓角,只適用於rectangle --> <corners android:bottomRightRadius="20dp" android:topRightRadius="20dp"/> </shape>

這樣就不需要自定義.9的圖片了;
PS:由於本人的需求是右端才是圓角,如果需要4個角都是圓角只需要修改一點代碼即可;

<corners android:bottomRightRadius="20dp" android:topRightRadius="20dp"/>

修改成:

<corners android:radius="20dp" />

即可;



文/heybik(簡書作者)
原文鏈接:http://www.jianshu.com/p/6e7ea842d5ce
著作權歸作者所有,轉載請聯系作者獲得授權,並標注“簡書作者”。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM