您的位置:首页 > 移动开发 > Android开发

android代码实现圆角布局及selector背景选择

2015-05-12 10:36 316 查看
烦,项目改需求了,需要标题栏 及大部分的按钮颜色随时间变化:需求是这些控件早上是一个颜色,中午是一个颜色,晚上又是一个颜色,并且这些颜色值得后台返给前台,控件有圆角的,也有不是圆角的,都有点击效果。对于产品的这些需求,我只想说你开心就好。该改还是得改。

整理一下:布局圆角、selector、rgb色值设置背景色。这里面,一般的没有点击效果的不是圆角的控件很好处理,只要设置背景色就好。对于这些有圆角 或者有点击效果 的控件,想到了用代码实现色了selector和圆角布局,这时候对后台返回的rgb色值就相对容易操作了。

实现:

首先申明按钮点击状态:

public static int[] mNormalState = new int[] {};
public static int[] mPressState = new int[] { android.R.attr.state_pressed, android.R.attr.state_enabled };
public static int[] mDisableState = new int[] { -android.R.attr.state_enabled };
public static int[] mSelectedState = new int[] { android.R.attr.state_selected, android.R.attr.state_enabled };


写了一个静态的方法:适合相关的控件使用,相关的代码设置圆角布局 ,代码设置selector都子啊方法中

/**
*
* @author
* @Description: 圆角布局
* @param context
* @param cTopLeft
*            布局左上角 圆角半径
* @param cTopRight
*            布局右上角 圆角半径
* @param cBottomLeft
*            布局左下角 圆角半径
* @param cBottomRight
*            布局右下角 圆角半径
*/
public static void modifyIndexBtnCircleBg(Context context, View view, float cTopLeft, float cTopRight, float cBottomLeft,
float cBottomRight) {
//
float outRectr[] = new float[] { cTopLeft, cTopLeft, cTopRight, cTopRight, cBottomRight, cBottomRight, cBottomLeft, cBottomLeft };
// 创建状态管理器 ,根据控件的状态,改变控件显示
//创建drawable
StateListDrawable drawable = new StateListDrawable();
//设置圆角
RoundRectShape rectShape = new RoundRectShape(outRectr, null, null);
// 创建按钮按下的状态 并设置圆角
ShapeDrawable pressedDrawable = new ShapeDrawable(rectShape);
//创建按钮正常状态 并设置圆角
ShapeDrawable normalDrawable = new ShapeDrawable(rectShape);
// 设置我们按钮背景的颜色
switch (TimeUtil.timePoint()) {
//早上
case NUM_MORNING:
normalDrawable.getPaint().setColor(
Color.argb(255, Constants.rMorningSelect, Constants.gMorningSelect, Constants.bMorningSelect));
pressedDrawable.getPaint().setColor(
Color.argb(255, Constants.rMorningSelectNot, Constants.gMorningSelectNot, Constants.bMorningSelectNot));
break;
//中午
case NUM_NOON:
//设置rgb颜色值
normalDrawable.getPaint().setColor(Color.argb(255, Constants.rNoonSelect, Constants.gNoonSelect, Constants.bNoonSelect));
pressedDrawable.getPaint().setColor(
Color.argb(255, Constants.rNoonSelectNot, Constants.gNoonSelectNot, Constants.bNoonSelectNot));
break;
//晚上
case NUM_NIGHT:
normalDrawable.getPaint().setColor(Color.argb(255, Constants.rNightSelect, Constants.gNightSelect, Constants.bNightSelect));
pressedDrawable.getPaint().setColor(
Color.argb(255, Constants.rNightSelectNot, Constants.gNightSelectNot, Constants.bNightSelectNot));
break;
}
// 添加点击是按下的状态
drawable.addState(mPressState, pressedDrawable);
// 添加正常的状态
drawable.addState(mNormalState, normalDrawable);
view.setBackgroundDrawable(drawable);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: