設計有三寶:圓角、漸變、陰影
對我程序來說,圓角、漸變是日常。要麼寫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;
}
}