PathMeasure

构造方法

PathMeasure();
PathMeasure(Path path, boolean forceClosed);
//forceClosed	是否close这个path

常用API

//设置path,与构造方法配合使用
setPath(Path path, boolean forceClosed);
//测量Path的长度
getLength();
//判断轮廓路径是否是闭环的
isClosed();
//移动到下一个轮廓,如轮廓有多个path,则返回true.
nextContour();
getMatrix(float distance, Matrix matrix, int flags);
//根据距离返回坐标点和点的切线值并存入pos与tan
//0 <= distance <= getLength()
getPosTan(float distance, float[] pos, float[] tan);
//传入一个开始和结束距离(0..getLength()),然后会返回介于这之间的Path并传入到dst中
getSegment(float startD, float stopD, Path dst, boolean startWithMoveTo);
//startWithMoveTo	决定开始距离是否从MoveTo坐标为参照物

案例

//创建轨道path
private Path trackPath = new Path();
//创建目标path
private Path dstPath = new Path();
private Paint paint;
private PathMeasure mPathMeasure;
//进度
private float fraction;

@Override
protected void onDraw(Canvas canvas) {
    //重置两个path
    trackPath.reset();
    dstPath.reset();
    //为轨道path添加路径
    trackPath.addCircle(centerX, centerY, 100, Path.Direction.CCW);
    //为mPathMeasure设置路径
    mPathMeasure.setPath(trackPath, false);
    //将mPathMeasure中的路径的一部分生成到目标path中
    mPathMeasure.getSegment(0, mPathMeasure.getLength() * fraction, dstPath, false);
    //绘制目标path
    canvas.drawPath(dstPath, paint);
}
public void goPathAnim(long duration) {
    ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 1);
    valueAnimator.setDuration(duration);
    valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            //生成进度
            fraction = (Float) animation.getAnimatedValue();
            //刷新操作
            postInvalidate();
        });
        valueAnimator.start();
    }
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐