Android LinearGradient线性渐变

1 linearGradient简介

linearGradient线性渐变,会用到Paint的setShader,Shader 被称为着色器,在opengl中这个概念经常被用到,android中的shader主要用来给图像、文字着色,Shader在绘制过程中会返回横向重要的颜色组,Paint设置shader后,绘制时会从shader中获取颜色,也就是需要shader告诉画笔某处的颜色值。

Shader 具体实现类包括:
BitmapShader,ComposeShader,LinearGradient,RadialGradient,SweepGradient

LinearGradient两种构造函数:

/**
 * Create a shader that draws a linear gradient along a line.
 *
 * @param x0       The x-coordinate for the start of the gradient line
 * @param y0       The y-coordinate for the start of the gradient line
 * @param x1       The x-coordinate for the end of the gradient line
 * @param y1       The y-coordinate for the end of the gradient line
 * @param color0   The color at the start of the gradient line.
 * @param color1   The color at the end of the gradient line.
 * @param tile     The Shader tiling mode
*/
public LinearGradient(float x0, float y0, float x1, float y1,
    @ColorInt int color0, @ColorInt int color1, @NonNull TileMode tile) ;


/**
 * Create a shader that draws a linear gradient along a line.
 *
 * @param x0           The x-coordinate for the start of the gradient line
 * @param y0           The y-coordinate for the start of the gradient line
 * @param x1           The x-coordinate for the end of the gradient line
 * @param y1           The y-coordinate for the end of the gradient line
 * @param colors       The colors to be distributed along the gradient line
 * @param positions    May be null. The relative positions [0..1] of
 *                     each corresponding color in the colors array. If this is null,
 *                     the the colors are distributed evenly along the gradient line.
 * @param tile         The Shader tiling mode
*/
public LinearGradient(float x0, float y0, float x1, float y1, @NonNull @ColorInt int colors[], @Nullable float positions[], @NonNull TileMode tile) ;

参数说明:
(x0,y0):渐变起始点坐标
(x1,y1):渐变结束点坐标
color0:渐变开始点颜色,16进制的颜色表示,必须要带有透明度
color1:渐变结束颜色
colors:渐变数组
positions:位置数组,position的取值范围[0,1],作用是指定某个位置的颜色值,如果传null,渐变就线性变化。
tile:用于指定控件区域大于指定的渐变区域时,空白区域的颜色填充方法。

  • CLAMP边缘拉伸,为被shader覆盖区域,使用shader边界颜色进行填充
    -REPEAT 在水平和垂直两个方向上重复,相邻图像没有间隙
    -MIRROR以镜像的方式在水平和垂直两个方向上重复,相邻图像有间隙

第一个构造函数可以指定两个颜色之间的渐变,第二个构造函数可以指定多个颜色之间的渐变,线性渐变不但可以代码实现还可以xml文件实现,这里只讲解代码实现方式。

2 两种颜色的线性渐变

只需要设置开始结束点坐标,开始颜色,结束颜色。
实例代码:

mPaint = new Paint();
mPaint.setColor(Color.BLUE);
mPaint.setAntiAlias(true);
mPaint.setStrokeWidth(3);
mPaint.setStyle(Paint.Style.FILL);
mPaint.setTextSize(20);

LinearGradient linearGradient = new LinearGradient(getWidth(),400,0,0,Color.RED,Color.GREEN, Shader.TileMode.CLAMP);
mPaint.setShader(linearGradient);
canvas.drawRect(0,0,getWidth(),400,mPaint);

xml中设置渐变可以通过设置angle角度来改变渐变的开始结束,可以设置从上到下,从下到上,从左到右,从右到左,代码中如何设置呢?

3 如何通过坐标设置渐变方向:

通过坐标可以轻松实现,渐变方向的控制:
(0,0)->(0,400)从上到下
(0,400)->(0,0) 从下到上

0,0)->(getMeasuredWidth(),0) 表示从左到右
(getMeasuredWidth(),0)->(0,0) 表示从右到左

0,0)-> (getMeasuredWidth(),getMeasuredHeight()) 斜角,从左上角到右下角

从左到右:

 

从右到左:

 

** 渐变填充颜色总结**

  • 要实现从上到下需要设置shader开始结束点坐标为左上角到左下角或右上角到右下角坐标。

  • 要实现从下到上需要设置shader开始结束点坐标为左下角到左上角或右下角到右上角。

  • 要实现从左到右需要设置shader开始结束点坐标为左上角到右上角或者左下角到右下角。

  • 要实现从右到左需要设置shader开始结束坐标为右上角到左上角或者右下角到左下角。

  • 要实现对角shader,需要设置开始结束点坐标左上角到右下角。

4 多颜色填充 colors,positions数组参数讲解

LinearGradient(float x0, float y0, float x1, float y1, @NonNull @ColorInt int colors[], @Nullable float positions[], @NonNull TileMode tile) ;

positions为null时,线性填充,和没有positions数组的构造函数效果一样。

Positions数组中值为0-1,0表示开始绘制点,1表示结束点,0.5对应中间点等等。数组中位置信息对应颜色数组中的颜色。
//例如
int [] colors = {Color.RED,Color.GREEN, Color.BLUE};
float[] position = {0f, 0.3f, 1.0f};
上面position[0]对应数组中的第一个RED,0.3f的位置对应颜色中的GREEN,1.0f的位置对应颜色中的BLUE,所以从0-0.3的位置是从RED到GREEN的渐变,从0.3到1.0的位置的颜色渐变是GREEN到BLUE。

int [] colors = {Color.RED,Color.GREEN, Color.BLUE};
float[] position = {0f, 0.3f, 1.0f};
LinearGradient linearGradient = new LinearGradient(0,0,getMeasuredWidth(),0,colors,position, Shader.TileMode.CLAMP);

mPaint.setShader(linearGradient);
canvas.drawRect(0,0,getMeasuredWidth(),getMeasuredHeight(),mPaint);

 

如果把0.3改成0.7:

 

5 利用LinearGradient实现变色字体

利用设置了变色shader的画笔,就可以画出变色字体:

int [] colors = {Color.RED,Color.GREEN, Color.BLUE};
float[] position = {0f, 0.7f, 1.0f};
LinearGradient linearGradient = new LinearGradient(0,0,getMeasuredWidth(),0,colors,position, Shader.TileMode.CLAMP);

mPaint.setShader(linearGradient);
// canvas.drawRect(0,0,getMeasuredWidth(),getMeasuredHeight(),mPaint);
canvas.drawText("Android绘图小糊涂",0,getMeasuredHeight()/2,mPaint);

 

如何让字体颜色不停地变动:
Shader 可以设置matrix变换,利用translate不停地移动shader,实现渐变效果,下面的实例不能用于生产环境,我只是写个例子,后面会开文章讲解可用于生产的渐变。

int [] colors = {Color.BLACK,Color.RED, Color.BLUE,Color.BLACK};
Rect rect = new Rect();
mPaint.getTextBounds(str,0,str.length(), rect);
int fontWidth = rect.width();
linearGradient = new LinearGradient(0,0,-fontWidth+10,0,colors,null, Shader.TileMode.CLAMP);
Matrix matrix = new Matrix();
matrix.setTranslate(tran,0);
linearGradient.setLocalMatrix(matrix);
tran = (tran + advance) ;
if (tran >= fontWidth*2){
   tran = 0;
}
mPaint.setShader(linearGradient);
canvas.drawText(str,0,getMeasuredHeight()/2,mPaint);
invalidate();

 

 

TileMode 边缘填充模式

如果shader的大小小于view的大小时如何绘制其他没有被shader覆盖的区域?
跟最后一个参数有关,
-CLAMP
边缘拉伸,利用边缘的颜色,填充剩余部分
-REPEAT
在水平和垂直两个方向上重复,相邻图像没有间隙,重复shader
-MIRROR
以镜像的方式在水平和垂直两个方向上重复,相邻图像有间隙,镜面shader

LinearGradient linearGradient = new LinearGradient(0,0,getMeasuredWidth()/2,getMeasuredHeight()/2,Color.RED,Color.GREEN, Shader.TileMode.CLAMP);
mPaint.setShader(linearGradient);
canvas.drawRect(0,0,getMeasuredWidth(),getMeasuredHeight(),mPaint);

CLAMP:

 

 

REPEAT:

 

MIRROR:

 

 

 

 

如果想要从对角线设置shader,图形最好是正方形这样比较好看设置:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
   int width =  MeasureSpec.getSize(widthMeasureSpec);
    setMeasuredDimension(width,width);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    LinearGradient linearGradient = new LinearGradient(0,0,getMeasuredWidth()/2,getMeasuredHeight()/2,Color.RED,Color.GREEN, Shader.TileMode.MIRROR);
    mPaint.setShader(linearGradient);
    canvas.drawRect(0,0,getMeasuredWidth(),getMeasuredHeight(),mPaint);
}

 

 

 

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
封面 1 序 2 捐助说明 5 目 录 7 第一章 View的绘图流程 12 1.1、概述 12 1.2、Activity的组成结构 13 1.3、View树的绘图流程 15 1.3.1 测量组件大小 16 1.3.2 确定子组件的位置 17 1.3.3 绘制组件 18 1.4、说点别的 22 1.5 练习作业 22 第二章 Graphics2D API 23 2.1、概述 23 2.2、Point类和PointF类 23 2.3、Rect类和RectF类 25 2.4、Bitmap类和BitmapDrawable类 32 2.5、Canvas类与Paint类 34 2.5.1 绘图概述 34 2.5.2 Paint类 34 2.5.3 Canvas类 39 2.6 练习作业 63 第三章 使用Graphics2D实现动态效果 64 3.1 概述 64 3.2 invalidate()方法 65 3.3 坐标转换 69 3.4 剪切区(Clip) 73 3.5 案例:指针走动的手表 82 3.6 练习作业 88 第四章 双缓存技术 89 4.1 双缓存 89 4.2 在屏幕上绘制曲线 90 4.3 在屏幕上绘制矩形 99 4.4 案例:绘图App 104 4.4.1 绘图属性 106 4.4.2 软件参数 108 4.4.3 绘图缓冲区 109 4.4.4 撤消操作 111 4.4.5 图形绘制 113 4.4.6 绘图区 118 4.4.7 主界面 119 4.5 练习作业 122 第五章 阴影、渐变和位图运算 123 5.1 概述 123 5.2 阴影 123 5.3 渐变 125 5.3.1 线性渐变LinearGradient) 126 5.3.2 径向渐变(RadialGradient) 130 5.3.3 扫描渐变(SweepGradient) 135 5.3.4 位图渐变(BitmapShader) 138 5.3.5 混合渐变(ComposeShader) 140 5.3.6 渐变与Matrix 142 5.4 位图运算 143 5.4.1 PorterDuffXfermode 143 5.4.2 图层(Layer) 146 5.4.3 位图运算技巧 148 5.5 案例1:圆形头像 152 5.6 案例2:刮刮乐 156 5.7 练习作业 161 第六章 自定义组件 163 6.1 概述 163 6.2 自定义组件的基本结构 164 6.3 重写onMeasure方法 166 6.4 组件属性 175 6.4.1 属性的基本定义 175 6.4.2 读取来自style和theme中的属性 181 6.5 案例1:圆形ImageView组件 186 6.6 案例2:验证码组件CodeView 190 6.7 练习作业 202 第七章 自定义容器 204 7.1 概述 204 7.2 ViewGroup类 205 7.2.1 ViewGroup常用方法 205 7.2.2 ViewGroup的工作原理 208 7.2.3 重写onLayout()方法 213 7.3 CornerLayout布局 217 7.3.1 基本实现 217 7.3.2 内边距padding 224 7.3.3 外边距margin 228 7.3.4 自定义LayoutParams 238 7.4 案例:流式布局(FlowLayout) 246 7.5 练习作业 256 第八章 Scroller与平滑滚动 257 8.1 概述 257 8.2 认识scrollTo()和scrollBy()方法 258 8.3 Scroller类 264 8.4 平滑滚动的工作原理 271 8.5 案例:触摸滑屏 272 8.5.1 触摸滑屏的技术分析 272 8.5.2 速度跟踪器VelocityTracker 273 8.5.3 触摸滑屏的分步实现 274 8.6 练习作业 285 第九章 侧边栏 287 9.1 概述 287 9.2 使用二进制保存标识数据 289 9.2.1 位运算符 289 9.2.2 位运算的常用功能 292 9.3 继承自ViewGroup的侧边栏 293 9.4 继承自HorizontalScrollView的侧边栏 304 9.5 练习作业 312 第十章 加强版ListView 313 10.1 概述 313 10.2 ListView的基本使用 314 10.3 ListItem随手指左右滑动 318 10.4 向右滑动删除ListItem 326 10.5 滑动ListItem出现删除按钮 336 10.5.1 列表项专用容器ExtendLayout 337 10.5.2 列表项能滑出删除按钮的ListView 342 10.5.3 定义布局文件 350 10.5.4 显示ListView 351 10.6练习作业 353 案例代码说明 354

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值