美文网首页Android开发
《第二行代码进阶》如何避免写过多的drawable作为背景

《第二行代码进阶》如何避免写过多的drawable作为背景

作者: 你的益达233 | 来源:发表于2021-09-07 15:46 被阅读0次
类似下面的button的背景
btn1.png
btn2.png
是不是第一时间想到的是去写shape来作为背景,是可以这样做,但是你会发现你的shape会越写越多。项目也跟着乱了起来。

那有没有办法解决这一问题呢

ShapeUtils来了

用代码的方式解决,直接拿去用吧!

public class ShapeUtils {

/**
 * 设置 shape 的颜色
 * @param view
 * @param solidColor
 */
public static void setShapeColor(View view, int solidColor){
    if(view == null){
        return;
    }
    GradientDrawable gradientDrawable = new GradientDrawable();
    gradientDrawable.setColor(solidColor);
    view.setBackground(gradientDrawable);
}

/**

 * desc : 设置圆角
 **/
public static void setShapeCorner(View view, float corner){
    if(view == null){
        return;
    }
    GradientDrawable gradientDrawable = new GradientDrawable();
    gradientDrawable.setCornerRadius(corner);
    view.setBackground(gradientDrawable);
}

/**
 * 设置shape倒角和颜色
 * @param view
 * @param solidColor
 * @param corner
 */
public static void setShapeCorner2Color(View view, int solidColor, float corner){
    if(view == null){
        return;
    }
    GradientDrawable gradientDrawable = new GradientDrawable();
    gradientDrawable.setColor(solidColor);
    gradientDrawable.setCornerRadius(corner);
    view.setBackground(gradientDrawable);
}

/**
 * 设置shape倒角 颜色 和描边颜色和大小
 * @param view
 * @param solidColor
 * @param corner
 */
public static void setShapeCorner2Color2Stroke(View view, int solidColor, float corner,int strokeColor,int strokeWidth){
    if(view == null){
        return;
    }
    GradientDrawable gradientDrawable = new GradientDrawable();
    gradientDrawable.setColor(solidColor);
    gradientDrawable.setCornerRadius(corner);
    gradientDrawable.setStroke(strokeWidth, strokeColor);
    view.setBackground(gradientDrawable);
}

/**
 * 设置矩形渐变,同时shape渐变类型只能是线性从上倒下,颜色数组中的顺序即是渐变顺序
 * @param view
 * @param colors
 */
public static void setShapeGradient(View view, int[] colors){
    if(view == null){
        return;
    }
    if(colors.length > 3){
        return;
    }
    GradientDrawable gradientDrawable = new GradientDrawable();
    gradientDrawable.setGradientType(LINEAR_GRADIENT);
    gradientDrawable.setColors(colors);
    view.setBackground(gradientDrawable);
}

/**
 * 设置矩形渐变
 * @param view
 * @param colors
 * @param gradientType 渐变类型
 */
public static void setShapeGradient(View view, int[] colors,int gradientType){
    if(view == null){
        return;
    }
    if(colors.length > 3){
        return;
    }
    GradientDrawable gradientDrawable = new GradientDrawable();
    gradientDrawable.setGradientType(gradientType);
    gradientDrawable.setColors(colors);
    view.setBackground(gradientDrawable);
}

/**
 * 设置圆角渐变,同时shape渐变类型只能是线性从左倒右,颜色数组中的顺序即是渐变顺序
 * @param view
 * @param colors
 */
public static void setShapeGradientAndRound(View view, int[] colors, float corner){
    if(view == null){
        return;
    }
    if(colors.length > 3){
        return;
    }
    GradientDrawable gradientDrawable = new GradientDrawable();
    // 设置颜色渐变方向
    gradientDrawable.setOrientation(LEFT_RIGHT);
    gradientDrawable.setColors(colors);
    gradientDrawable.setCornerRadius(corner);
    view.setBackground(gradientDrawable);
}

/**
 * 设置圆角渐变,颜色数组中的顺序即是渐变顺序
 * orientation : 渐变的方向
 */
public static void setShapeGradientAndRound(View view, int[] colors, GradientDrawable.Orientation orientation, float corner){
    if(view == null){
        return;
    }
    if(colors.length > 3){
        return;
    }
    GradientDrawable gradientDrawable = new GradientDrawable();
    // 设置颜色渐变方向
    gradientDrawable.setOrientation(orientation);
    gradientDrawable.setColors(colors);
    gradientDrawable.setCornerRadius(corner);
    view.setBackground(gradientDrawable);
}

/**
 
 * desc : 圆角渐变,方向是从上到下,圆角可任意其中一个或多个
 * radii: 四个角八个数,每二个一个角,从左上顺时针
 **/
public static void setShapeGradientAndRound(View view, int[] colors, float[] radii){
    if(view == null){
        return;
    }
    if(colors.length > 3){
        return;
    }
    GradientDrawable gradientDrawable = new GradientDrawable();
    // 设置颜色渐变方向
    gradientDrawable.setOrientation(TOP_BOTTOM);
    gradientDrawable.setColors(colors);
    gradientDrawable.setCornerRadii(radii);
    view.setBackground(gradientDrawable);
}

/**
 
 * desc : 圆角渐变,圆角可任意其中一个或多个
 * radii: 四个角八个数,每二个一个角,从左上顺时针
 **/
public static void setShapeGradientAndRound(View view, int[] colors, float[] radii, GradientDrawable.Orientation orientation){
    if(view == null){
        return;
    }
    if(colors.length > 3){
        return;
    }
    GradientDrawable gradientDrawable = new GradientDrawable();
    // 设置颜色渐变方向
    gradientDrawable.setOrientation(orientation);
    gradientDrawable.setColors(colors);
    gradientDrawable.setCornerRadii(radii);
    view.setBackground(gradientDrawable);
}

/**
 
 * desc: 设置圆角加带描边
**/
public static void setShapeRoundAndEdge(View view,int lineWide,int lineColor, int solidColor, float corner){
    GradientDrawable gradientDrawable = new GradientDrawable();
    gradientDrawable.setStroke(lineWide,lineColor);
    if(solidColor != 0){
        gradientDrawable.setColor(solidColor);
    }
    gradientDrawable.setCornerRadius(corner);
    view.setBackground(gradientDrawable);
}

/**
 
 * desc: 分别设置四个圆角加带描边(没描边可传0)
 **/
public static void setShapeRoundAndEdge(View view,int lineWide,int lineColor, int solidColor, float[] corner){
    GradientDrawable gradientDrawable = new GradientDrawable();
    if(lineWide != 0 && lineColor != 0){
        gradientDrawable.setStroke(lineWide,lineColor);
    }
    if(solidColor != 0){
        gradientDrawable.setColor(solidColor);
    }
    gradientDrawable.setCornerRadii(corner);
    view.setBackground(gradientDrawable);
}
}

使用示例:

ShapeUtils.setShapeCorner2Color(rcl, context.getResources().getColor(R.color.white), Utils.dp2px(context, 12));

    ShapeUtils.setShapeGradientAndRound(btnBatchGreet, new int[]{context.getResources().getColor(R.color.c_40d6bd), context.getResources().getColor(R.color.c_33c1cb)}, Utils.dp2px(context, 4));  

这样写还不用想见shape.xml的文件名,轻松多了

相关文章

网友评论

    本文标题:《第二行代码进阶》如何避免写过多的drawable作为背景

    本文链接:https://www.haomeiwen.com/subject/omhpwltx.html