Android开发之PathMeasure的基本用法

前言: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);
看下效果:


打印日志:


------------完----------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

等待着冬天的风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值