自定义View之Path的使用

本文详细介绍了Android图形API中的Path类,包括Path的构造函数、枚举值、方法及其用法。Path用于记录和绘制复杂的几何路径,涉及直线、曲线等元素。文章通过实例展示了Line系列、addXXX系列和is系列的方法,并探讨了Path在自定义View中的应用,如绘制矩形、圆形、贝塞尔曲线等。同时,文章提到了Path的填充模式、布尔运算和路径变换等高级特性。
摘要由CSDN通过智能技术生成

Path是什么?

The Path class encapsulates compound (multiple contour) geometric paths consisting of straight line segments, quadratic curves, and cubic curves. It can be drawn with canvas.drawPath(path, paint), either filled or stroked (based on the paint's Style), or it can be used for clipping or to draw text on a path.

Path类封装了由直线段,二次贝塞尔曲线和三次贝塞尔曲线组成的复合几何路径。它可以使用canvas.drawPath(path,paint)绘制,填充或描边,或者它可以用于剪切或在路径上绘制文本。

我们此处说的Path就是单纯的路径,不是文件的路径,是在android.graphics包下的.它在自定义View的时候能帮我们记录路径,然后调用Canvas.drawPath就可以画出.这个图形可以是简单的几何图形,也可以使圆弧等,可以使用到贝塞尔曲线(额,本人大专生,先开始看到这个名词也是一脸懵逼,那么api中只给出了二阶和三阶的贝塞尔曲线,如有需要,自行百度了解).

Path的构造函数?

第一个创建一个空的Path

第二个创建一个拥有路径的Path

Path中的三个枚举值?

Path.Direction     方向,CW为顺时针,CCW为逆时针

Path.FillType       一共有4种填充类型,看下去你就知道了

Path.Op                用来做布尔运算,可以简单的理解为交集,并集等的运算

不要着急,在接下来的操作中我们会一一介绍

Path的方法?

下面一一介绍咯,看好,走你~

在说怎么用之前,先说一下,关于Rect,Canvas以及Paint,Point的用法,请看我的另外文章

自定义View之Rect的使用与理解

自定义View之Canvas的使用

自定义View之Point的使用

paint的文章暂时没有写,上面两篇文章也不完整,但快了,后续会把这里更新了.

下面直接给出自定义View的代码以及xml文件,防止你说我不给源码

public class MyPathView extends View {
    private static final String TAG = "MyPathView";

    private Paint mPaint;
    private Paint mMyPathViewPaint;

    public MyPathView(Context context) {
        this(context,null);
    }

    public MyPathView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MyPathView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        
        /*
            初始化画笔,相关知识请查看我写的其他博客进行学习,这里主要讲的是Path的用法与理解
         */
        mPaint = new Paint();
        mPaint.setAntiAlias(true);                  //抗锯齿
        mPaint.setStyle(Paint.Style.STROKE);        //设置画笔风格为描边
        mPaint.setColor(Color.RED);                 //画笔颜色
        mPaint.setStrokeWidth(2);                   //描边的宽度
        mPaint.setTextSize(30);                     //字体大小

        /*
            这里是我们用到的画笔
        */
        mMyPathViewPaint = new Paint();
        mMyPathViewPaint.setAntiAlias(true);
        mMyPathViewPaint.setStyle(Paint.Style.STROKE);
        mMyPathViewPaint.setColor(Color.BLACK);
        mMyPathViewPaint.setStrokeWidth(2);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//       这里是我用来画坐标轴的,注意android的坐标轴y轴向下才是正的
        canvas.drawLine(getWidth()/2,0,getWidth()/2,getHeight(),mPaint);
        canvas.drawLine(getWidth()/2,getHeight(),getWidth()/2-20,getHeight()-20,mPaint);
        canvas.drawLine(getWidth()/2,getHeight(),getWidth()/2+20,getHeight()-20,mPaint);
        canvas.drawLine(0,getHeight()/2,getWidth(),getHeight()/2,mPaint);
        canvas.drawLine(getWidth(),getHeight()/2,getWidth()-20,getHeight()/2-20,mPaint);
        canvas.drawLine(getWidth(),getHeight()/2,getWidth()-20,getHeight()/2+20,mPaint);
        canvas.drawText("Y",getWidth()/2+10,getHeight()-30,mPaint);
        canvas.drawText("X",getWidth()-50,getHeight()/2-10,mPaint);

//       之后的代码都是写在这里的,这个是模板

    }
}
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <xhj.zime.com.pathdemo.MyPathView
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</android.support.constraint.ConstraintLayout>

Path怎么用?

个人把里面的方法分为这几大类

1.Line系列

2.addXXX系列

3.is系列

4.没有系列的系列

接下来我们一起来细看Path的一些用法:

Line系列:

line系列我总结的主要方法有这几个,综合起来看效果比较好:

第一组:

public void moveTo(float x, float y)
public void lineTo(float x, float y)
public void setLastPoint(float dx, float dy)
public void close()

不使用setLastPoint:

        can
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值