经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
自定义View入门-绘制基础(1)
来源:cnblogs  作者:安卓干货铺  时间:2019/9/23 8:36:04  对本文有异议
### 前言 说道自定义View,我们一定会想到,自定义View的绘制流程 - 测量阶段(measure) - 布局阶段(layout) - 绘制阶段(draw) 我们看到的一些炫酷的view效果,都是在绘制方法里去实现的, 也就是`draw(Canvas)`, 我们先放下 测量与布局, 先从绘制基础开始学起。 ### 详解 说到`ondraw(Canvas)`方法,不得不提`Paint`与`Canvas`。我们先来看`Paint` ###### 1.Paint Paint就是"画笔",我们先去看下Paint类的源码解释: ``` ** * The Paint class holds the style and color information about how to draw * geometries, text and bitmaps. */ ``` Paint类可以画几何图形,文本与bitmap。 Paint类方法比较多, 这里拿Paint.Style举例: - Paint.Style.FILL:填充内部 - Paint.Style.FILL_AND_STROKE :填充内部和描边 - Paint.Style.STROKE :描边 ![](https://img2018.cnblogs.com/blog/1312938/201909/1312938-20190921160047699-1410664224.png) ###### 2.Canvas **(1).定义** Canvas就是“画布”,我们先去看下Canvas类的源码解释: ``` * 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). ``` - 承载像素的位图 - 持有绘画方法调用的画布 - 描述画图颜色和风格的画笔 - 画图的类型。 **(2).绘制方法** 方法比较多了,这里我就随便举几个例子: - 画线 ``` Paint paint=new Paint(); paint.setColor(Color.BLUE); paint.setStrokeWidth(20); paint.setStyle(Paint.Style.FILL); canvas.drawLine(200,200,450,200,paint); ``` ![](https://img2018.cnblogs.com/blog/1312938/201909/1312938-20190921160048089-269409115.png) - 画矩形 ``` Paint paint=new Paint(); paint.setColor(Color.BLUE); paint.setStrokeWidth(50); paint.setStyle(Paint.Style.FILL ); canvas.drawRect(100,100,200,200,paint); ``` ![](https://img2018.cnblogs.com/blog/1312938/201909/1312938-20190921160048326-68976265.png) - 画扇形-140度 ``` Paint paint=new Paint(); paint.setColor(Color.BLUE); paint.setStrokeWidth(50); paint.setStyle(Paint.Style.FILL ); canvas.drawArc(100,100,400,400,0,140,false,paint); ``` ![](https://img2018.cnblogs.com/blog/1312938/201909/1312938-20190921160048569-499644571.png) 更多的方法以及方法的含义可以去下面的API地址去看! [API地址](https://developer.android.google.cn/reference/android/graphics/Canvas.html) 今天就讲到这里 ,绘制基础还有一个非常重要的类,**Paht(路径)类**,下一节讲一下。 希望对大家有所帮助! 大家可以关注我的微信公众号:「秦子帅」一个有质量、有态度的公众号! ![公众号](https://img2018.cnblogs.com/blog/1312938/201909/1312938-20190921160048726-1254022071.jpg)

原文链接:http://www.cnblogs.com/qinzishuai/p/11563423.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号