Android中动态的更改selector中某张图片的属性
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                Android中动态的更改selector中某张图片的属性
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.                        
                                在我們平常開發(fā)的時(shí)候會(huì)有許多狀態(tài)按鈕,比如state_pressed,android:state_checked,或者就正常狀態(tài)等等,我們做這樣的效果通常需要三個(gè)文件,一張是按下的圖片,一張是正常狀態(tài)的圖片,一張是管理它們的selector文件,如果在不斷更新迭代的過程中出現(xiàn)了很多這樣的按鈕,而且它們的顏色什么的都不一樣,那我們的res/drawable文件夾下就會(huì)出現(xiàn)很多個(gè)這樣的組合文件,導(dǎo)致我們的程序越來越大、越來越大,這肯定不是我們想看到的。
 
那么,我現(xiàn)在要拿出什么來解決這個(gè)問題呢?就是動(dòng)態(tài)的更改它們的屬性,把所有的工作都放在代碼里面,減少selector.xml文件的產(chǎn)生:
我們?cè)趯憣傩缘臅r(shí)候盡量要使用這樣的方式(如果美工給了圖的話,盡量讓提供顏色值就可以了,不要做那么多圖):
<span style="font-size:12px;"><?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:state_pressed="true"><shape><corners android:radius="5dp" /><solid android:color="#78909c" /></shape></item><item><shape><corners android:radius="5dp" /><solid android:color="#607d8b" /></shape></item></selector></span>布局文件:
<span style="font-size:12px;"><?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/container"android:layout_width="match_parent"android:layout_height="match_parent" ><Buttonandroid:id="@+id/button"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_margin="10dp"android:text="Hello"android:textColor="#fff" /></FrameLayout></span> 這個(gè)xml文件的正常顯示效果是:
 效果還是挺不錯(cuò)的,接下來我們要?jiǎng)討B(tài)改變它的效果,怎么做呢:
 
 
怎么樣,效果實(shí)現(xiàn)了我們需要的。
最后,貼上一個(gè)抽取出來的工具類:
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method;import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable.ConstantState; import android.graphics.drawable.GradientDrawable; import android.graphics.drawable.StateListDrawable;public class SelectorUtils {/*** 動(dòng)態(tài)修改selector中圖片的背景顏色* * @param drawable* selectorDrawable* @param rgbColors* 默認(rèn)以及按下狀態(tài)的顏色值*/public static void changeViewColor(StateListDrawable drawable, int[] rgbColors) {ConstantState cs = drawable.getConstantState();if (rgbColors.length < 2) {return;}try {Method method = cs.getClass().getMethod("getChildren", null);// 通過反射調(diào)用getChildren方法獲取xml文件中寫的drawable數(shù)組method.setAccessible(true);Object obj = method.invoke(cs, null);Drawable[] drawables = (Drawable[]) obj;for (int i = 0; i < drawables.length; i++) {// 接下來我們要通過遍歷的方式對(duì)每個(gè)drawable對(duì)象進(jìn)行修改顏色值GradientDrawable gd = (GradientDrawable) drawables[i];if (gd == null) {break;}if (i == 0) {// 我們對(duì)按下的狀態(tài)做淺色處理gd.setColor(rgbColors[0]);} else {// 對(duì)默認(rèn)狀態(tài)做深色處理gd.setColor(rgbColors[1]);}}// 最后總結(jié)一下,為了實(shí)現(xiàn)這個(gè)效果,剛開始并沒有看到setColor的方法,而是通過反射獲取GradientDrawable對(duì)象的屬性GradientState,// 再通過反射調(diào)用GradientState對(duì)象的setSolidColor方法去實(shí)現(xiàn),效果不太理想。// 最后在仔仔細(xì)細(xì)一一看GradientDrawable對(duì)象的屬性,發(fā)現(xiàn)屬性Paint// mFillPaint,從名字就可以看出這個(gè)對(duì)象是用來繪制drawable的背景的,// 于是順著往下找,發(fā)現(xiàn)setColor方法,于是bingo,這個(gè)過程也是挺曲折的。} catch (NoSuchMethodException e1) {e1.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();} catch (IllegalArgumentException e) {e.printStackTrace();} catch (InvocationTargetException e) {e.printStackTrace();}} }有疑問歡迎留言。
總結(jié)
以上是生活随笔為你收集整理的Android中动态的更改selector中某张图片的属性的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 【LeetCode】3月16日打卡-Da
 - 下一篇: Githug第42关rebase_ont