温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Paint与Canvas类如何在Android中使用

发布时间:2020-11-21 17:02:49 来源:亿速云 阅读:125 作者:Leah 栏目:移动开发

本篇文章给大家分享的是有关Paint与Canvas类如何在Android中使用,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

Paint类的常用的方法

1.setColor方法,用于设置画笔的颜色,

public void setColor(int color)//参数color为颜色值,也可以使用Color类定义的颜色

Color.BLACK:黑色

Color.BLUE:蓝色

Color.CYAN:青绿色

Color.DKGRAY:灰黑色

Color.YELLOW:黄色

Color.GRAY:灰色

Color.GREEN:绿色

Color.LTGRAY:浅绿色

Color.MAGENTA:红紫色

Color.TRANSPARENT:透明色

2.setAlpha方法,用于设置画笔的透明度

public void setAlpha(int a )//参数a为透明度,其取值范围为0~255,数值越小越透明

3.setStyle方法,用于设置画笔的风格,可以指定是圆心还是实心,该方法在矩形,圆形有明显的效果

public void setStyle(Paint.Style style)//参数style为画笔的风格

Style.FILL:实心

Style.FILL_AND_STROKE:同时显示实心和空心

Style.STROKE:空心

4.setStrokeWidth方法,用于设置画笔的空心线宽,该方法在矩形,圆形,等图形上有明显的效果

public void setStrokeWidth(float width)//参数width为线宽,浮点型数据

5.setTextSize方法,用于设置画笔的字体大小,主要用于绘制字符串

public void setTextSize(float textSize)//

6.setTypeface方法用于设置画笔的字体样式,可以使用系统自带的字段,也可以使用自定义的字体

public void Typeface(Typeface typeface)//typeface为字体样式

Typeface.DEFAULT:默认字体

Typeface.DEFAULT_BOLD:加粗字体

Typeface.MONOSPACE:monospace字体

Typeface.SANS_SERIF:sans字体

Typeface.SERIF:serif字体

7.setTextScaleX方法.用于设置画笔字体的比例因子,默认为1,当大于1时表示横向拉伸,小于1时表示横向压缩

public void setTextScaleX(float scaleX)

8.setARGB方法,用于设置画笔的颜色和透明度

public void setARGB(int a,int r,int g,int b);

参数a为透明度,范围0~255

参数r为红色的颜色值,范围0~255

参数g为绿色的颜色值,范围0~255

参数b为蓝色的颜色值,范围0~255

9.setUnderlineText方法,用于设置画笔的下划线

public void setUnderlineText(Boolean underlintext)

当取值为true时,表示显示下划线

10.setTextSkewX方法,用于设置画笔的倾斜因子

public void setTextSkewX(float skewX)

参数skewX为倾斜因子,正数表示向左倾斜,负数表示向右倾斜

Canvas类的方法

1.public void drawColor(int color)

用于设置画布的背景颜色

2.public void drawLine(float starX,float startY,float stopX,float stopY,Paint paint);

用于在画布上绘制直线

参数分别为直线起点的X坐标,Y坐标,终点的X坐标,Y坐标,用到的画笔

3.public void drawLines(float[] pts,Paint paint)

用于在画布上绘制多条直线

参数pts为绘制直线的端点数组,每条直线占用4个数据

4.public void drawPoint(float x,float y,Paint paint)

用于在画布上绘制点

参数为点的X,Y坐标,和所用的画笔

5.public void drawPoints(float[] pts,Paint paint)

public void drawPoints(float[] pts,int offset,int count,Paint paint)

参数pts为绘制点的数组,每个点占用2个数据

参数offset为跳过的数据的个数

参数count为实际参与绘制的数据的个数

6.public void drawRect(Rect rect,Paint paint)

public void drawRect(RectF rect,Paint paint)

public void drawRect(float left,float float top,float right,float below,Paint paint)

用于绘制矩形

7.public void drawRoundRect(RectF rect,float rx,float ry,Paint paint)

用于绘制圆角矩形

rx为X方向上的圆角半径

ry为Y方向上的圆角半径

8.public void drawCircla(float cx,float cy,float radius, Paint paint)

用于在画布上绘制圆形

cx为圆形的x坐标

cy为圆形的y坐标

rad为圆的的半径

9.public void drawOval(RectF rect.Paint paint)

用于绘制椭圆

通过指定椭圆的外切矩形实现

10.public void drawPath(Path path,Paint paint)

用于在画布上绘制任意多边形来实现

11.public void drawArc(RectF oval,float startAngle,float sweepAngle,`Boolean usecenter,Paint p)

参数oval为圆弧所在的椭圆对象

startAngle为圆弧的起始角度,

sweepAngle为圆弧的角度,

useCenter表示是否显示半径连线,当取值为true时,显示圆弧与圆心的半径连线,

12,public void drawText(String text,float x,float y, Paint paint)

public void drawText(char[] text,int index,int count,float x,float y,Paint paint)
public void drawText(CharSequence text,int start,int end,float x,float y,Paint paint)
public void drawText(String text,int start,int end,float x,float y,Paint paint)

参数text为字符串的内容,

x为X坐标

y为Y坐标

index为显示的起始字符位置

count为显示的字符个数

start为显示的起始字符的位置

end为显示的终止的字符的位置

13,public void drawBitmap(Bitmap bitmap,float left,float top,Paint paint)

参数bitmap为Bitmap对象,代表图像资源,

left为图像显示的左边的位置

right为图像的显示的右边的位置

14,public int save()

用于锁定画布中的某一个或某几个对象,用于锁定对象操作的场合

使用sava方法锁定画布并完成操作之后,需要使用restore方法解除锁定

15,public Boolean clipRect(Rect rect)

public Boolean clipRect(float left,float top,float right,float bottom)

public Boolean clipRect(int left,int top,int right,int boottom)

该方法用于裁剪画布,设置画布的显示区域

16,public void rotate(float degrees)

public void rotate(float degrees,float px,float py)

用于旋转画布,通过旋转画布,可以将画布上绘制的对象旋转

参数degrees为旋转的角度,正数为顺时针方向,负数为逆时针方向

px为旋转点的x坐标

py为旋转点的y坐标

以上就是Paint与Canvas类如何在Android中使用,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI