zoukankan      html  css  js  c++  java
  • android中画弧函数canvas.drawArc()之理解

      在学习android中图形图像处理技术这部分内容时,对绘制圆弧函数canvas.drawArc()的用法、参数含义及画图原理很是不理解,在网上搜索了一些,加上自己的理解,在此做个小总结,作为学习过程中的一个小脚印:

      一. 代码及相应的运行结果

        1.填充圆弧但不含圆心(注意:代码变动部分加粗红色给出,下同

          (1)代码

    1          /***********配置画笔*************/
    2             Paint paint=new Paint();    //采用默认设置创建一个画笔
    3             paint.setAntiAlias(true);//使用抗锯齿功能
    4             paint.setColor(0xFFA4C739);    //设置画笔的颜色为绿色
    5             paint.setStyle(Paint.Style.FILL);//设置画笔类型为填充
    6             /***********绘制圆弧*************/
    7             RectF rectf_head=new RectF(10, 10, 100, 100);//确定外切矩形范围
    8             rectf_head.offset(100, 20);//使rectf_head所确定的矩形向右偏移100像素,向下偏移20像素
    9             canvas.drawArc(rectf_head, -10, -160, false, paint);//绘制圆弧,不含圆心                

           (2) 运行结果

        2.填充圆弧并且含圆心

          (1)代码

    1        /***********配置画笔*************/
    2             Paint paint=new Paint();    //采用默认设置创建一个画笔
    3             paint.setAntiAlias(true);//使用抗锯齿功能
    4             paint.setColor(0xFFA4C739);    //设置画笔的颜色为绿色
    5             paint.setStyle(Paint.Style.FILL);//设置画笔类型为填充
    6             /***********绘制圆弧*************/
    7             RectF rectf_head=new RectF(10, 10, 100, 100);//确定外切矩形范围
    8             rectf_head.offset(100, 20);//使rectf_head所确定的矩形向右偏移100像素,向下偏移20像素
    9             canvas.drawArc(rectf_head, -10, -160, true, paint);//绘制圆弧,含圆心

           (2)运行效果

      

        3.不填充圆弧含圆心

          (1)代码

    1        /***********配置画笔*************/
    2             Paint paint=new Paint();    //采用默认设置创建一个画笔
    3             paint.setAntiAlias(true);//使用抗锯齿功能
    4             paint.setColor(0xFFA4C739);    //设置画笔的颜色为绿色
    5             paint.setStyle(Paint.Style.STROKE);//设置画笔类型为STROKE(个人感觉是描边的意思)
    6             /***********绘制圆弧*************/
    7             RectF rectf_head=new RectF(10, 10, 100, 100);//确定外切矩形范围
    8             rectf_head.offset(100, 20);//使rectf_head所确定的矩形向右偏移100像素,向下偏移20像素
    9             canvas.drawArc(rectf_head, -10, -160, true, paint);//绘制圆弧,含圆心

           (2)运行效果

        4.不填充圆弧不含圆心

          (1)代码

    1        /***********配置画笔*************/
    2             Paint paint=new Paint();    //采用默认设置创建一个画笔
    3             paint.setAntiAlias(true);//使用抗锯齿功能
    4             paint.setColor(0xFFA4C739);    //设置画笔的颜色为绿色
    5             paint.setStyle(Paint.Style.STROKE);//设置画笔类型为STROKE类型(个人感觉是描边的意思)
    6             /***********绘制圆弧*************/
    7             RectF rectf_head=new RectF(10, 10, 100, 100);//确定外切矩形范围
    8             rectf_head.offset(100, 20);//使rectf_head所确定的矩形向右偏移100像素,向下偏移20像素
    9             canvas.drawArc(rectf_head, -10, -160, false, paint);//绘制圆弧,不含圆心

           (2)运行效果

     

     

        5. 总结

          由上面的例子可以看出,是否包含圆心是有函数drawArc()第四个参数决定的(true:包含;false:不包含)。圆弧是否为填充由画笔的类型决定的

    paint.setStyle()的参数决定的(Paint.Style.STROKE:描边(个人的翻译,未必准确);Paint.Style.FILL:填充;Paint.Style.FILL_AND_STROKE:既填充又描边)。

      二.绘制圆弧函数详细分析--drawArc()

         1.官方API:   

      public void drawArc(RectF oval,  float startAngle,  float sweepAngle, boolean useCenter, Paint paint)
    Draw the specified arc, which will be scaled to fit inside the specified oval. If the sweep angle is >= 360, then the oval is drawn completely. Note that this differs slightly from SkPath::arcTo, which treats the sweep angle mod 360. 
    参数:(英文)
    oval - The bounds of oval used to define the shape and size of the arc
    startAngle - Starting angle (in degrees) where the arc begins
    sweepAngle - Sweep angle (in degrees) measured clockwise
    useCenter - If true, include the center of the oval in the arc, and close it if it is being stroked. This will draw a wedge
    paint - The paint used to draw the arc 
       参数:(中文)
      oval -     用于确定圆弧形状与尺寸的椭圆边界(即椭圆外切矩形)
      startAngle -    开始角度(以时钟3点的方向为0°,逆时针为正方向
      sweepAngle -    扫过角度(以时钟3点的方向为0°,逆时针为正方向
      useCenter -     是否包含圆心
      paint -           绘制圆弧的画笔
      2.绘制圆弧的原理
    • RectF(float left, float top, float right, float bottom)得到一个矩形,此虚拟矩形内切绘制一个椭圆(如果长和宽相等,则为圆)。

    • 以矩形的中心为圆心,以时钟3点的方向为0°,逆时针为正方向,从0°正方向旋转startAngle 度,和椭圆相交得到一条直线和一个交点。
    • 从这条直线开始,正方向旋转sweepAngle度,得到另一条直线和交点,这样就得到了一个两交点之间的 圆弧。
      

      

  • 相关阅读:
    mac iterm2 安装 lrzsz rz sz命令
    bash rz 上传文件失败问题
    vue-router scrollBehavior无效的问题及解决方案
    dependencies devDependencies peerDependencies optionalDependencies区别
    Nuxt / Vue.js in TypeScript: Object literal may only specify known properties, but 'components' does not exist in type 'VueClass'
    NodeJS中的require和import
    Git fetch和git pull的区别, 解决Git报错:error: You have not concluded your merge (MERGE_HEAD exists).
    webstorm打开带有node_modules文件夹的工程时很卡
    CSS3自定义滚动条样式 -webkit-scrollbar
    nginx 中location和root、alias
  • 原文地址:https://www.cnblogs.com/tjudzj/p/4387145.html
Copyright © 2011-2022 走看看