按钮的状态
- 默认
- 按下
- disable
颜色生成工具
public class ColorUtils {
private int color;
public static final int DISABLED_ALPHA_FILL = 165;
public static final int DISABLED_ALPHA_EDGE = 190;
public static final float ACTIVE_OPACITY_FACTOR_FILL = 0.125f;
public static final float ACTIVE_OPACITY_FACTOR_EDGE = 0.025f;
public ColorUtils(int color) {
this.color = color;
}
/**
* Darkens a color by reducing its RGB channel values.
*
* @param context
* the current context
* @param res
* the color resource
* @param percent
* the percent to decrease
* @return a color int
*/
public static int decreaseRgbChannels(int c, float percent) {
// reduce rgb channel values to produce box shadow effect
int red = (Color.red(c));
red -= (red * percent);
red = red > 0 ? red : 0;
int green = (Color.green(c));
green -= (green * percent);
green = green > 0 ? green : 0;
int blue = (Color.blue(c));
blue -= (blue * percent);
blue = blue > 0 ? blue : 0;
return Color.argb(Color.alpha(c), red, green, blue);
}
public static int increaseOpacity(int color, int alpha) {
return increaseOpacityFromInt(color, alpha);
}
public static int increaseOpacityFromInt(int c, int alpha) {
return Color.argb(alpha, Color.red(c), Color.green(c), Color.blue(c));
}
public int defaultEdge() {
return decreaseRgbChannels(color, ACTIVE_OPACITY_FACTOR_EDGE);
}
public int activeFill() {
return decreaseRgbChannels(color, ACTIVE_OPACITY_FACTOR_FILL);
}
public int activeEdge() {
return decreaseRgbChannels(color, ACTIVE_OPACITY_FACTOR_FILL + ACTIVE_OPACITY_FACTOR_EDGE);
}
public int disabledFill() {
return increaseOpacity(color, DISABLED_ALPHA_FILL);
}
public int disabledEdge() {
return increaseOpacity(color, DISABLED_ALPHA_FILL - DISABLED_ALPHA_EDGE);
}
}
Color类为android中的类,在android.graphics包中
运行
public class ColorMain {
public static void main(String[] args) {
int color = 0xff479af8;
ColorUtils colorUtils = new ColorUtils(color);
System.out.println("默认状态填充颜色:#" + Integer.toHexString(color));
System.out.println("默认状态边缘颜色:#" + Integer.toHexString(colorUtils.defaultEdge()));
System.out.println("按下状态填充颜色:#" + Integer.toHexString(colorUtils.activeFill()));
System.out.println("按下状态边缘颜色:#" + Integer.toHexString(colorUtils.activeEdge()));
System.out.println("禁止状态填充颜色:#" + Integer.toHexString(colorUtils.disabledFill()));
System.out.println("禁止状态边缘颜色:#" + Integer.toHexString(colorUtils.disabledEdge()));
}
}
结果
在value/colors.xml中配置颜色值
<color name="button_primary">#ff479af8</color>
<color name="button_primary_edge">#ff4596f1</color>
<color name="button_primary_pressed">#ff3e86d9</color>
<color name="button_primary_pressed_edge">#ff3c82d2</color>
<color name="button_primary_disabled">#a5479af8</color>
<color name="button_primary_disabled_edge">#e7479af8</color>
在drawable添加selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape>
<solid android:color="@color/button_primary_pressed" />
<stroke android:width="1dp" android:color="@color/button_primary_edge" />
<corners android:radius="@dimen/bbuton_rounded_corner_radius" />
</shape>
</item>
<item android:state_enabled="false">
<shape>
<solid android:color="@color/button_primary_disabled" />
<stroke android:width="1dp" android:color="@color/button_primary_disabled_edge" />
<corners android:radius="@dimen/bbuton_rounded_corner_radius" />
</shape>
</item>
<item>
<shape>
<solid android:color="@color/button_primary" />
<stroke android:width="1dp" android:color="@color/button_primary_edge" />
<corners android:radius="@dimen/bbuton_rounded_corner_radius" />
</shape>
</item>
</selector>
在按钮中应用
<Button
android:layout_width="match_parent"
android:layout_height="44dp"
android:background="@drawable/btn_blue"
android:text="@string/login"
android:textColor="@color/white"
/>
参考github