1. 程式人生 > >程式碼中設定Shape和Selector

程式碼中設定Shape和Selector

開發中經常需要使用Shape和Selector,如果每個都用xml設定的話,會佔用apk大小,同時命名多了也會混亂,使用程式碼來設定會方便很多。

需要用到2個類:GradientDrawable(shape)和StateListDrawable(Selector)

GradientDrawable相關api:

setColor(rgb); //填充顏色
setGradientType(GradientDrawable.RECTANGLE); //shape矩形
setCornerRadius(radius); //四周圓角半徑
setStroke(1,strokenColor); //邊框厚度與顏色

gd.setCornerRadii(floats);//設定圓角,左上,右上,右下,左下。八個引數new floats[8 ]


下面提供一個工具類以及使用程式碼
【工具類DrawableUtils】:shape

public class DrawableUtils {  
public static GradientDrawable getDrawable(int rgb,float radius){  
GradientDrawable gradientDrawable = new GradientDrawable();  
gradientDrawable.setColor(rgb);//設定顏色  
gradientDrawable.setGradientType(GradientDrawable.RECTANGLE);//設定顯示的樣式  
gradientDrawable.setCornerRadius(radius);//設定圓角的半徑  
gradientDrawable.setStroke(DensityUtil.dip2px(1),rgb);//描邊  
return gradientDrawable;  
}
但是如果想設定Gradient的漸變色該咋辦呢?

方法是改變GradientDrawable的建立方法:

int colors[] = { 0xff255779 , 0xff3e7492, 0xffa6c0cd };//分別為開始顏色,中間夜色,結束顏色

GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors);

StateListDrawable工具類:seletor---drawable

public static StateListDrawable getSelector(Drawable normalDrawable,Drawable pressDrawable) {  
StateListDrawable stateListDrawable = new StateListDrawable();  
//給當前的顏色選擇器新增選中圖片指向狀態,未選中圖片指向狀態  
stateListDrawable.addState(new int[]{android.R.attr.state_enabled, android.R.attr.state_pressed}, pressDrawable);  
//設定預設狀態  
stateListDrawable.addState(new int[]{}, normalDrawable);  
return stateListDrawable;  
}  
}

seletor---color

ColorStateList(int[][] states, int[] colors)

引數一:設定狀態是一個二維陣列  引數二:對應的顏色值

例如:

mBtnTextChange = (Button) findViewById(R.id.btn_text_change);
        int[][] states = {{-android.R.attr.state_pressed}, {android.R.attr.state_pressed}}; //狀態
        ColorStateList colorStateList = new ColorStateList(states, new int[]{Color.BLUE, Color.RED});
        mBtnTextChange.setTextColor(colorStateList);

前面帶符號的表示android:state_pressed="false"

程式碼中設定:

//設定Selector

GradientDrawable normalDrawable = DrawableUtils.getDrawable(rgb, radius);  
GradientDrawable pressDrawable = DrawableUtils.getDrawable(Color.WHITE, radius);  
StateListDrawable selector = DrawableUtils.getSelector(normalDrawable, pressDrawable);  
textView.setBackground(selector);
例項:
public void initData(String content) {  
//隨機打亂datas順序  
Collections.shuffle(Arrays.asList(datas));  
random=new Random();  
//動態建立TextView  
for(int i = 0; i < datas.length; i++) {  
TextView textView=new TextView(getActivity());  
textView.setText(datas[i]);  
//【設定textView的屬性】=============================================  
//字型大小  
textView.setTextSize(DensityUtil.dip2px(10));  
//padding  
int padding = DensityUtil.dip2px(3);  
textView.setPadding(padding,padding,padding,padding);  
//margin  
ViewGroup.MarginLayoutParams params=new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);  
params.leftMargin=DensityUtil.dip2px(8);  
params.topMargin=DensityUtil.dip2px(8);  
params.rightMargin=DensityUtil.dip2px(8);  
params.bottomMargin=DensityUtil.dip2px(8);  
textView.setLayoutParams(params);  
  
//背景顏色  
int red=random.nextInt(211);  
int green=random.nextInt(211);  
int blue=random.nextInt(211);  
int rgb= Color.rgb(red, green, blue);  
//設定背景方式1:  
//textView.setBackgroundColor(rgb);  
//設定背景方式2:  
float radius=DensityUtil.dip2px(5);  
//textView.setBackground(DrawableUtils.getDrawable(rgb,radius));  
  
//【設定textView的點選事件或者說是selector】===========================  
//設定Selector  
GradientDrawable normalDrawable = DrawableUtils.getDrawable(rgb, radius);  
GradientDrawable pressDrawable = DrawableUtils.getDrawable(Color.WHITE, radius);  
StateListDrawable selector = DrawableUtils.getSelector(normalDrawable, pressDrawable);  
textView.setBackground(selector);
/if(Build.VERSION.SDK_INT < 16){//根據版本  
    narrowView.setBackgroundDrawable(gd);  
}else{  
    narrowView.setBackground(gd);  
}//設定點選響應// textView.setClickable(true);textView.setOnClickListener(new View.OnClickListener() 
{@Override
public void onClick(View v) {Toast.makeText(getActivity(), ((TextView)v).getText().toString(), Toast.LENGTH_SHORT).show();}});
//【將textView新增到FramLayout中】=====================================flowLayout.addView(textView);}}  

程式碼中動態改變shape的color:

GradientDrawable drawable =(GradientDrawable)view.getBackground();
drawable.setColor(getResources().getColor(color));