Android代码处理视图圆角和色彩的工具类

编程入门 行业动态 更新时间:2024-10-06 21:31:19

Android代码处理<a href=https://www.elefans.com/category/jswz/34/1770164.html style=视图圆角和色彩的工具类"/>

Android代码处理视图圆角和色彩的工具类

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地址:

更多推荐

Android代码处理视图圆角和色彩的工具类

本文发布于:2024-03-23 20:46:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1742635.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   圆角   色彩   代码   工具

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!