首页 > 编程知识 正文

setpixelsize,gradient公式

时间:2023-05-05 16:53:49 阅读:110453 作者:547

安卓绘制安卓绘制的Paint(1 (学习1 (1) )。

安卓绘制的Canvas基础(2) )。

安卓图的Path(3) )。

安卓绘制的drawText绘制文本关联(4) )。

安卓绘制的Canvas概念理解(5) ) ) ) ) ) ) )。

安卓绘制的Canvas变换(6) )。

安卓图形的Canvas状态保存和恢复(7) )。

安卓图的Patheffect(8) )。

安卓绘制的线性渐变(9)。

安卓显卡的sweepgradient(10 )。

安卓绘制的径向梯度辐射渐变(11 ) )。

安卓绘制的位图着色器(12 )。

安卓显卡的ComposeShader、PorterDuff.mode和xfermode(13 )。

安卓绘制的drawText、getTextBounds、measureText、FontMetrics、基线(14 ) )。

安卓图的贝塞尔曲线概述(15 )。

安卓图的路径度量(16 ) )。

Android是一种动态更改渐变的渐变驱动程序

1线性渐变概述线性渐变使用“paint set shader”。 Shader称为着色器,opengl经常使用这个概念。 android材质球主要用于对图像进行着色,材质球在绘制过程中会返回到重要的水平颜色组

Shader的具体实现类如下。

BitmapShader、ComposeShader、LinearGradient、RadialGradient和SweepGradient

3359 blog.csdn.net/u 010126792/article/details/83787779我在这篇文章中写了android渐变的实现,在这里我们将从LinearGradient开始学习。

线性梯度的两个构造函数:

/* * * createashaderthatdrawsalineargradientalongaline.* * @ param x0 the x-coordinateforthestartofthegradientline * @ param y0 they-coordinateforthestartofthegradientline * @ paramx 1th ex-coordinatefortheendofthegradientline * @ param y1 they-coordinatefortheendofthegradientline * @ param color0thecoloratthestartofthegradientline.* @ param color1thecord @ paramtiletheshadertilingmode */publiclineargradient (float x0,float y0 float y1,@ColorInt int color0,@ColorInt int color1)/* * * createashaderthatdrawsalineargradientalongaline.* * @ param x0 the x-coordinateforthestartofthegradientline * @ param y0 they-coordinateforthestartofthegradientline * @ paramx 1th ex-coordinatefortheendofthegradientline * @ param y1 they-coordinatefortheendofthegradientline * @ paramcolorsthecolorstobedistributedalongthegradientline * @ parampositionsmaybenull.therelativepositions [0.1] of * eachcorrespondingcolorinthecolorsarry.ifthisisnull,* the

buted 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,图形最好是正方形这样比较好看设置:

@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int width = MeasureSpec.getSize(widthMeasureSpec); setMeasuredDimension(width,width);}@Overrideprotected 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);}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。