Android Paint类详解

概述

android中绘制特定图案类似显示中的绘画需要画笔和画纸,为此android提供了Paint和Canvas。
Paint和Canvas分别代表画笔和画布。
Paint类似画笔,保存着绘制文本,图形,图片的样式和颜色信息。(颜色,宽度,粗细,透明度,字体样式,字体大小)。
大体上可以分为两类,一类与图形绘制相关,一类与文本绘制相关

提供了三种初始化方法

1.Paint()
2.Paint(int flags)
3.Paint(Paint paint)

字体方法

方法说明
public void setTextSize (float textSize)
设置字体大小;单位是px
例:paint.setTextSize(20);
注意:如果是dp要注意转换
public void setFakeBoldText (boolean fakeBoldText)
实现粗体文字
例:paint.setFakeBoldText (true);
public void setTextSkewX (float skewX)
设置斜体文字
例: paint.setTextSkewX(20);
public Typeface 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);
public void setTextScaleX (float scaleX)
设置绘制文字x轴的缩放比例,实现文字的拉伸的效果
例:paint.setTextScaleX((float) 0.7);
public void setStrikeThruText (boolean strikeThruText)
设置文字带有删除线的效果
例:paint.setStrikeThruText(true);
public void setUnderlineText (boolean underlineText)
设置带有下划线的文字效果
例:paint.setUnderlineText(true);
public void setTextAlign (Paint.Align align)
设置字体对齐方式;是相对于开始绘制的坐标点
有以下三种对齐方式:
1.Paint.Align.LEFT 左对齐
2.Paint.Align.CENTER 居中
3.Paint.Align.RIGHT 右对齐
例:paint.setTextAlign(Paint.Align.RIGHT)
public void getTextBounds (char[] text, int index, int count, Rect bounds)
保存边界包含所有字符的最小矩形,隐含的原点在(0,0)
text : 需要测量的字符数组
index: 要测量的数组中第一个字符的索引
count : 要度量的字符数,从索引开始
bounds: 返回文本边界的Rect对象
例:
char[] cha = new char[] { ‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’ }
Paint mPaint = new Paint();
Rect mBound = new Rect();
mPaint.getTextBounds(cha , 0, mText.length(), mBound);
public void getTextBounds (String text, int start, int end, Rect bounds)
保存边界包含所有字符的最小矩形,隐含的原点在(0,0)
text : 需要测量的字符串
index: 要测量的数组中第一个字符的索
count : 要度量的字符数,从索引开始
bounds: 返回文本边界的Rect对象
例:
String str =“abcdefg”;
Paint mPaint = new Paint();
Rect mBound = new Rect();
mPaint.getTextBounds(str, 0, mText.length(), mBound);
public Paint.Align getTextAlign ()
返回绘制文本的对齐值
public Locale getTextLocale ()
获取文本区域设置
getTextPath(String text, int start, int end, float x, float y, Path path)
返回指定文本的路径
text :字符串文本
start: 文本中需要返回路径的第一个字符
end: 文本中需要返回路径的最后一个字符
x: 文本原点的x轴坐标值
y: 文本原点的y轴坐标值
path: 保存文本路径的path对象
例:
String text = “测试文本 Test”;
Path desPath = new Path();
srcPaint.getTextPath(text,0,text.length(),50,100,desPath);
getTextPath(char[] text, int index, int count, float x, float y, Path path)
返回指定文本的路径
text :字符数组文本
index: 要测量的数组中第一个字符的索引
count: 要度量的字符数,从索引开始
x: 文本原点的x轴坐标值
y: 文本原点的y轴坐标值
path: 保存文本路径的path对象
例:
char[] cha = new char[] { ‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’ }
Path desPath = new Path();
srcPaint.getTextPath(cha,0,cha.length(),50,100,desPath);
public float getTextScaleX ()
返回文本绘制的水平缩放系数。默认值是1.0
public float getTextSize ()
返回(画笔)文本大小
public float getTextSkewX ()
返回文本绘制的水平倾斜系数。默认值是0
public int getTextWidths (String text, float[] widths)
返回字符串宽度
text: 要测量的文本
widths: (数组)以接收字符的宽度。必须至少和文本一样大
public int getTextWidths (CharSequence text, int start, int end, float[] widths)
返回字符串宽度
text: 要测量的文本(不能为空)
start: 要测量的第一个字符的索引
end: 要测量的最后一个字符的索引
widths: (数组)以接收字符的宽度;必须至少和文本一样大 (end - start).
public int getTextWidths (String text, int start, int end, float[] widths)
返回字符串宽度
text: 要测量的文本(不能为空)
start: 要测量的第一个字符的索引
end: 要测量的最后一个字符的索引
widths: (数组)以接收字符的宽度;必须至少和文本一样大
public int getTextWidths (char[] text, int index, int count, float[] widths)
返回字符串宽度
text: 要测量的文本(不能为空)
start: 要测量的第一个字符的索引
end: 要测量的最后一个字符的索引
widths: (数组)以接收字符的宽度;必须至少和文本总数一样大
public float measureText (String text)
返回文本的宽度
text: 要测量的文本(不能为null)
public float measureText (CharSequence text, int start, int end)
返回文本的宽度
text: 要测量的文本
start: 开始的第一个字符的索引
end: 索引的最后一个字符+1
public float measureText (String text, int start, int end)
返回文本的宽度
text: 要测量的文本(不能为null)
start: 开始的第一个字符的索引
end: 索引的最后一个字符+1
public float measureText (char[] text, int index, int count)
返回文本的宽度
text: 要测量的文本(不能为null)
index: 开始的第一个字符的索引
count: 从第一个索引算需要测量的字符数
public float ascent ()
根据当前字体和文本大小返回基线之上的距离(负)
public float descent ()
根据当前的字体和文本大小返回基线以下的距离(正)
public Typeface getTypeface ()
获取paint的typeface对象
public Xfermode getXfermode ()
获取paint的xfermode对象

常用方法

方法说明
public void setSubpixelText (boolean subpixelText)
提升文本在LCD屏幕上的显示效果
例:paint.setSubpixelText(true)
public void setColor (int color)
设置画笔颜色
color: 在绘图中设置的新颜色(包括alpha)
例:paint.setColor(Color.RED);
注意: 颜色是一个包含alpha和r,g,b的32位值。这个32位的值没有被预乘,这意味着它的alpha可以是任何值,不管r、g、b的值是多少
public int getColor ()
返回paint的颜色
例: paint.getColor();
注意: 颜色是一个包含alpha和r,g,b的32位值。这个32位的值没有被预乘,这意味着它的alpha可以是任何值,不管r、g、b的值是多少
public void setARGB (int a, int r, int g, int b)
接受a,r,g,b并构造颜色(int)
a: 取值范围是 0~255
r: 取值范围是 0~255
g: 取值范围是 0~255
b: 取值范围是 0~255
例:paint.setARGB(100,0,0,0);
public void setStrokeWidth (float width)
设置画笔宽度
width: 画笔宽度
例:paint.setStrokeWidth(10);
注意: 宽度单位为px
public void setAlpha (int a)
设置透明度
a: 取值范围是 0~255(0是透明,255不透明)
例:paint.setAlpha(255);
注意:需要先调用setColor(),再调用setAlpha才会生效,否则将会被覆盖,因为setColor中包含了alpha
public int getAlpha ()
返回颜色的透明度值( 0~255)
例: paint. getAlpha ();
public void setAntiAlias (boolean aa)
设置抗锯齿,使边界更顺滑
例:paint.setAntiAlias(true);
public void setStyle (Paint.Style style)
设置填充样式
style: 填充样式,有3种
1.Paint.Style.FILL 填充内部,会把闭合区域填充颜色
2.Paint.Style.FILL_AND_STROKE填充内部和描边
3.Paint.Style.STROKE 仅描边,仅仅绘制边界
例:paint.setStyle(Paint.Style.FILL);
public Paint.Style getStyle ()
返回填充样式
例: paint.getStyle ();

线条样式

方法说明
public void setStrokeJoin (Paint.Join join)
设置连接点样式
有以下三种形式:
1.Join.MITER(结合处为锐角)
2.Join.Round(结合处为圆弧)
3.Join.BEVEL(结合处为直线)
例:paint.setStrokeJoin(Paint.Join.BEVEL);
public Paint.Join getStrokeJoin ()
返回连接点样式
例: paint.getStrokeJoin ();
setStrokeCap(Paint.Cap cap)
设置画笔头样式(落笔,收笔时)
有以下三种形式:
1.Cap.ROUND(圆形)
2.Cap.SQUARE(方形)
3.Cap.BUTT(无)
例:paint.setStrokeCap(Paint.Cap.ROUND);
public Paint.Cap getStrokeCap ()
返回画笔头样式
例:paint.getStrokeCap ();
setPathEffect(PathEffect effect)
给path设置样式(效果)
一般使用其六个子类:
1.CornerPathEffect(float radius)
2.DashPathEffect(float[] intervals, float phase)加速效果
3.DiscretePathEffect(float segmentLength, float deviation)
4.PathDashPathEffect(Path shape, float advance, float phase, PathDashPathEffect.Style style)
5.ComposePathEffect(PathEffect outerpe, PathEffect innerpe)
6.SumPathEffect(PathEffect first, PathEffect second)
例:
PathEffect pathEffect= new CornerPathEffect(20);
paint.setPathEffect(pathEffect);
public PathEffect getPathEffect ()
返回path设置的样式
例: paint.getPathEffect ();
CornerPathEffect(float radius)
将Path的各个连接线段之间的夹角用一种更平滑的方式连接,类似于圆弧与切线的效果
radius:指定圆弧的半径
例:
CornerPathEffect cornerPathEffect = new CornerPathEffect(50);
mPaint.setPathEffect(cornerPathEffect);
DashPathEffect(float[] intervals, float phase)
将Path的线段虚线化
intervals: intervals:数组必须包含偶数个条目(>=2);偶数指定“on”奇数指定“off” 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:为绘制时的偏移量(对所有的interval求和进行mod),动态改变其值会让路径产生动画的效果
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 常量来表示
public Shader setShader (Shader shader)
设置或清除着色器对象
通过null清除任何以前的着色器
一般使用其9个子类:
1. BitmapShader(Bitmap bitmap, TileMode tileX, TileMode tileY)
2. ComposeShader (Shader shaderA, Shader shaderB, PorterDuff.Mode mode)
3. ComposeShader (Shader shaderA, Shader shaderB, Xfermode mode)
4. LinearGradient(float x0, float y0, float x1, float y1, int color0, int color1, TileMode tile)
5. LinearGradient(float x0, float y0, float x1, float y1, int colors[], float positions[], TileMode tile)
6. RadialGradient(float x, float y, float radius, int color0, int color1, TileMode tile)
7. RadialGradient(float x, float y, float radius, int colors[], float positions[], TileModetile)
8. SweepGradient(float cx, float cy, int color0, int color1)
9. SweepGradient(float cx, float cy, int colors[], float positions[])
例:
paint.setShader(linearGradient);
public Shader getShader ()
返回paint的着色器对象
例: paint.getShader();
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:混合效果如图
16种混合效果
例:
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:混合效果

颜色优化

方法说明
public void setDither (boolean dither)
否使用图像抖动处理,绘制的图片颜色更加平滑和饱满在实际的应用场景中,抖动更多的作用是在图像降低色彩深度绘制时,避免出现大片的色带与色块
例:paint.setDither(true);
public final boolean isDither ()
返回是否使用了抖动处理
例: paint.isDither();
public void setFilterBitmap (boolean filter)
设置FILTER_BITMAP_FLAG标志位,若使用图像显得更加平滑
例:paint.setFilterBitmap(true);
public final boolean isFilterBitmap ()
是否使用FILTER_BITMAP_FLAG 标志位
例: paint.isFilterBitmap ();

阴影或者上层效果

方法说明
public void setShadowLayer (float radius, float dx, float dy, int color)
主图层下面绘制一个阴影层,使用指定的偏移量和颜色,以及模糊半径
如果半径为0,那么阴影层被移除
radius:设置角度
dx:x轴偏移量(正负代表右左)
dy:y轴偏移量(正负代表右左)
shadowColor: 阴影颜色
例:paint.setShadowLayer(10, 0, 0, Color.BLUE);
public void clearShadowLayer ()
清除阴影层
例: paint.clearShadowLayer ()
public MaskFilter setMaskFilter (MaskFilter maskfilter)
设置或清除maskfilter对象;在绘制层上方使用附加效果
通过null清除任何以前的maskfilter
MaskFilter对象有2个子类
1. EmbossMaskFilter(float[] direction, float ambient, float specular, float blurRadius)
2. BlurMaskFilter(float radius, BlurMaskFilter.Blur style)
例:paint.setMaskFilter(paint.setMaskFilter(blurMaskFilterOUTER))
public MaskFilter getMaskFilter ()
返回maskfilter对象
例:paint.getMaskFilter ();
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);

获取路径

方法说明
public boolean getFillPath (Path src, Path dst)
获取实际路径
src :原路径
dst :实际路径的保存位置
public void 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);
public void getTextPath (char[] text, int index, int count, float x, float y, Path path)
获取文字路径
Text:文字字符数组
Start:开始获取位置
End:结束获取的位置
X:文字所在的X坐标
Y:文字所在的Y坐标
Path:保存的路径
例:
char[] cha = new char[] { ‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’ }
Path desPath = new Path();
srcPaint.getTextPath(cha,0,cha.length(),50,100,desPath);

更多用法请点击Paint API

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值