Android中Paint类和Canvas类的方法汇总
Paint类的常用的方法
1.setColor方法,用于设置画笔的颜色,
publicvoidsetColor(intcolor)//参数color为颜色值,也可以使用Color类定义的颜色
Color.BLACK:黑色
Color.BLUE:蓝色
Color.CYAN:青绿色
Color.DKGRAY:灰黑色
Color.YELLOW:黄色
Color.GRAY:灰色
Color.GREEN:绿色
Color.LTGRAY:浅绿色
Color.MAGENTA:红紫色
Color.TRANSPARENT:透明色
2.setAlpha方法,用于设置画笔的透明度
publicvoidsetAlpha(inta)//参数a为透明度,其取值范围为0~255,数值越小越透明
3.setStyle方法,用于设置画笔的风格,可以指定是圆心还是实心,该方法在矩形,圆形有明显的效果
publicvoidsetStyle(Paint.Stylestyle)//参数style为画笔的风格
Style.FILL:实心
Style.FILL_AND_STROKE:同时显示实心和空心
Style.STROKE:空心
4.setStrokeWidth方法,用于设置画笔的空心线宽,该方法在矩形,圆形,等图形上有明显的效果
publicvoidsetStrokeWidth(floatwidth)//参数width为线宽,浮点型数据
5.setTextSize方法,用于设置画笔的字体大小,主要用于绘制字符串
publicvoidsetTextSize(floattextSize)//
6.setTypeface方法用于设置画笔的字体样式,可以使用系统自带的字段,也可以使用自定义的字体
publicvoidTypeface(Typefacetypeface)//typeface为字体样式
Typeface.DEFAULT:默认字体
Typeface.DEFAULT_BOLD:加粗字体
Typeface.MONOSPACE:monospace字体
Typeface.SANS_SERIF:sans字体
Typeface.SERIF:serif字体
7.setTextScaleX方法.用于设置画笔字体的比例因子,默认为1,当大于1时表示横向拉伸,小于1时表示横向压缩
publicvoidsetTextScaleX(floatscaleX)
8.setARGB方法,用于设置画笔的颜色和透明度
publicvoidsetARGB(inta,intr,intg,intb);
参数a为透明度,范围0~255
参数r为红色的颜色值,范围0~255
参数g为绿色的颜色值,范围0~255
参数b为蓝色的颜色值,范围0~255
9.setUnderlineText方法,用于设置画笔的下划线
publicvoidsetUnderlineText(Booleanunderlintext)
当取值为true时,表示显示下划线
10.setTextSkewX方法,用于设置画笔的倾斜因子
publicvoidsetTextSkewX(floatskewX)
参数skewX为倾斜因子,正数表示向左倾斜,负数表示向右倾斜
Canvas类的方法
1.publicvoiddrawColor(intcolor)
用于设置画布的背景颜色
2.publicvoiddrawLine(floatstarX,floatstartY,floatstopX,floatstopY,Paintpaint);
用于在画布上绘制直线
参数分别为直线起点的X坐标,Y坐标,终点的X坐标,Y坐标,用到的画笔
3.publicvoiddrawLines(float[]pts,Paintpaint)
用于在画布上绘制多条直线
参数pts为绘制直线的端点数组,每条直线占用4个数据
4.publicvoiddrawPoint(floatx,floaty,Paintpaint)
用于在画布上绘制点
参数为点的X,Y坐标,和所用的画笔
5.publicvoiddrawPoints(float[]pts,Paintpaint)
publicvoiddrawPoints(float[]pts,intoffset,intcount,Paintpaint)
参数pts为绘制点的数组,每个点占用2个数据
参数offset为跳过的数据的个数
参数count为实际参与绘制的数据的个数
6.publicvoiddrawRect(Rectrect,Paintpaint)
publicvoiddrawRect(RectFrect,Paintpaint)
publicvoiddrawRect(floatleft,floatfloattop,floatright,floatbelow,Paintpaint)
用于绘制矩形
7.publicvoiddrawRoundRect(RectFrect,floatrx,floatry,Paintpaint)
用于绘制圆角矩形
rx为X方向上的圆角半径
ry为Y方向上的圆角半径
8.publicvoiddrawCircla(floatcx,floatcy,floatradius,Paintpaint)
用于在画布上绘制圆形
cx为圆形的x坐标
cy为圆形的y坐标
rad为圆的的半径
9.publicvoiddrawOval(RectFrect.Paintpaint)
用于绘制椭圆
通过指定椭圆的外切矩形实现
10.publicvoiddrawPath(Pathpath,Paintpaint)
用于在画布上绘制任意多边形来实现
11.publicvoiddrawArc(RectFoval,floatstartAngle,floatsweepAngle,`Booleanusecenter,Paintp)
参数oval为圆弧所在的椭圆对象
startAngle为圆弧的起始角度,
sweepAngle为圆弧的角度,
useCenter表示是否显示半径连线,当取值为true时,显示圆弧与圆心的半径连线,
12,publicvoiddrawText(Stringtext,floatx,floaty,Paintpaint)
publicvoiddrawText(char[]text,intindex,intcount,floatx,floaty,Paintpaint)
publicvoiddrawText(CharSequencetext,intstart,intend,floatx,floaty,Paintpaint)
publicvoiddrawText(Stringtext,intstart,intend,floatx,floaty,Paintpaint)
参数text为字符串的内容,
x为X坐标
y为Y坐标
index为显示的起始字符位置
count为显示的字符个数
start为显示的起始字符的位置
end为显示的终止的字符的位置
13,publicvoiddrawBitmap(Bitmapbitmap,floatleft,floattop,Paintpaint)
参数bitmap为Bitmap对象,代表图像资源,
left为图像显示的左边的位置
right为图像的显示的右边的位置
14,publicintsave()
用于锁定画布中的某一个或某几个对象,用于锁定对象操作的场合
使用sava方法锁定画布并完成操作之后,需要使用restore方法解除锁定
15,publicBooleanclipRect(Rectrect)
publicBooleanclipRect(floatleft,floattop,floatright,floatbottom)
publicBooleanclipRect(intleft,inttop,intright,intboottom)
该方法用于裁剪画布,设置画布的显示区域
16,publicvoidrotate(floatdegrees)
publicvoidrotate(floatdegrees,floatpx,floatpy)
用于旋转画布,通过旋转画布,可以将画布上绘制的对象旋转
参数degrees为旋转的角度,正数为顺时针方向,负数为逆时针方向
px为旋转点的x坐标
py为旋转点的y坐标
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持毛票票!