PathMeasure相关知识及运用

PathMeasure详解文章:
[eclipse_xu]:http://www.jianshu.com/p/3efa5341abcc
[GcsSloop]自定义View魔法系列:http://www.gcssloop.com/customview/Path_PathMeasure/
[扔物线]开发进阶系列:https://zhuanlan.zhihu.com/p/27787919

运用demo来自[Anonymous___]:http://www.jianshu.com/p/4bb16cefca23

实践+注解

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.graphics.RectF;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.mps.hencoder.R;

/**
 * 路径绘制: 主要使用PathMeasure类
 * Created by F011512088 on 2017/12/4.
 */

public class GranzortView extends View {

    private final String TAG = GranzortView.class.getSimpleName();

    private Paint paint;

    private float strokeWidth = 10;
    private float differ = 20;
    private float maxRadius;

    private float mViewWidth;
    private float mViewHeight;

    private Path innerCircle;//内圆 path
    private Path outerCircle;//外圆 path
    private Path trangle1;//第一个三角形的 Path
    private Path trangle2;//第二个三角形的 Path
    private Path drawPath;//用于截取路径的 Path,用于保存PathMeasure路径
    private float distance = 0;//当前动画执行的百分比取值为0-1

    private int duration = 3000;
    private ValueAnimator valueAnimator;
    private ValueAnimator.AnimatorUpdateListener animatorUpdateListener;
    private ValueAnimator.AnimatorListener animatorListener;

    private PathMeasure pathMeasure;

    private State mCurrentState = State.CIRCLE_STATE;
    private Handler mHanlder;

    //三个阶段的枚举
    private enum State {
        CIRCLE_STATE,
        TRANGLE_STATE,
        FINISH_STATE
    }

    public GranzortView(Context context) {
        super(context);
        init();
    }

    public GranzortView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public GranzortView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {

        initPaint();

        initPath();

        initAnimatorListener();

        initAnimator();

        initHandler();

        mCurrentState = State.CIRCLE_STATE;
        valueAnimator.start();
    }

    private void initPath() {
        innerCircle = new Path();
        outerCircle = new Path();

        trangle1 = new Path();
        trangle2 = new Path();

        drawPath = new Path();

        pathMeasure = new PathMeasure();

    }

    private void initPaint() {
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.WHITE);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(strokeWidth);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setStrokeJoin(Paint.Join.BEVEL);
        paint.setShadowLayer(15, 0, 0, Color.WHITE);//白色光影效果
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mViewWidth = getMeasuredWidth();
        mViewHeight = getMeasuredHeight();
        setViewPaint();
    }

    private void setViewPaint() {
        float minLength = Math.min(mViewWidth / 2, mViewHeight / 2);
        maxRadius = (minLength - strokeWidth);
        //画布原点转为中心点
        RectF innerRect = new RectF(-maxRadius + differ, -maxRadius + differ, maxRadius - differ, maxRadius - differ);
        RectF outerRect = new RectF(-maxRadius, -maxRadius, maxRadius, maxRadius);
        innerCircle.addArc(innerRect, 150, -359.9F);     // 不能取360f,否则可能造成测量到的值不准确
        outerCircle.addArc(outerRect, 60, -359.9F);

        pathMeasure.setPath(innerCircle, false);
        /**
         * boolean getPosTan (float distance, float[] pos, float[] tan)
         * 用于获取路径上某点的坐标及其切线的坐标
         * 描述: 通过指定distance(0

你可能感兴趣的:(PathMeasure相关知识及运用)