博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android开发 GradientDrawable详解
阅读量:4652 次
发布时间:2019-06-09

本文共 4244 字,大约阅读时间需要 14 分钟。

前言

  GradientDrawable类似与Xml布局里的shape,常用在一些自己封装的对话框控件的背景或者其他View中,优势是不需要你在带着xml布局文件一起封包..

画线

GradientDrawable gradientDrawable = new GradientDrawable();        gradientDrawable.setShape(GradientDrawable.LINE);        gradientDrawable.setStroke(5, Color.YELLOW);//线的宽度 与 线的颜色        mTextView.setBackground(gradientDrawable);

效果图:

画虚线

mTextView.setLayerType(View.LAYER_TYPE_SOFTWARE,null); //要显示虚线一定要关闭硬件加速        GradientDrawable gradientDrawable = new GradientDrawable();        gradientDrawable.setShape(GradientDrawable.LINE);        gradientDrawable.setStroke(1, Color.BLACK, 10, 10);//第一个参数为线的宽度  第二个参数是线的颜色 第三个参数是虚线段的长度 第四个参数是虚线段之间的间距长度        mTextView.setBackground(gradientDrawable);

也可以在布局里关闭指定view的硬件加速

android:layerType="software"

效果图:

画圆

GradientDrawable gradientDrawable = new GradientDrawable();        gradientDrawable.setShape(GradientDrawable.OVAL);        gradientDrawable.setColor(Color.BLUE);        gradientDrawable.setSize(50,50);        mTextView.setBackground(gradientDrawable);

效果图:

画圆环

GradientDrawable gradientDrawable = new GradientDrawable();        gradientDrawable.setShape(GradientDrawable.OVAL);        gradientDrawable.setColor(Color.BLUE);        gradientDrawable.setStroke(10,Color.YELLOW);        gradientDrawable.setSize(50,50);        mTextView.setBackground(gradientDrawable);

效果图:

圆角矩形

GradientDrawable gradientDrawable = new GradientDrawable();        gradientDrawable.setShape(GradientDrawable.RECTANGLE);        gradientDrawable.setColor(Color.RED);        gradientDrawable.setStroke(10,Color.BLUE);        gradientDrawable.setCornerRadius(10);        gradientDrawable.setSize(50,50);        mTextView.setBackground(gradientDrawable);

效果图:

虚线矩形

GradientDrawable gradientDrawable = new GradientDrawable();        gradientDrawable.setShape(GradientDrawable.LINEAR_GRADIENT);        gradientDrawable.setStroke(1, Color.GREEN,30, 30);        mTextView.setBackground(gradientDrawable);

效果图:

颜色渐变

线性渐变

int[] colors = {Color.YELLOW, Color.GREEN, Color.BLUE};        GradientDrawable gradientDrawable = new GradientDrawable();        gradientDrawable.setShape(GradientDrawable.RECTANGLE);        gradientDrawable.setColors(colors); //添加颜色组       gradientDrawable.setGradientType(GradientDrawable.LINEAR_GRADIENT);//设置线性渐变        gradientDrawable.setSize(50,50);        mTextView.setBackground(gradientDrawable);

效果图:

改变线性渐变方向

int[] colors = {Color.YELLOW, Color.GREEN, Color.BLUE};        GradientDrawable gradientDrawable = new GradientDrawable();        gradientDrawable.setShape(GradientDrawable.RECTANGLE);        gradientDrawable.setColors(colors); //添加颜色组        gradientDrawable.setGradientType(GradientDrawable.LINEAR_GRADIENT);//设置线性渐变        gradientDrawable.setOrientation(GradientDrawable.Orientation.RIGHT_LEFT);//设置渐变方向        gradientDrawable.setSize(50,50);        mTextView.setBackground(gradientDrawable);

效果图:

半径渐变

int[] colors = {Color.YELLOW, Color.GREEN, Color.BLUE};        GradientDrawable gradientDrawable = new GradientDrawable();        gradientDrawable.setShape(GradientDrawable.RECTANGLE);        gradientDrawable.setColors(colors); //添加颜色组        gradientDrawable.setGradientType(GradientDrawable.RADIAL_GRADIENT);//设置半径渐变        gradientDrawable.setGradientRadius(50);//渐变的半径值        gradientDrawable.setSize(50,50);        mTextView.setBackground(gradientDrawable);

效果图:

扫描渐变

int[] colors = {Color.YELLOW, Color.GREEN, Color.BLUE};        GradientDrawable gradientDrawable = new GradientDrawable();        gradientDrawable.setShape(GradientDrawable.RECTANGLE);        gradientDrawable.setColors(colors); //添加颜色组        gradientDrawable.setGradientType(GradientDrawable.SWEEP_GRADIENT);//设置扫描渐变        gradientDrawable.setGradientCenter(0.5f,0.5f);//渐变中心点        gradientDrawable.setSize(50,50);        mTextView.setBackground(gradientDrawable);

防抖

gradientDrawable.setDither(true);

可以让渐变的时候颜色阶梯降低,变得更柔和

 

透明度

GradientDrawable gradientDrawable = new GradientDrawable();        gradientDrawable.setShape(GradientDrawable.RECTANGLE);        gradientDrawable.setColor(Color.YELLOW);         gradientDrawable.setAlpha(70);//设置透明度        mTextView.setBackground(gradientDrawable);

 

posted on
2019-07-06 14:21  阅读(
...) 评论(
...) 收藏

转载于:https://www.cnblogs.com/guanxinjing/p/11142599.html

你可能感兴趣的文章
[转]Linux命令的返回值
查看>>
NSTimeZone
查看>>
gsp 换行
查看>>
【DWM1000】 code 解密8一 TAG接收blink response 信号
查看>>
添加跟反射
查看>>
uva 1451(树形结合)
查看>>
在C#环境中动态调用IronPython脚本(二)
查看>>
ASP.Net 重写IHttpModule 来拦截 HttpApplication 实现HTML资源压缩和空白过滤
查看>>
javascript时间格式转换
查看>>
wireshark in ubuntu
查看>>
为什么利率上升,债券价格下降?
查看>>
Linux常用的网络命令
查看>>
mysql的唯一索引
查看>>
Notepad++
查看>>
c语言第一次作业--分支 顺序结构
查看>>
一个PPT转H5的小工具
查看>>
java string 全角半角转换
查看>>
spring-boot:run启动报错
查看>>
多入参加法
查看>>
js 表单非空验证
查看>>