概念
路径测量,一个用来测量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)