标签:paint Color float Paint int 详解 new Android
Android Paint类详解
概述
android中绘制特定图案类似显示中的绘画需要画笔和画纸,为此android提供了Paint和Canvas。
Paint和Canvas分别代表画笔和画布。
Paint类似画笔,保存着绘制文本,图形,图片的样式和颜色信息。(颜色,宽度,粗细,透明度,字体样式,字体大小)。
大体上可以分为两类,一类与图形绘制相关,一类与文本绘制相关
提供了三种初始化方法
1.Paint()
2.Paint(int flags)
3.Paint(Paint paint)
常用方法
方法 | 说明 |
---|---|
setTextSize(float textSize) 设置字体大小;单位是px | 例:paint.setTextSize(20); 注意:如果是dp要注意转换 |
setFakeBoldText(boolean fakeBoldText) 实现粗体文字 | 例:paint.setFakeBoldText (true); |
setTextSkewX(float skewX) 设置斜体文字 | 例: paint.setTextSkewX(20); |
setTypeface(Typeface typeface) 设置字体样式 | typeface有以下5种: 1.ypeface.DEFAULT:默认字体 2.Typeface.DEFAULT_BOLD:加粗字体 3.Typeface.MONOSPACE:monospace字体 4.Typeface.SANS_SERIF:sans字体 5.Typeface.SERIF:serif字体 例:paint.setTypeface(Typeface.SANS_SERIF); |
setTextScaleX(float scaleX) 设置绘制文字x轴的缩放比例,实现文字的拉伸的效果 | 例:paint.setTextScaleX((float) 0.7); |
setStrikeThruText(boolean strikeThruText) 设置文字带有删除线的效果 | 例:paint.setStrikeThruText(true); |
setUnderlineText(boolean underlineText) 设置带有下划线的文字效果 | 例:paint.setUnderlineText(true); |
setTextAlign() 设置字体对齐方式;是相对于开始绘制的坐标点 | 有以下三种对齐方式: 1.Paint.Align.LEFT 左对齐 2.Paint.Align.CENTER 居中 3.Paint.Align.RIGHT 右对齐 例:paint.setTextAlign(Paint.Align.RIGHT) |
setSubpixelText(boolean subpixelText) 提升文本在LCD屏幕上的显示效果 | 例:paint.setSubpixelText(true) |
setColor(int a) 设置画笔颜色 | 例:paint.setColor(Color.RED); |
setARGB(int a,int r,int g,int b) 设置alpha | red,green,blue;取值范围都是 0~255 例:paint.setARGB(100,0,0,0); |
setStrokeWidth(float width) 设置画笔宽度;单位px | 例:paint.setStrokeWidth(10); |
setAlpha(int a) 设置透明度 | 取值得范围0到255; 0是透明,255不透明 例:paint.setAlpha(255); 注意:需要先调用setColor(),再调用setAlpha才会生效,否则将会被覆盖,因为setColor中包含了alpha |
setAntiAlias(boolean) 抗锯齿使边界更顺滑;消耗较大资源 | 例:paint.setAntiAlias(true); |
setStyle(Paint.Style style) 设置填充样式; | 有以下三种形式: 1.Paint.Style.FILL 填充内部,会把闭合区域填充颜色 2.Paint.Style.FILL_AND_STROKE填充内部和描边 3.Paint.Style.STROKE 仅描边,仅仅绘制边界 例:paint.setStyle(Paint.Style.FILL); |
线条样式
方法 | 说明 |
---|---|
setStrokeJoin(Paint.Join join) 连接点的样式 | 有以下三种形式: 1.Join.MITER(结合处为锐角) 2.Join.Round(结合处为圆弧) 3.Join.BEVEL(结合处为直线) 例:paint.setStrokeJoin(Paint.Join.BEVEL); |
setStrokeCap(Paint.Cap cap) 画笔的样式(落笔,收笔时) | 有以下三种形式: 1.Cap.ROUND(圆形) 2.Cap.SQUARE(方形) 3.Cap.BUTT(无) 例:paint.setStrokeCap(Paint.Cap.ROUND); |
setPathEffect(PathEffect effect) 给path设置样式(效果) | 一般使用其六个子类: 1.CornerPathEffect 2.DashPathEffect 3.DiscretePathEffect 4.PathDashPathEffect 5.ComposePathEffect 6.SumPathEffect 例: PathEffect pathEffect= new CornerPathEffect(20); paint.setPathEffect(pathEffect); |
CornerPathEffect(float radius) 将Path的各个连接线段之间的夹角用一种更平滑的方式连接,类似于圆弧与切线的效果 | radius:指定圆弧的半径 例: CornerPathEffect cornerPathEffect = new CornerPathEffect(50); mPaint.setPathEffect(cornerPathEffect); |
DashPathEffect(float[] intervals, float phase) 将Path的线段虚线化 | intervals:为虚线的ON和OFF的数组,数组中元素数目需要 >= 2个 phase:为绘制时的偏移量,动态改变其值会让路径产生动画的效果 例: DashPathEffect dashPathEffect= new DashPathEffect(new float[] {20, 10}, 1); mPaint.setPathEffect(dashPathEffect); |
PathDashPathEffect(Path shape, float advance, float phase, PathDashPathEffect.Style style) 使用Path图形来填充当前的路径 PathDashPathEffect和DashPathEffect是类似的,不同的是PathDashPathEffect可以自己定义路径虚线的样式 | shape:填充图形 advance:每个图形间的间隔 phase:为绘制时的偏移量,动态改变其值会让路径产生动画的效果 style:是该类自由的枚举值,有三种情况: 1.ROTATE:线段连接处的图形转换旋转到与下一段移动方向相一致的角度进行连接 2.MORPH:图形会以发生拉伸或压缩等变形的情况与下一段相连接 3.TRANSLATE:图形会以位置平移的方式与下一段相连接 例: PathDashPathEffect pathDashPathEffect= new PathDashPathEffect(path, 12, phase, PathDashPathEffect.Style.ROTATE); mPaint.setPathEffect(pathDashPathEffect); |
DiscretePathEffect(float segmentLength, float deviation) 打散Path的线段,使得在原来路径的基础上发生打散效果 在路径上绘制很多“杂点”的突出来模拟一种类似生锈铁丝的效果 | segmentLength:指定最大的段长 deviation:为绘制时的偏离量 例: DiscretePathEffect discretePathEffect= new DiscretePathEffect(3.0F, 5.0F); mPaint.setPathEffect(discretePathEffect); |
ComposePathEffect(PathEffect outerpe, PathEffect innerpe) 组合效果,会首先将innerpe变现出来,接着在innerpe的基础上来增加outerpe效果 | 例: CornerPathEffect cornerPathEffect = new CornerPathEffect(50); DashPathEffect dashPathEffect= new DashPathEffect(new float[] {20, 10}, 1) ComposePathEffect composePathEffect= new ComposePathEffect(cornerPathEffect ,dashPathEffect) |
SumPathEffect(PathEffect first, PathEffect second) 叠加效果,和ComposePathEffect不同,在表现时会将两个参数的效果都独立的表现出来, 接着将两个效果简单的重叠在一起显示出来 | 例: CornerPathEffect cornerPathEffect = new CornerPathEffect(50); DashPathEffect dashPathEffect= new DashPathEffect(new float[] {20, 10}, 1) SumPathEffect sumPathEffect= new SumPathEffect(cornerPathEffect ,dashPathEffect) |
着色渐变
方法 | 说明 |
---|---|
渐变模式 | ABAB型:两种颜色重复变化,通过 TileMode 类的 REPEAT 常量来表示; ABBA型:两种颜色镜像变化,通过 TileMode 类的 MIRROR 常量来表示; AABB型:两种颜色只出现一次,通过 TileMode类的 CLAMP 常量来表示 |
setShader(Shader shader) 使用已定义的Shader对象 | 例: paint.setShader(linearGradient); |
LinearGradient(float x0, float y0, float x1, float y1, int color0, int color1, TileMode tile) 线性渐变 | x0、y0:用于决定线性方向的起始点的坐标(x0,y0) x1、y1:用于决定线性方向的终止点的坐标(x1,y1) color0:第一种颜色 color1:第二种颜色 tile:渐变模式 例: LinearGradient linearGradient = new LinearGradient(100,100,200,100,Color.RED,Color.BLUE,Shader.TileMode.MIRROR) ; paint.setShader(linearGradient); |
LinearGradient(float x0, float y0, float x1, float y1, int colors[], float positions[], TileMode tile) 线性渐变 | x0、y0:用于决定线性方向的起始点的坐标(x0,y0) x1、y1:用于决定线性方向的终止点的坐标(x1,y1) color0[]:多种颜色 positions[]:颜色的位置(比例) tile:渐变模式 例: LinearGradient linearGradient = new LinearGradient(100,100,800,100,new int [] {Color.RED,Color.YELLOW,Color.BLUE},new float[]{0,0.5f,1},Shader.TileMode.CLAMP) ;; paint.setShader(linearGradient); 注意:参数 colors[] 和 positions[] 都是数组,前者用于指定多种颜色,后者用于指定每种颜色的起始比例位置。positions 数组中的元素个数与 colors 要相同 |
RadialGradient(float x, float y, float radius, int color0, int color1, TileMode tile) 径向渐变是以指定的点为中心,向四周以渐变颜色进行圆周扩散 | x、y:中心点坐标 radius:渐变半径 color0:起始颜色 color1:结束颜色 tile:渐变模式 例: RadialGradient radialGradient = new RadialGradient(400,400,300,Color.RED,Color.BLUE,Shader.TileMode.CLAMP) ; paint.setShader(radialGradient); |
RadialGradient(float x, float y, float radius, int colors[], float positions[], TileModetile) 径向渐变是以指定的点为中心,向四周以渐变颜色进行圆周扩散 | x、y:中心点坐标 radius:渐变半径 colors[]:多种颜色 positions[]:颜色的位置(比例) tile:渐变模式 例: radialGradient = new RadialGradient(400,1200,300,new int[]{Color.RED,Color.YELLOW,Color.GRAY,Color.BLUE},new float[]{0,0.4f,0.8f,1},Shader.TileMode.CLAMP) ; paint.setShader(radialGradient); 注意:参数 colors[] 和 positions[] 都是数组,前者用于指定多种颜色,后者用于指定每种颜色的起始比例位置。positions 数组中的元素个数与 colors 要相同 |
SweepGradient(float cx, float cy, int color0, int color1) 扫面渐变 ,类似于军事雷达一样,不断围绕圆心扫描 | cx、cy:圆点坐标 color0:起始颜色 color1:结束颜色 例: SweepGradient sweepGradient = new SweepGradient(400,400,Color.RED,Color.BLUE) paint.setShader(sweepGradient); |
SweepGradient(float cx, float cy, int colors[], float positions[]) 支持多种颜色的扫描渐变 | x、y:中心点坐标 colors[]:多种颜色 positions[]:颜色的位置(比例);null为均分 例: sweepGradient = new SweepGradient(400,1200,new int[]{Color.RED,Color.YELLOW,Color.GRAY,Color.BLUE},null) ; paint.setShader(sweepGradient); 注意:参数 colors[] 和 positions[] 都是数组,前者用于指定多种颜色,后者用于指定每种颜色的起始比例位置。positions 数组中的元素个数与 colors 要相同 |
BitmapShader(Bitmap bitmap, TileMode tileX, TileMode tileY) 位图渐变 | bitmap:使用的位图 tileX: 位图在上X轴方向上的模式 tileY:位图在上Y轴方向上的模式 例: bitmapShader = new BitmapShader(bitmap, Shader.TileMode.MIRROR,Shader.TileMode.REPEAT); paint.setShader(bitmapShader ); |
ComposeShader (Shader shaderA, Shader shaderB, PorterDuff.Mode mode) 使两个Shader组合在一起作为一个新Shader | shaderA:第一个Shader对象 shaderB:第二个Shader对象 mode:混合效果如图 例: radialGradient = new RadialGradient(200, 200, 200, Color.RED, Color.GREEN, Shader.TileMode.CLAMP); sweepGradient = new SweepGradient(200, 200, new int[] { Color.GREEN, Color.WHITE, Color.GREEN }, null); composeShader =new ComposeShader (radialGradient ,sweepGradient ,PorterDuff.Mode.DARKEN ) paint.setShader(composeShader ); |
ComposeShader (Shader shaderA, Shader shaderB, Xfermode mode) 使两个Shader组合在一起作为一个新Shader() 此方法已过时 | shaderA:第一个Shader对象 shaderB:第二个Shader对象 mode:混合效果 |
颜色优化
方法 | 说明 |
---|---|
setDither(boolean dither) 否使用图像抖动处理,绘制的图片颜色更加平滑和饱满在实际的应用场景中,抖动更多的作用是在图像降低色彩深度绘制时,避免出现大片的色带与色块 | 例:paint.setDither(true); |
setFilterBitmap(boolean filter) 线性过滤;如果该项设置为true,结果图像显得更加平滑 | 例:paint.setFilterBitmap(true); |
阴影或者上层效果
方法 | 说明 |
---|---|
setShadowLayer(float radius, float dx, float dy, int shadowColor) 设置阴影;在绘制层下方的附加效果 | radius:设置角度 dx:x轴偏移量(正负代表右左) dy:y轴偏移量(正负代表右左) shadowColor: 阴影颜色 例:paint.setShadowLayer(10, 0, 0, Color.BLUE); |
setMaskFilter(MaskFilter maskfilter) 在绘制层上方使用附加效果 | maskfilter:MaskFilter对象 例:paint.setMaskFilter(paint.setMaskFilter(blurMaskFilterOUTER)) |
BlurMaskFilter(float radius, BlurMaskFilter.Blur style) 指定了一个模糊的样式和半径来处理Paint的边缘,让目标部分模糊不清;在绘制层下方的附加效果 | radius:模糊半径 style它有四种如下模式: 1.NORMAL: 内外都模糊绘制 2.SOLID: 内部正常绘制,外部模糊 3.INNER: 内部模糊,外部不绘制 4.OUTER: 内部不绘制,外部模糊 例: BlurMaskFilter blurMaskFilterOUTER = new BlurMaskFilter(50,BlurMaskFilter.Blur.OUTER); paint.setMaskFilter(blurMaskFilterOUTER) |
EmbossMaskFilter(float[] direction, float ambient, float specular, float blurRadius) 指定了光源的方向和环境光强度来添加浮雕效果,是让目标部分有凹凸的水印图案 | direction :是一个 3 个元素的数组,指定了光源的方向 ambient :是环境光的强度,数值范围是 0 到 1 specular :是炫光的系数 blurRadius :是应用光线的范围 例: EmbossMaskFilter embossMaskFilter = new EmbossMaskFilter(new float[]{10, 10, 10}, 0.1f, 5, 5); paint.setMaskFilter(embossMaskFilter); |
获取路径
方法 | 说明 |
---|---|
getFillPath(Path src, Path dst) 获取实际路径 | src :原路径 dst :实际路径的保存位置 |
getTextPath(String text, int start, int end, float x, float y, Path path) 获取文字路径 | Text:文字 Start:开始获取位置 End:结束获取的位置 X:文字所在的X坐标 Y:文字所在的Y坐标 Path:保存的路径 例: String text = “测试文本 Test”; Path desPath = new Path(); srcPaint.getTextPath(text,0,text.length(),50,100,desPath); |
标签:paint,Color,float,Paint,int,详解,new,Android 来源: https://blog.csdn.net/weixin_45558166/article/details/110264358
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。