Android处理视图圆角和色彩的工具类
程序员文章站
2022-07-04 17:02:32
android代码处理视图圆角和色彩的工具类,供大家参考,具体内容如下一直都用的.xml文件处理圆角与色彩或色彩渐变,觉得很不方便,后来发现了gradientdrawable这个类,就整了个工具类,用...
android代码处理视图圆角和色彩的工具类,供大家参考,具体内容如下
一直都用的.xml文件处理圆角与色彩或色彩渐变,觉得很不方便,后来发现了gradientdrawable这个类,就整了个工具类,用起来觉得挺方便
效果图:
下面贴代码:
public class drawcorner { /** * 默认绘制圆角函数,背景白色,半径20 * */ public static gradientdrawable drawcorner() { gradientdrawable drawable = new gradientdrawable(); drawable.setcornerradius(20); drawable.setcolor(color.white); return drawable; } /** * 默认绘制圆角函数,背景白色 * 参数: * radius:圆角半径 * */ public static gradientdrawable drawcorner(int radius) { gradientdrawable drawable = new gradientdrawable(); drawable.setcornerradius(radius); drawable.setcolor(color.white); return drawable; } /** * 默认绘制圆角函数,背景白色 * 参数: * radii:四个圆角半径,依次是左上x、左上y、右上x、右上y、右下x、右下y、左下x、左下y * */ public static gradientdrawable drawcorner(float[] radii) { if (radii == null || radii.length != 8) { radii = new float[]{0, 0, 0, 0, 0, 0, 0, 0}; } gradientdrawable drawable = new gradientdrawable(); drawable.setcornerradii(radii); drawable.setcolor(color.white); return drawable; } /** * 默认绘制圆角函数 * 参数: * radius:圆角半径 * color:背景色 * */ public static gradientdrawable drawcorner(int radius, int color) { gradientdrawable drawable = new gradientdrawable(); drawable.setcornerradius(radius); drawable.setcolor(color); return drawable; } /** * 默认绘制圆角函数 * 参数: * radii:四个圆角半径,依次是左上x、左上y、右上x、右上y、右下x、右下y、左下x、左下y * color:背景色 * */ public static gradientdrawable drawcorner(float[] radii, int color) { if (radii == null || radii.length != 8) { radii = new float[]{0, 0, 0, 0, 0, 0, 0, 0}; } gradientdrawable drawable = new gradientdrawable(); drawable.setcornerradii(radii); drawable.setcolor(color); return drawable; } /** * 默认绘制圆角函数 * 参数: * radius:圆角半径 * colors:渐变背景色 * */ public static gradientdrawable drawcorner(int radius, int[] colors) { gradientdrawable drawable = new gradientdrawable(); drawable.setcornerradius(radius); drawable.setgradienttype(gradientdrawable.linear_gradient); drawable.setcolors(colors); return drawable; } /** * 默认绘制圆角函数 * 参数: * radii:四个圆角半径,依次是左上x、左上y、右上x、右上y、右下x、右下y、左下x、左下y * colors:渐变背景色 * */ public static gradientdrawable drawcorner(float[] radii, int[] colors) { if (radii == null || radii.length != 8) { radii = new float[]{0, 0, 0, 0, 0, 0, 0, 0}; } gradientdrawable drawable = new gradientdrawable(); drawable.setcornerradii(radii); drawable.setgradienttype(gradientdrawable.linear_gradient); drawable.setcolors(colors); return drawable; } /** * 默认绘制圆角函数 * 参数: * radius:圆角半径 * colors:渐变背景色 * orientation:渐变类型( 0:上到下,1:右上到左下,2:右到左,3:右下到左上,4:下到上,5:左下到右上,6:左到右,7:左上到右下 ) * */ public static gradientdrawable drawcorner(int radius, int[] colors, int orientation) { gradientdrawable drawable = new gradientdrawable(); drawable.setcornerradius(radius); drawable.setgradienttype(gradientdrawable.linear_gradient); switch (orientation) { case 0: drawable.setorientation(gradientdrawable.orientation.top_bottom); break; case 1: drawable.setorientation(gradientdrawable.orientation.tr_bl); break; case 2: drawable.setorientation(gradientdrawable.orientation.right_left); break; case 3: drawable.setorientation(gradientdrawable.orientation.br_tl); break; case 4: drawable.setorientation(gradientdrawable.orientation.bottom_top); break; case 5: drawable.setorientation(gradientdrawable.orientation.bl_tr); break; case 6: drawable.setorientation(gradientdrawable.orientation.left_right); break; case 7: drawable.setorientation(gradientdrawable.orientation.tl_br); break; } drawable.setcolors(colors); return drawable; } /** * 默认绘制圆角函数 * 参数: * radii:四个圆角半径,依次是左上、右上、右下、左下 * colors:渐变背景色 * orientation:渐变类型( 0:上到下,1:右上到左下,2:右到左,3:右下到左上,4:下到上,5:左下到右上,6:左到右,7:左上到右下 ) * */ public static gradientdrawable drawcorner(float[] radii, int[] colors, int orientation) { if (radii == null || radii.length != 8) { radii = new float[]{0, 0, 0, 0, 0, 0, 0, 0}; } gradientdrawable drawable = new gradientdrawable(); drawable.setcornerradii(radii); drawable.setgradienttype(gradientdrawable.linear_gradient); switch (orientation) { case 0: drawable.setorientation(gradientdrawable.orientation.top_bottom); break; case 1: drawable.setorientation(gradientdrawable.orientation.tr_bl); break; case 2: drawable.setorientation(gradientdrawable.orientation.right_left); break; case 3: drawable.setorientation(gradientdrawable.orientation.br_tl); break; case 4: drawable.setorientation(gradientdrawable.orientation.bottom_top); break; case 5: drawable.setorientation(gradientdrawable.orientation.bl_tr); break; case 6: drawable.setorientation(gradientdrawable.orientation.left_right); break; case 7: drawable.setorientation(gradientdrawable.orientation.tl_br); break; } drawable.setcolors(colors); return drawable; } }
调用方式很简单,就是
view.setbackground(drawcorner.drawcorner);
就行了
例如:
textview textview = findviewbyid(r.id.test); textview.setbackground(drawcorner.drawcorner());
github地址
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。