关闭

Android动态封装圆角,按下效果的背景

265人阅读 评论(0) 收藏 举报
/**

* @param RADIUS
*            角度
* @param normalFileName
*            正常颜色
* @param pressFileName
*            点击颜色
* @param type
*            -1:left 0:center 1:right 2:四角圆角
* @return
*/
public static StateListDrawable addDrawable(int RADIUS, int normalFileName,
int pressFileName, int type) {
StateListDrawable sd = new StateListDrawable();


if (RADIUS != 0) {
float[] float1 = null;


if (type == -1) {
float1 = new float[] { 0, 0, 0, 0, 0, 0, RADIUS, RADIUS };


} else if (type == 0) {
float1 = new float[] { 0, 0, 0, 0, 0, 0, 0, 0 };


} else if (type == 1) {
float1 = new float[] { 0, 0, 0, 0, RADIUS, RADIUS, 0, 0 };


} else if (type == 2) { // 设置四个角为圆角
float1 = new float[] { RADIUS, RADIUS, RADIUS, RADIUS, RADIUS,
RADIUS, RADIUS, RADIUS };


} else {
float1 = new float[] { 0, 0, 0, 0, RADIUS, RADIUS, RADIUS,
RADIUS };
}


GradientDrawable gd = new GradientDrawable();
gd.setCornerRadii(float1);
gd.setColor(normalFileName);
GradientDrawable gd2 = new GradientDrawable();
gd2.setCornerRadii(float1);
gd2.setColor(pressFileName);


int pressed = android.R.attr.state_pressed;
int window_focused = android.R.attr.state_window_focused;
int focused = android.R.attr.state_focused;
int selected = android.R.attr.state_selected;
sd.addState(new int[] { pressed, window_focused }, gd2);
sd.addState(new int[] { pressed, -focused }, gd2);
sd.addState(new int[] { selected }, gd2);
sd.addState(new int[] { focused }, gd2);
sd.addState(new int[] {}, gd);
return sd;


} else {
GradientDrawable gd = new GradientDrawable();
gd.setColor(normalFileName);
GradientDrawable gd2 = new GradientDrawable();
gd2.setColor(pressFileName);
int pressed = android.R.attr.state_pressed;
int window_focused = android.R.attr.state_window_focused;
int focused = android.R.attr.state_focused;
int selected = android.R.attr.state_selected;
sd.addState(new int[] { pressed, window_focused }, gd2);
sd.addState(new int[] { pressed, -focused }, gd2);
sd.addState(new int[] { selected }, gd2);
sd.addState(new int[] { focused }, gd2);
sd.addState(new int[] {}, gd);
return sd;
}


}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:739次
    • 积分:28
    • 等级:
    • 排名:千里之外
    • 原创:2篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章存档