android 用代码实现文本选择器,改变字体颜色,背景色,渐变
2021-06-09 本文已影响0人
刘坤林
一下代码可以一定程度上代替我们编写xml来设置背景。
有报错自己修改一下就好。
public static MyDrawable instance ( ) {
return new MyDrawable ( );
}
private MyDrawable ( ) {
foreStyle = new ForeStyle ( );
backStyle = new BackStyle ( );
}
/**
* 前景
*/
private final ForeStyle foreStyle;
/**
* 背景
*/
private final BackStyle backStyle;
//============================前景===============================
public MyDrawable fore ( int color ) {
foreStyle.setColor ( color );
return this;
}
public MyDrawable fore ( String color ) {
foreStyle.setColor ( color );
return this;
}
public MyDrawable foreRes ( int resId ) {
foreStyle.setColorRes ( resId );
return this;
}
public MyDrawable fore ( int color, int colorSel ) {
foreStyle.setColor ( color, colorSel );
return this;
}
public MyDrawable fore ( String color, String colorSel ) {
foreStyle.setColor ( color, colorSel );
return this;
}
public MyDrawable foreRes ( int resId, int resIdSel ) {
foreStyle.setColorRes ( resId, resIdSel );
return this;
}
public MyDrawable fore ( int... colors ) {
foreStyle.setColors ( colors );
return this;
}
//============================背景===============================
public MyDrawable backRadius ( int radius ) {
backStyle.setRadius ( radius );
return this;
}
public MyDrawable backRadius ( int topLeft, int topRight, int bottomLeft,
int bottomRight ) {
backStyle.setRadius ( topLeft, topRight, bottomLeft, bottomRight );
return this;
}
public MyDrawable back ( int color ) {
backStyle.setColor ( color );
return this;
}
public MyDrawable back ( String color ) {
backStyle.setColor ( color );
return this;
}
public MyDrawable backRes ( int resId ) {
backStyle.setColorRes ( resId );
return this;
}
public MyDrawable backRes ( int resId, int colorResSel ) {
backStyle.setColorRes ( resId, colorResSel );
return this;
}
public MyDrawable back ( int color, int colorSel ) {
backStyle.setColor ( color, colorSel );
return this;
}
public MyDrawable back ( int... colors ) {
backStyle.setColors ( colors );
return this;
}
public MyDrawable backBorder ( int color, int width ) {
backStyle.setBorderColor ( color, width );
return this;
}
public MyDrawable backBorderRes ( int resId, int width ) {
backStyle.setBorderColorRes ( resId, width );
return this;
}
public MyDrawable backBorderRes ( String color, int width ) {
backStyle.setBorderColor ( color, width );
return this;
}
//============================提取===============================
/**
* 提取
*
* @param target 目标控件
*/
public void into ( View target ) {
//前景色
if ( target instanceof TextView ) {
target.setClickable ( true );
if ( foreStyle.colors != null && foreStyle.colors.length == 2 ) {
LinearGradient mLinearGradient = new LinearGradient ( 0, 0, 0,
( ( TextView ) target ).getPaint ( ).getTextSize ( ),
foreStyle.colors[ 0 ], foreStyle.colors[ 1 ],
Shader.TileMode.CLAMP );
( ( TextView ) target ).getPaint ( ).setShader ( mLinearGradient );
target.invalidate ( );
} else {
if ( foreStyle.normalColor == Integer.MAX_VALUE ) {
foreStyle.setColor ( ( ( TextView ) target ).getCurrentTextColor ( ) );
}
if ( foreStyle.selectColor == Integer.MAX_VALUE ) {
( ( TextView ) target ).setTextColor ( foreStyle.normalColor );
} else {
ColorStateList colorStateList = new ColorStateList ( new int[][] {
{ - android.R.attr.state_pressed },
{ android.R.attr.state_pressed }
}, new int[] { foreStyle.normalColor, foreStyle.selectColor } );
( ( TextView ) target ).setTextColor ( colorStateList );
}
}
}
float tl = toFloat ( 1.6, backStyle.topLeft );
float tr = toFloat ( 1.6, backStyle.topRight );
float bl = toFloat ( 1.6, backStyle.bottomLeft );
float br = toFloat ( 1.6, backStyle.bottomRight );
float[] radius = new float[] { tl, tl, tr, tr, br, br, bl, bl };
if ( backStyle.colors != null && backStyle.colors.length > 0 ) {
//处理渐变
GradientDrawable drawable = new GradientDrawable (
GradientDrawable.Orientation.TOP_BOTTOM, backStyle.colors );
drawable.setCornerRadii ( radius );
target.setBackground ( drawable );
} else {
//处理普通样式
if ( backStyle.normalColor != Integer.MAX_VALUE ) {
GradientDrawable normal = new GradientDrawable ( );
normal.setColor ( backStyle.normalColor );
normal.setCornerRadii ( radius );
normal.setStroke ( backStyle.borderWidth, backStyle.borderColor );
if ( backStyle.selectColor == Integer.MAX_VALUE ) {
target.setBackground ( normal );
} else {
target.setClickable ( true );
GradientDrawable select = new GradientDrawable ( );
select.setColor ( backStyle.selectColor );
select.setCornerRadii ( radius );
select.setStroke ( backStyle.borderWidth, backStyle.borderColor );
StateListDrawable drawable = new StateListDrawable ( );
drawable.addState ( new int[] { android.R.attr.state_selected }, select );
drawable.addState ( new int[] { android.R.attr.state_pressed }, select );
drawable.addState ( new int[] { android.R.attr.state_enabled }, normal );
drawable.addState ( new int[] { }, normal );
target.setBackground ( drawable );
}
}
}
}
public StateListDrawable get ( ) {
float tl = toFloat ( 1.6, backStyle.topLeft );
float tr = toFloat ( 1.6, backStyle.topRight );
float bl = toFloat ( 1.6, backStyle.bottomLeft );
float br = toFloat ( 1.6, backStyle.bottomRight );
float[] radius = new float[] { tl, tl, tr, tr, br, br, bl, bl };
if ( backStyle.colors != null && backStyle.colors.length > 0 ) {
//处理渐变
GradientDrawable normal = new GradientDrawable (
GradientDrawable.Orientation.TOP_BOTTOM, backStyle.colors );
normal.setCornerRadii ( radius );
StateListDrawable drawable = new StateListDrawable ( );
drawable.addState ( new int[] { android.R.attr.state_selected }, normal );
drawable.addState ( new int[] { android.R.attr.state_pressed }, normal );
drawable.addState ( new int[] { android.R.attr.state_enabled }, normal );
drawable.addState ( new int[] { }, normal );
return drawable;
} else {
//处理普通样式
if ( backStyle.normalColor != Integer.MAX_VALUE ) {
GradientDrawable normal = new GradientDrawable ( );
normal.setColor ( backStyle.normalColor );
normal.setCornerRadii ( radius );
normal.setStroke ( backStyle.borderWidth, backStyle.borderColor );
if ( backStyle.selectColor == Integer.MAX_VALUE ) {
StateListDrawable drawable = new StateListDrawable ( );
drawable.addState ( new int[] { android.R.attr.state_selected }, normal );
drawable.addState ( new int[] { android.R.attr.state_pressed }, normal );
drawable.addState ( new int[] { android.R.attr.state_enabled }, normal );
drawable.addState ( new int[] { }, normal );
return drawable;
} else {
GradientDrawable select = new GradientDrawable ( );
select.setColor ( backStyle.selectColor );
select.setCornerRadii ( radius );
select.setStroke ( backStyle.borderWidth, backStyle.borderColor );
StateListDrawable drawable = new StateListDrawable ( );
drawable.addState ( new int[] { android.R.attr.state_selected }, select );
drawable.addState ( new int[] { android.R.attr.state_pressed }, select );
drawable.addState ( new int[] { android.R.attr.state_enabled }, normal );
drawable.addState ( new int[] { }, normal );
return drawable;
}
}
}
return null;
}
public static class ForeStyle {
protected ForeStyle ( ) {
}
protected int normalColor = Integer.MAX_VALUE;
protected int selectColor = Integer.MAX_VALUE;
protected int[] colors;
public ForeStyle setColor ( int normalColor, int selectColor ) {
this.normalColor = normalColor;
this.selectColor = selectColor;
return this;
}
public ForeStyle setColor ( String normalColor, String selectColor ) {
this.normalColor = colorParse ( normalColor );
this.selectColor = colorParse ( selectColor );
return this;
}
public ForeStyle setColorRes ( int normalColor, int selectColor ) {
this.normalColor = colorByRes ( normalColor );
this.selectColor = colorByRes ( selectColor );
return this;
}
//==========================================================
public ForeStyle setColor ( int normalColor ) {
this.normalColor = normalColor;
return this;
}
public ForeStyle setColor ( String normalColor ) {
this.normalColor = colorParse ( normalColor );
return this;
}
public ForeStyle setColorRes ( int normalColor ) {
this.normalColor = colorByRes ( normalColor );
return this;
}
//=====================================================
public ForeStyle setColors ( int... colors ) {
this.colors = colors;
return this;
}
}
public static class BackStyle {
protected int topLeft, topRight, bottomLeft, bottomRight;
protected int borderColor = Integer.MAX_VALUE;
protected int borderWidth;
protected BackStyle ( ) {
}
protected int normalColor = Integer.MAX_VALUE;
protected int selectColor = Integer.MAX_VALUE;
protected int[] colors;
public BackStyle setColor ( int normalColor, int selectColor ) {
this.normalColor = normalColor;
this.selectColor = selectColor;
return this;
}
public BackStyle setColor ( String normalColor, String selectColor ) {
this.normalColor = colorParse ( normalColor );
this.selectColor = colorParse ( selectColor );
return this;
}
public BackStyle setColorRes ( int normalColor, int selectColor ) {
this.normalColor = colorByRes ( normalColor );
this.selectColor = colorByRes ( selectColor );
return this;
}
//==========================================================
public BackStyle setColor ( int normalColor ) {
this.normalColor = normalColor;
return this;
}
public BackStyle setColor ( String normalColor ) {
this.normalColor = colorParse ( normalColor );
return this;
}
public BackStyle setColorRes ( int normalColor ) {
this.normalColor = colorByRes ( normalColor );
return this;
}
//=====================================================
public BackStyle setColors ( int... colors ) {
this.colors = colors;
return this;
}
public BackStyle setRadius ( int radius ) {
this.topLeft = radius;
this.topRight = radius;
this.bottomLeft = radius;
this.bottomRight = radius;
return this;
}
public BackStyle setRadius ( int topLeft, int topRight, int bottomLeft, int bottomRight ) {
this.topLeft = topLeft;
this.topRight = topRight;
this.bottomLeft = bottomLeft;
this.bottomRight = bottomRight;
return this;
}
public BackStyle setBorderColor ( int borderColor, int borderWidth ) {
this.borderColor = borderColor;
this.borderWidth = borderWidth;
return this;
}
public BackStyle setBorderColorRes ( int borderColor, int borderWidth ) {
this.borderColor = colorByRes ( borderColor );
this.borderWidth = borderWidth;
return this;
}
public BackStyle setBorderColor ( String borderColor, int borderWidth ) {
this.borderColor = colorParse ( borderColor );
this.borderWidth = borderWidth;
return this;
}
}
private static Float toFloat ( double value, double per ) {
return Double.valueOf ( value * per ).floatValue ( );
}
private static int colorParse ( String str ) {
try {
return Color.parseColor ( str );
} catch ( Exception e ) {
return Color.LTGRAY;
}
}
private static int colorByRes ( int resId ) {
try {
return ContextCompat.getColor ( BaseApp.getContext ( ), resId );
} catch ( Exception e ) {
return Color.LTGRAY;
}
}