Android 動(dòng)態(tài)改變SeekBar進(jìn)度條顏色與滑塊顏色的實(shí)例代碼
遇到個(gè)動(dòng)態(tài)改變SeekBar進(jìn)度條顏色與滑塊顏色的需求,有的是根據(jù)不同進(jìn)度改變成不同顏色。
對(duì)于這個(gè)怎么做呢?大家都知道設(shè)置下progressDrawable與thumb即可,但是這樣設(shè)置好就是確定的了,要?jiǎng)討B(tài)更改需要在代碼里實(shí)現(xiàn)。
用shape進(jìn)度條與滑塊
SeekBar設(shè)置
代碼里動(dòng)態(tài)設(shè)置setProgressDrawable與setThumb
畫圖形,大家都比較熟悉,background是背景圖,secondaryProgress第二進(jìn)度條,progress進(jìn)度條:
<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="@color/gray_cc" android:centerColor="@color/gray_cc" android:centerY="0.75" android:endColor="@color/gray_cc" 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="@color/gray_cc" android:centerColor="@color/gray_cc" android:centerY="0.75" android:endColor="@color/gray_cc" android:angle="270"/> </shape> </clip> </item> </layer-list>
然后畫滑塊:
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/seekbar_thumb_gray" android:state_focused="true" android:state_pressed="true" /> <item android:drawable="@drawable/seekbar_thumb_gray" android:state_focused="false" android:state_pressed="false" /> <item android:drawable="@drawable/seekbar_thumb_gray" android:state_focused="true" android:state_pressed="false" /> <item android:drawable="@drawable/seekbar_thumb_gray" android:state_focused="true" /> </selector>
最后xml里設(shè)置:
android:progressDrawable="@drawable/seekbar_light" android:thumb="@drawable/seekbar_thumb"
這里提醒下滑塊滑到0或者最大可能展示不完,滑塊只展示一半;還有有的背景太高;所以這些需要設(shè)置一下即可:
android:maxHeight="5dp" android:minHeight="5dp" android:paddingLeft="10dp" android:paddingRight="10dp"
上面maxHeight與minHeight可以讓背景不那么高,更改值可以控制高度。
paddingLeft與paddingRight最好是滑塊的寬度一半,這樣即可展示完全。
下面進(jìn)入正題就是代碼里動(dòng)態(tài)設(shè)置顏色。
我們?cè)诖a里可以使用getProgressDrawable得到進(jìn)度背景,所以可以以此來更改。
由于得到的是LayerDrawable,包含多個(gè)層次,當(dāng)然上面我們只畫了三個(gè)層次背景;不確定,可以循環(huán)進(jìn)行得到ID進(jìn)行判斷:
//獲取seerbar層次drawable對(duì)象 LayerDrawable layerDrawable = (LayerDrawable) sb.getProgressDrawable(); // 有多少個(gè)層次(最多三個(gè)) int layers = layerDrawable.getNumberOfLayers(); Drawable[] drawables = new Drawable[layers]; for (int i = 0; i < layers; i++) { switch (layerDrawable.getId(i)) { // 如果是seekbar背景 case android.R.id.background: drawables[i] = layerDrawable.getDrawable(0); break; // 如果是拖動(dòng)條第一進(jìn)度 case android.R.id.progress: //這里為動(dòng)態(tài)的顏色值 drawables[i] = new PaintDrawable(progressColor); drawables[i].setBounds(layerDrawable.getDrawable(0).getBounds()); break; ... } } sb.setProgressDrawable(new LayerDrawable(drawables)); sb.setThumb(thumb); sb.invalidate();
上面可以得到不同的背景,然后動(dòng)態(tài)進(jìn)行更改設(shè)置即可,上面代碼可以完成最上面的圖,因?yàn)楸尘安恍枰兓伾员尘皥D不做變化,然后拿到進(jìn)度條背景后我們采用的是PaintDrawable畫顏色,當(dāng)然你可以采用其他的構(gòu)造一個(gè)背景Drawable,然后設(shè)置你需求的樣式。滑塊也可以是背景圖片,或者給進(jìn)度背景差一樣可以使用getThumb到到背景重新設(shè)置即可。
我上篇文章提到過Android更改純色背景圖片顏色,不清楚的可以點(diǎn)我進(jìn)去查看Android更改純色背景圖片顏色。
因?yàn)槲覀冞@里也是純顏色,這就更好辦,更簡單:
//獲取seerbar層次drawable對(duì)象 LayerDrawable layerDrawable = (LayerDrawable) sb.getProgressDrawable(); //因?yàn)楫嫳尘皥D時(shí)候第二進(jìn)度背景圖沒有畫,所以直接為1 Drawable drawable = layerDrawable.getDrawable(1); drawable.setColorFilter(progressColor, PorterDuff.Mode.SRC); //獲取滑塊背景 Drawable thumb = sb.getThumb(); thumb.setColorFilter(thumbColor, PorterDuff.Mode.SRC); sb.invalidate();
由上我們可以利用更改純色方法改得到的背景顏色,當(dāng)然前提是你們背景圖也是純色。
這樣又可以愉快玩耍了。
注意
第一種方案可以畫多種樣式,主要看你的對(duì)Drawable的使用了,但是如果對(duì)背景圖,如果進(jìn)度左右兩端有圓弧,動(dòng)態(tài)畫圖時(shí)候需要格外設(shè)置圓弧,估計(jì)實(shí)現(xiàn)也不簡單吧,不然背景圖是沒有圓弧的。
第二種方案對(duì)純色的可以使用,對(duì)復(fù)雜多樣式很難行得通。
以上所述是小編給大家介紹的Android 動(dòng)態(tài)改變SeekBar進(jìn)度條顏色與滑塊顏色的實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Android?autojs隨時(shí)翻譯剪貼板單詞實(shí)現(xiàn)示例
這篇文章主要為大家介紹了Android?autojs隨時(shí)翻譯剪貼板單詞,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09Android判斷用戶2G/3G/4G移動(dòng)數(shù)據(jù)網(wǎng)絡(luò)
這篇文章主要介紹了Android判斷用戶2G/3G/4G移動(dòng)數(shù)據(jù)網(wǎng)絡(luò)的方法,感興趣的小伙伴們可以參考一下2015-12-12Android 網(wǎng)絡(luò)請(qǐng)求框架Volley實(shí)例詳解
這篇文章主要介紹了Android 網(wǎng)絡(luò)請(qǐng)求框架Volley實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06Android動(dòng)態(tài)顯示當(dāng)前年月日時(shí)分秒系統(tǒng)時(shí)間(示例代碼)
這篇文章主要介紹了Android動(dòng)態(tài)顯示當(dāng)前年月日時(shí)分秒系統(tǒng)時(shí)間的示例代碼,需要的朋友可以參考下2017-05-05Android圖片處理教程之全景查看效果實(shí)現(xiàn)
這篇文章主要給大家介紹了關(guān)于Android圖片處理教程之全景查看效果實(shí)現(xiàn)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-06-06Android應(yīng)用中使用及實(shí)現(xiàn)系統(tǒng)“分享”接口實(shí)例
為了應(yīng)用的推廣、傳播,很多的應(yīng)用中都有“分享”功能,這篇文章主要介紹了Android應(yīng)用中使用及實(shí)現(xiàn)系統(tǒng)“分享”接口實(shí)例,有興趣的可以了解一下。2016-12-12android Setting中隱藏項(xiàng)實(shí)現(xiàn)原理與代碼
我們都知道做程序員有時(shí)會(huì)惡搞,就像android中,程序員在setting中就隱藏這樣一項(xiàng),接下來將詳細(xì)介紹,感興趣的朋友可以了解下哦2013-01-01