您好,登录后才能下订单哦!
这篇文章主要介绍Androi原生绘图工具Paint怎么用,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
前言:
生活中绘制图案需要纸和笔,android
中提供了类似的工具Canvas
和Paint
,分别对应画布和画笔。
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
上,然后渲染到屏幕上。
Paint提供了三种初始化方法:
Paint() Paint(int flag) Paint(Paint paint)
setAlpha(int a) 设置范围0~255
填充样式表示绘线和填充的选择:
Paint.Style.FILL Paint.Style.FILL_AND_STROKE Paint.Style.STROKE
setColor
,setARGB
设置颜色,设置画笔的颜色,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);
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);
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);
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怎么用”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。