Android PathMeasure详细解读

Android PathMeasure是一个用于测量和操作Path的工具类。它可以计算Path的长度、跳转Path、获取Path的片段。forceClosed参数可以强制Path闭合,影响getLength()的返回值。getSegment()方法用于获取Path上的指定段落,并可在目标Path上绘制。当forceClosed为true时,即使原始Path未闭合,也会被当作闭合Path处理。
摘要由CSDN通过智能技术生成

概念

路径测量,一个用来测量Path的工具类

常用API

常用API如Path长度测量,Path跳转,Path片段获取、

构造方法
PathMeasure()
PathMeasure(Path path, boolean forceClosed) forceClosedPath决定是否需要闭合

基本代码

public class PathMeasureView extends View {

    private Paint mPaint = new Paint();
    private Paint mLinePaint = new Paint(); //坐标系
    private Bitmap mBitmap;

    public PathMeasureView(Context context) {
        super(context);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.BLACK);
        mPaint.setStrokeWidth(4);

        mLinePaint.setStyle(Paint.Style.STROKE);
        mLinePaint.setColor(Color.RED);
        mLinePaint.setStrokeWidth(6);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawLine(0, getHeight() / 2, getWidth(), getHeight() / 2, mLinePaint);
        canvas.drawLine(getWidth() / 2, 0, getWidth() / 2, getHeight(), mLinePaint);
		//将canvas中心移动到屏幕的中心,两条线相交的位置
        canvas.translate(getWidth() / 2, getHeight() / 2);
        ....
         }
  }

在这里插入图片描述

PathMeasure无参构造

pathMeasure需要通过setPath方法关联一个创建好的path,要不无法使用。

PathMeasure.setPath()

public void setPath (Path path, boolean forceClosed)

通过PathMeasure.setPath的方式来将Path和PathMeasure进行绑定。

如果Path进行了调整,需要重新调用setPath方法进行关联,也就是说 pathMeasure.getLength()方法得到的值的之前的。
需要重新调用setPath方法来更新值。

PathMeasure有参构造

public PathMeasure (Path path, boolean forceClosed)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值