设计有三宝:圆角、渐变、阴影
对我程序来说,圆角、渐变是日常。要么写shape文件,要么直接引用开源库来实现圆角渐变等效果。
这里给大家推荐一个开源项目,专门打造圆角的圆角神器
下面提供一些圆角、渐变的方法和工具类,开源和本地随意选择
主要支持:
1.可定义边框及其颜色和填充色的
/**
* @param solidColor 填充色
* @param strokeW 边框大小
* @param strokeColor 边框颜色
*/
public static GradientDrawable getGradientDrawable(Context context, int solidColor, float strokeW, int strokeColor) {
float density = context.getResources().getDisplayMetrics().density;
float strokeWidth = strokeW * density;
// 创建drawable
GradientDrawable gradientDrawable = new GradientDrawable();
gradientDrawable.setShape(GradientDrawable.OVAL);
gradientDrawable.setColor(solidColor);
gradientDrawable.setStroke((int) strokeWidth, strokeColor);
return gradientDrawable;
}
2.圆角、边框、填充色、边框色
/**
* @param radius 圆角大小
* @param solidColor 填充色
* @param strokeW 边框大小
* @param strokeColor 边框颜色
*/
public static GradientDrawable getGradientDrawable(Context context,
int radius, int solidColor, int strokeW, int strokeColor) {
float density = context.getResources().getDisplayMetrics().density;
float roundRadius = radius * density;
float strokeWidth = strokeW * density;
// 创建drawable
GradientDrawable gradientDrawable = new GradientDrawable();
gradientDrawable.setColor(solidColor);
gradientDrawable.setCornerRadius(roundRadius);
gradientDrawable.setStroke((int) strokeWidth, strokeColor);
return gradientDrawable;
}
3.圆角、渐变、渐变方向、边框、填充色、边框色
public static GradientDrawable getGradientDrawable(Context context,
float radius, GradientDrawable.Orientation orientation, int[] colors, float strokeW, int strokeColor) {
float density = context.getResources().getDisplayMetrics().density;
float roundRadius = radius * density;
float strokeWidth = strokeW * density;
GradientDrawable gradientDrawable = new GradientDrawable(orientation, colors);// 创建drawable
gradientDrawable.setCornerRadius(roundRadius);
gradientDrawable.setStroke((int) strokeWidth, strokeColor);
return gradientDrawable;
}
4.不一样的圆角、边框、填充色、边框色
public static GradientDrawable getGradientDrawable(Context context, float[] radii, int solidColor, float strokeW, int strokeColor) {
float density = context.getResources().getDisplayMetrics().density;
float strokeWidth = strokeW * density;
GradientDrawable gradientDrawable = new GradientDrawable();// 创建drawable
gradientDrawable.setColor(solidColor);
gradientDrawable.setCornerRadii(radii);
gradientDrawable.setStroke((int) strokeWidth, strokeColor);
return gradientDrawable;
}
5.带状态的的圆角、边框、填充色、边框色
public static StateListDrawable getStateListDrawable(Context context,
int colorNormal, int colorPress, float radius, int strokeWidth,
int strokeColor) {
float density = context.getResources().getDisplayMetrics().density;
float roundRadius = radius * density;
strokeWidth = (int) (strokeWidth * density);
GradientDrawable gdNormal = new GradientDrawable();
gdNormal.setColor(colorNormal);
gdNormal.setCornerRadius(roundRadius);
gdNormal.setStroke(strokeWidth, strokeColor);
GradientDrawable gdPress = new GradientDrawable();
gdPress.setColor(colorPress);
gdPress.setCornerRadius(roundRadius);
gdPress.setStroke(strokeWidth, strokeColor);
StateListDrawable drawable = new StateListDrawable();
drawable.addState(new int[]{-android.R.attr.state_pressed},
gdNormal);
drawable.addState(new int[]{android.R.attr.state_pressed}, gdPress);
return drawable;
}
6.带状态的的不同方向不同大小的圆角、边框、填充色、边框色
public static StateListDrawable getStateListDrawable(Context context, float[] radii,
int colorNormal, int colorPress, float radius, int strokeWidth,
int strokeColor) {
float density = context.getResources().getDisplayMetrics().density;
float roundRadius = radius * density;
strokeWidth = (int) (strokeWidth * density);
GradientDrawable gdNormal = new GradientDrawable();
gdNormal.setColor(colorNormal);
gdNormal.setCornerRadius(roundRadius);
gdNormal.setCornerRadii(radii);
gdNormal.setStroke(strokeWidth, strokeColor);
GradientDrawable gdPress = new GradientDrawable();
gdPress.setColor(colorPress);
gdPress.setCornerRadius(roundRadius);
gdPress.setCornerRadii(radii);
StateListDrawable drawable = new StateListDrawable();
drawable.addState(new int[]{-android.R.attr.state_pressed},
gdNormal);
drawable.addState(new int[]{android.R.attr.state_pressed}, gdPress);
return drawable;
}
以上已经可以满足日常大多数扁平化设计的圆角、渐变了,小伙伴们用得开心
下面是整个类,带参数注释
DrawableUtil.class
import android.content.Context;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;
public class DrawableUtil {
/**
* @param solidColor 填充色
* @param strokeW 边框大小
* @param strokeColor 边框颜色
*/
public static GradientDrawable getGradientDrawable(Context context, int solidColor, float strokeW, int strokeColor) {
float density = context.getResources().getDisplayMetrics().density;
float strokeWidth = strokeW * density;
// 创建drawable
GradientDrawable gradientDrawable = new GradientDrawable();
gradientDrawable.setShape(GradientDrawable.OVAL);
gradientDrawable.setColor(solidColor);
gradientDrawable.setStroke((int) strokeWidth, strokeColor);
return gradientDrawable;
}
/**
* @param radius 圆角大小
* @param solidColor 填充色
* @param strokeW 边框大小
* @param strokeColor 边框颜色
*/
public static GradientDrawable getGradientDrawable(Context context,
int radius, int solidColor, int strokeW, int strokeColor) {
float density = context.getResources().getDisplayMetrics().density;
float roundRadius = radius * density;
float strokeWidth = strokeW * density;
// 创建drawable
GradientDrawable gradientDrawable = new GradientDrawable();
gradientDrawable.setColor(solidColor);
gradientDrawable.setCornerRadius(roundRadius);
gradientDrawable.setStroke((int) strokeWidth, strokeColor);
return gradientDrawable;
}
/**
* @param orientation 渐变色方向 eg:GradientDrawable.Orientation.LEFT_RIGHT
* @param radius 圆角大小
* @param colors 渐变色默认从左到右
* @param strokeW 边框大小
* @param strokeColor 边框颜色
*/
public static GradientDrawable getGradientDrawable(Context context,
float radius, GradientDrawable.Orientation orientation, int[] colors, float strokeW, int strokeColor) {
float density = context.getResources().getDisplayMetrics().density;
float roundRadius = radius * density;
float strokeWidth = strokeW * density;
GradientDrawable gradientDrawable = new GradientDrawable(orientation, colors);// 创建drawable
gradientDrawable.setCornerRadius(roundRadius);
gradientDrawable.setStroke((int) strokeWidth, strokeColor);
return gradientDrawable;
}
/**
* @param radii 每一个角的圆角大小
* @param solidColor 填充色
* @param strokeW 边框大小
* @param strokeColor 边框颜色
*/
public static GradientDrawable getGradientDrawable(Context context, float[] radii, int solidColor, float strokeW, int strokeColor) {
float density = context.getResources().getDisplayMetrics().density;
float strokeWidth = strokeW * density;
GradientDrawable gradientDrawable = new GradientDrawable();// 创建drawable
gradientDrawable.setColor(solidColor);
gradientDrawable.setCornerRadii(radii);
gradientDrawable.setStroke((int) strokeWidth, strokeColor);
return gradientDrawable;
}
/**
* @param colorNormal 正常颜色
* @param colorPress 按下的颜色
* @param radius 圆角
* @param strokeWidth 边框
* @param strokeColor 边框色
*/
public static StateListDrawable getStateListDrawable(Context context,
int colorNormal, int colorPress, float radius, int strokeWidth,
int strokeColor) {
float density = context.getResources().getDisplayMetrics().density;
float roundRadius = radius * density;
strokeWidth = (int) (strokeWidth * density);
GradientDrawable gdNormal = new GradientDrawable();
gdNormal.setColor(colorNormal);
gdNormal.setCornerRadius(roundRadius);
gdNormal.setStroke(strokeWidth, strokeColor);
GradientDrawable gdPress = new GradientDrawable();
gdPress.setColor(colorPress);
gdPress.setCornerRadius(roundRadius);
gdPress.setStroke(strokeWidth, strokeColor);
StateListDrawable drawable = new StateListDrawable();
drawable.addState(new int[]{-android.R.attr.state_pressed},
gdNormal);
drawable.addState(new int[]{android.R.attr.state_pressed}, gdPress);
return drawable;
}
/**
* @param radii 每个方向的圆角大小
* @param colorNormal 正常颜色
* @param colorPress 按下的颜色
* @param radius 圆角
* @param strokeWidth 边框
* @param strokeColor 边框色
*/
public static StateListDrawable getStateListDrawable(Context context, float[] radii,
int colorNormal, int colorPress, float radius, int strokeWidth,
int strokeColor) {
float density = context.getResources().getDisplayMetrics().density;
float roundRadius = radius * density;
strokeWidth = (int) (strokeWidth * density);
GradientDrawable gdNormal = new GradientDrawable();
gdNormal.setColor(colorNormal);
gdNormal.setCornerRadius(roundRadius);
gdNormal.setCornerRadii(radii);
gdNormal.setStroke(strokeWidth, strokeColor);
GradientDrawable gdPress = new GradientDrawable();
gdPress.setColor(colorPress);
gdPress.setCornerRadius(roundRadius);
gdPress.setCornerRadii(radii);
StateListDrawable drawable = new StateListDrawable();
drawable.addState(new int[]{-android.R.attr.state_pressed},
gdNormal);
drawable.addState(new int[]{android.R.attr.state_pressed}, gdPress);
return drawable;
}
}