前言:PathMeasure是Path的测量类,ok!我们来看下有那些用法。
-------------分割线----------
关于PathMeasure(path,false);中的第二个参数,true都会自动测量path包括闭合部分的长度 ,false则反之!
我们来看下具体案例。
代码:
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.util.Log;
import android.view.View;
import static android.R.attr.x;
import static android.R.attr.y;
import static android.content.ContentValues.TAG;
/**
* Created by Fly on 2017/6/14.
*/
public class CustomView extends View {
private Paint paint;
private int mViewHeight;
private int mViewWidth;
public CustomView(Context context) {
super(context);
paint = new Paint();
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(10);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mViewHeight = h;
mViewWidth = w;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.translate(mViewWidth / 2, mViewHeight / 2);
Path path = new Path();
path.lineTo(0, 200);
path.lineTo(200, 200);
path.lineTo(200, 0);
PathMeasure measure1 = new PathMeasure(path, false);
PathMeasure measure2 = new PathMeasure(path, true);
Log.i("---length1---", measure1.getLength() + "");
Log.i("---length2---", measure2.getLength() + "");
//forceClosed:不管path绘制的是否关闭,forceClosed=true都会自动测量path包括闭合部分的长度
canvas.drawPath(path, paint);
}
}
运行效果图:
打印信息:
-------------分割线-------------
nextContour属性:
Path path = new Path();
//多条路径的效果需要关闭硬件加速
path.addRect(-200, -200, 200, 200, Path.Direction.CW);
path.addRect(-100, -100, 100, 100, Path.Direction.CW);
PathMeasure measure = new PathMeasure(path, false);
float length1 = measure.getLength();
boolean nextContour = measure.nextContour();//获取下一个路径,有可能没有多个路径了,返回false
float length2 = measure.getLength();
Log.i("---nextContour---", nextContour + "");
Log.i("---length1---", length1 + "");
Log.i("---length2---", length2 + "");
canvas.drawPath(path, paint);
运行效果图:
日志打印:
----------------分割线---------------
截取片段:
Path path = new Path();
path.addRect(-200, -200, 200, 200, Path.Direction.CW);
PathMeasure measure = new PathMeasure(path, false);
float length = measure.getLength();
Log.i("---length---", length + "");
canvas.drawPath(path, paint);
Path path1 = new Path();
// path1.lineTo(-300, -300);
// path1.lineTo(-200, 0);
// path1.lineTo(-200, 0);
// path1.lineTo(0, 0);
//startWithMoveTo:false,代表该起始点是否位上一个的结束点(是否保持连续性)。
// measure.getSegment(200, 600, path1, false);
measure.getSegment(200, 600, path1, true);
paint.setColor(Color.GREEN);
canvas.drawPath(path1, paint);
效果图:
----------分割线------------
getPosTan:
Path path = new Path();
path.addCircle(0, 0, 300, Path.Direction.CW);
PathMeasure measure = new PathMeasure(path, false);
float[] pos = new float[2];
float[] tan = new float[2];//tan=y/x
measure.getPosTan(measure.getLength() / 4, pos, tan);
Log.i("---------->", "position:x-" + pos[0] + ", y-" + pos[1]);
Log.i("---------->", "tan:x-" + tan[0] + ", y-" + tan[1]);
canvas.drawPath(path, paint);
看下效果:
打印日志:
------------完----------