//獲取一個shape對象
public static GradientDrawable getGradientDrawable(int color, int radius) {
// xml中定義的shape標籤 對應此類
GradientDrawable shape = new GradientDrawable();
shape.setShape(GradientDrawable.RECTANGLE);// 矩形
shape.setCornerRadius(radius);// 圓角半徑
shape.setColor(color);// 顏色
return shape;
}
//獲取狀態選擇器
public static StateListDrawable getSelector(Drawable normal, Drawable press) {
StateListDrawable selector = new StateListDrawable();
selector.addState(new int[] { android.R.attr.state_pressed }, press);// 按下圖片
selector.addState(new int[] {}, normal);// 默認圖片
return selector;
}
//獲取狀態選擇器
public static StateListDrawable getSelector(int normal, int press, int radius) {
GradientDrawable bgNormal = getGradientDrawable(normal, radius);
GradientDrawable bgPress = getGradientDrawable(press, radius);
StateListDrawable selector = getSelector(bgNormal, bgPress);
return selector;
}
java代碼編寫shape文件和狀態選擇器
java代碼編寫shape文件和狀態選擇器
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.