Android Path详解

    • Path内部类
      • Path.Direction
      • Path.FillType
      • Path.Op
    • Path的绘制
      • 1.矩形路径
      • 2.椭圆路径
      • 3.圆角矩形路径
      • 4.圆形路径
      • 5.弧形路径
      • 6.直线路径

Path内部类

Path.Direction

  • Path.Direction.CCW:counter-clockwise的缩写,逆时针创建路径。
  • Path.Direction.CW:clockwise的缩写,顺时针创建路径。

Path.FillType

Path.Op

Path的绘制

1.矩形路径

  • addRect(float left, float top, float right, float bottom, Path.Direction dir)
  • addRect(RectF rect, Path.Direction dir)
Path pathRect1 = new Path();RectF rectFRect1 = new RectF(50, 200 + paddingTop, 500, 400);pathRect1.addRect(rectFRect1, Path.Direction.CCW);mPaint.setStyle(Paint.Style.STROKE);mPaint.setStrokeWidth(5);canvas.drawPath(pathRect1, mPaint);

2.椭圆路径

  • addOval(RectF oval, Path.Direction dir)
  • addOval(float left, float top, float right, float bottom, Direction dir)
Path pathOval = new Path();RectF rectFOval1 = new RectF(50, 600 + paddingTop, 500, 800);pathOval.addOval(rectFOval1, Path.Direction.CW);mPaint.setStyle(Paint.Style.STROKE);mPaint.setStrokeWidth(5);canvas.drawPath(pathOval, mPaint);

3.圆角矩形路径

  • addRoundRect(RectF rect, float[] radii, Path.Direction dir)
  • addRoundRect(float left, float top, float right, float bottom, float[] radii,Direction dir)
  • addRoundRect(RectF rect, float rx, float ry, Path.Direction dir)
  • addRoundRect(float left, float top, float right, float bottom, float rx, float ry,Direction dir)
//统一圆角Path pathRoundRect1 = new Path();RectF rectFRoundRect = new RectF(50, 1000 + paddingTop, 500, 1200);pathRoundRect1.addRoundRect(rectFRoundRect,50,50,Path.Direction.CW);        mPaint.setStyle(Paint.Style.STROKE);mPaint.setStrokeWidth(5);canvas.drawPath(pathRoundRect1, mPaint);drawRightDescr(canvas, 1100 + paddingTop, "统一四角,addRoundRect");//四角自定义Path pathRoundRect2 = new Path();RectF rectFRoundRect2 = new RectF(50, 1200 + paddingTop, 500, 1400);float[] radii={0,0,50,50,0,0,50,50};pathRoundRect2.addRoundRect(rectFRoundRect2,radii,Path.Direction.CW);mPaint.setStyle(Paint.Style.STROKE);mPaint.setStrokeWidth(5);canvas.drawPath(pathRoundRect2, mPaint);

4.圆形路径

  • addCircle(float x, float y, float radius, Path.Direction dir)
    • x:圆心点X轴坐标。
    • y:圆心点Y轴坐标。
    • radius:半径。
    • Path.Direction:绘制方向。
Path pathCircle = new Path();pathCircle.addCircle(250,1700,100,Path.Direction.CW);mPaint.setStyle(Paint.Style.STROKE);mPaint.setStrokeWidth(5);canvas.drawPath(pathCircle, mPaint);

5.弧形路径

  • addArc(RectF oval, float startAngle, float sweepAngle)
Path pathArc = new Path();RectF rectFArc = new RectF(50,2000+paddingTop,500,2200);pathArc.addArc(rectFArc,0,120);mPaint.setStyle(Paint.Style.STROKE);mPaint.setStrokeWidth(5);canvas.drawPath(pathArc, mPaint);

6.直线路径

  • moveTo(float x, float y):设置下一个轮廓线的起点为点(x,y)。
  • lineTo(float x, float y):从最后一个点到指定的点(x,y)添加新的一行。如果在此之前没有调用过moveTo()设置起点,则自动将起点设置为(0,0)。
  • close():关闭当前轮廓线。如果当前点不等于轮廓线的第一个点,则自动添加线段。
//不闭合Path pathLines1 = new Path();pathLines1.moveTo(250,2400+paddingTop);pathLines1.lineTo(50,2600);pathLines1.lineTo(450,2600);mPaint.setStyle(Paint.Style.STROKE);mPaint.setStrokeWidth(5);canvas.drawPath(pathLines1,mPaint);//闭合Path pathLines2 = new Path();pathLines2.moveTo(250,2600+paddingTop);pathLines2.lineTo(50,2800);pathLines2.lineTo(450,2800);pathLines2.close();mPaint.setStyle(Paint.Style.STROKE);mPaint.setStrokeWidth(5);canvas.drawPath(pathLines2,mPaint);//五角星闭合Path pathLines3 = new Path();pathLines3.moveTo(250,2800+paddingTop);pathLines3.lineTo(100,3000);pathLines3.lineTo(400,2900);pathLines3.lineTo(100,2900);pathLines3.lineTo(100,2900);pathLines3.lineTo(400,3000);pathLines3.close();mPaint.setStyle(Paint.Style.STROKE);mPaint.setStrokeWidth(5);canvas.drawPath(pathLines3,mPaint);        

作者:Brainbg(白雨)
GitHub:https://github.com/Brainbg
博客:https://www.brainbg.com/
简书:https://www.jianshu.com/u/94518ede7100
CSDN:https://blog.csdn.net/u014720022

更多相关文章

  1. xcode与eclipse整合cocos2dx
  2. Android仿微信图片选择器(二)
  3. IDA 调试SO
  4. MAC NDK开发环境搭建
  5. eclipse中运行安卓程序,提示Failed to allocate memory: 8的解决
  6. android获取网络图片过大的问题
  7. Android(安卓)RectF()用法
  8. Android使用Drawable实现圆角矩形
  9. Android通过路径获取URI(全版本)

随机推荐

  1. android webview file标签点击弹出选择文
  2. android 6.0及以上危险权限的获取
  3. android事件分发机制一
  4. Mac 完全卸载 Android(安卓)Studio
  5. android 按钮置灰效果
  6. Android(安卓)Studio 使用技巧(6)
  7. android studio 配置Kotlin报错Error:Cau
  8. android host修改
  9. Android(安卓)Studio常用插件
  10. TabHost