安卓支持三种颜色渐变:线性渐变、径向渐变和渐变。 所有这三种渐变都是从android.graphics.Shader继承的,Paint类通过setShader ()方法支持渐变。
本文介绍了LinearGradient的使用,并看一下LinearGradient构造函数。
构建方法1 (仅支持双色渐变) :
第一参数(浮点x0、浮点y0、浮点x1、浮点y1、int color0、int color1、TileMode tileMode ):x0是渐变
第二个参数: y0表示渐变起点的y轴坐标
第三个参数: x1表示渐变终点的x轴坐标
第四个参数: y1表示渐变终点的y轴坐标
第五个参数: color0表示渐变开始的颜色,颜色值用十六进制表示
第六个参数: color1表示渐变末尾的颜色,颜色值以十六进制表示
第七个参数:如果控制区域大于指定的渐变区域,tileMode将指定其馀区域的颜色填充方式。
TileMode有三种方法: CLAMP、REPEAT和MIRROR。
CLAMP:拉伸并扩展边缘的一个像素
REPEAT:平移复制
MIRROR:镜面翻转
构建方法2 (支持多种颜色渐变) :
publiclineargradient(floatx0,float y0,float x1,float y1,int colors[],float positions[],TileMode tileMode ) {}参数
colors[]是一组渐变颜色值,颜色值以十六进制表示
positions[]是colors[]中多种颜色渐变时每种颜色的缩放位置,范围为0到1
让我们看一下渐变效果,了解这两个构造函数的参数的含义。
贴上代码:
publicclasslineargradientviewextendsview//默认渐变开始颜色(红) privatestaticfinalintdefault _ start _ color=color.parart 默认渐变结束颜色(黄色) privatestaticfinalintdefault _ end _ color=color.parse color (' # ffff 00 ' ); //开始、结束颜色private int mStartColor,mEndColor; //绘制的矩形区域private RectF mRectF; //画笔private Paint mPaint; publiclineargradientview (上下文上下文) this (上下文,null ); } publiclineargradientview (context context,@Nullable AttributeSet attrs ) this ) context,attrs,0 ); } publiclineargradientview (context context,@Nullable AttributeSet attrs,int defStyleAttr ) super ) context,attrs, defstyleattr typedarraytypedarray=context.obtainstyledattributes (attrs,R.styleable.LinearGradientView ); mtar tcolor=typed array.getcolor (r.style able.lineargradientview _ start color,DEFAULT_START_COLOR ); mend color=typed array.getcolor (r.style able.lineargradientview _ end color,DEFAULT_END_COLOR ); typedArray.recycle (; initPaint (; } private void initPaint () { mPaint=new Paint ); mpaint.setantialias(true; mPaint
.setStyle(Paint.Style.FILL); } @Override protected void onSizeChanged(int width, int height, int oldWidth, int oldHeight) { super.onSizeChanged(width, height, oldWidth, oldHeight); mRectF = new RectF(0, 0, width, height); mPaint.setShader(new LinearGradient(0, 0, mRectF.right, 0, mStartColor, mEndColor, Shader.TileMode.MIRROR)); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawRect(mRectF, mPaint); }}一、两种渐变色的情况:
1、从左向右设置渐变色(不管TileMode)
mPaint.setShader(new LinearGradient(0, 0, mRectF.right, 0, mStartColor, mEndColor, Shader.TileMode.MIRROR));
2、从左上角到右下角设置渐变色(不管TileMode)
3、从上到下设置渐变色(不管TileMode)
TileMode的三种模式的理解:
4、TileMode设置为MIRROR,从左向右设置渐变色(x方向的终点为View的中间位置)
mPaint.setShader(new LinearGradient(0, 0, mRectF.right/2, 0, mStartColor, mEndColor, Shader.TileMode.MIRROR));5、TileMode设置为REPEAT,从左向右设置渐变色(x方向的终点为View的中间位置)
mPaint.setShader(new LinearGradient(0, 0, mRectF.right/2, 0, mStartColor, mEndColor, Shader.TileMode.REPEAT));
6、TileMode设置为CLAMP,从左向右设置渐变色(x方向的终点为View的中间位置)
二、多种渐变色的情况
1、从左向右设置渐变色(x方向的终点为View的右边)
int[] colorArray = new int[]{Color.RED,Color.YELLOW,Color.BLUE,Color.GREEN};float[] positionArray = new float[]{0f,0.3f,0.6f,0.9f};mPaint.setShader(new LinearGradient(0, 0, mRectF.right, 0, colorArray, positionArray, Shader.TileMode.REPEAT));2、从左向右设置渐变色(x方向的终点为View的中间位置), tileMode为MIRROR
int[] colorArray = new int[]{Color.RED,Color.YELLOW,Color.BLUE,Color.GREEN}; float[] positionArray = new float[]{0f,0.3f,0.6f,0.9f}; mPaint.setShader(new LinearGradient(0, 0, mRectF.centerX(), 0, colorArray, positionArray, Shader.TileMode.MIRROR));3、看下渐变色文字效果
这里渐变色:
int[] colorArray = new int[]{Color.RED,Color.YELLOW,Color.BLUE,Color.GREEN}; float[] positionArray = new float[]{0f,0.3f,0.6f,0.9f}; mPaint.setShader(new LinearGradient(0, 0, mRectF.centerX(), 0, colorArray, positionArray, Shader.TileMode.MIRROR));