Android开发 GradientDrawable详解

前言

GradientDrawable 支持渐变色的Drawable,与shapeDrawable在画型上是类似的,多了支持渐变色。代码上的GradientDrawable比在xml里的shape下gradient属性强大的多,因为shape下gradient属性只支持三色阶渐变,而GradientDrawable可以有更多的色阶渐变。

画线

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);

在这里插入图片描述

本文来自博客园,作者:{观心静},转载请注明原文链接:{https://www.cnblogs.com/guanxinjing/p/11142599.html}

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值