欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Androi原生绘图工具Paint

程序员文章站 2022-06-24 11:28:53
目录2.1 setalpha透明度2.2 setstyle填充样式2.4 setshadowlayer 阴影2.5 setstrokecap2.6 setstrokejoin前言:生活中绘制图案需要纸...

前言:

生活中绘制图案需要纸和笔,android中提供了类似的工具canvaspaint,分别对应画布和画笔。

the paint class holds the style and color information about how to draw geometries, text and bitmaps.

paint拥有绘制几何图形、文本、图片的样式和颜色信息。

the canvas class holds the "draw" calls. to draw something, you need 4 basic components: a bitmap to hold the pixels, a canvas to host the draw calls (writing into the bitmap), a drawing primitive (e.g. rect, path, text, bitmap), and a paint (to describe the colors and styles for the drawing).

canvas拥有绘图的请求权,想要完成绘制,需要4个基本元素:保存像素的bitmap、一个可以调用draw方法的canvas、要绘制的内容描述、一个画笔。凭借这4个元素就可以绘制到一个bitmap上,然后渲染到屏幕上。

1.简介

今天先将一下paint的相关内容。paint提供了三种初始化方法:

paint() 
paint(int flag)
paint(paint paint)

2.基本属性使用

2.1 setalpha透明度

setalpha(int a) 设置范围0~255

2.2 setstyle填充样式

填充样式表示绘线和填充的选择:

paint.style.fill
paint.style.fill_and_stroke
paint.style.stroke

Androi原生绘图工具Paint

2.3 颜色,字体,对齐,抗锯齿

setcolorsetargb设置颜色,设置画笔的颜色,
setargb()参数范围0-255。
setantialias(boolean) 抗锯齿,使边界更顺滑(有些屏幕分辨率不高,导致像素点比较大,绘制边界可能会有颗粒感,打开抗锯齿边界颗粒感会减少)。
settextsize(float textsize) 字体大小,单位是px,如果是dp要注意转换。
settextalign(paint.align.right)设置字体对齐方式,根据下面的实例可以,对齐方式基于开始绘制的点。

mtextpaint.settextalign(paint.align.left);
canvas.drawtext("matumbaman的博客",200, 200, mtextpaint);

mtextpaint.settextalign(paint.align.right);
canvas.drawtext("matumbaman的博客",200, 300, mtextpaint);

mtextpaint.settextalign(paint.align.center);
canvas.drawtext("matumbaman的博客",200, 400, mtextpaint);

Androi原生绘图工具Paint

2.4 setshadowlayer 阴影

setshadowlayer(float radius, float dx, float dy, int shadowcolor) 设置阴影

radius设置角度,dx,dy控制字体的上下左右出现,有正负之分,dx的正负代表右左,dy的正负代表下上。

mtextpaint.settextalign(paint.align.left);
mtextpaint.setshadowlayer(10, -20, 40, color.gray);
canvas.drawtext("matumbaman的博客",200, 200, mtextpaint);


mtextpaint.settextalign(paint.align.right);
canvas.drawtext("matumbaman的博客",200, 300, mtextpaint);

mtextpaint.settextalign(paint.align.center);
canvas.drawtext("matumbaman的博客",200, 400, mtextpaint);

Androi原生绘图工具Paint

2.5 setstrokecap

setstrokecap(paint.cap.round)设置绘制起始点和结尾点的样式,

三种样式round,butt,square

cap.round(圆形)
cap.square(方形)
paint.cap.butt(无)

path path = new path();
path.moveto(100, 100);
path.lineto(100, 200);
path.lineto(200, 300);
canvas.drawpath(path, mpaint2);//无

path.reset();
path.moveto(300, 100);
path.lineto(300, 200);
path.lineto(400, 300);
mpaint2.setstrokecap(paint.cap.round);//圆
canvas.drawpath(path, mpaint2);

path.reset();
path.moveto(500, 100);
path.lineto(500, 200);
path.lineto(600, 300);
mpaint2.setstrokecap(paint.cap.square);//方
canvas.drawpath(path, mpaint2);

Androi原生绘图工具Paint

2.6 setstrokejoin

setstrokejoin(paint.join join),设置绘制path连接点的样式

join.miter(结合处为锐角)
join.round(结合处为圆弧)
join.bevel(结合处为直线)

path path = new path();
path.moveto(100, 100);
path.lineto(100, 200);
path.lineto(200, 300);
mpaint2.setstrokejoin(paint.join.miter);
canvas.drawpath(path, mpaint2);

path.reset();
path.moveto(300, 100);
path.lineto(300, 200);
path.lineto(400, 300);
mpaint2.setstrokejoin(paint.join.round);
canvas.drawpath(path, mpaint2);

path.reset();
path.moveto(500, 100);
path.lineto(500, 200);
path.lineto(600, 300);
mpaint2.setstrokejoin(paint.join.bevel);
canvas.drawpath(path, mpaint2);

Androi原生绘图工具Paint

到此这篇关于androi原生绘图工具paint的文章就介绍到这了,更多相关android原生绘图paint内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!