Android Path详解

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

你可能感兴趣的:(Android-自定义View,Android,自定义view,动画)