美文网首页
PathMeasure的使用 2019-10-30

PathMeasure的使用 2019-10-30

作者: GoodWen | 来源:发表于2019-10-30 13:49 被阅读0次
1.png 2.png

需要用到的素材


arrow.jpg

代码演练 PathMeasureView 最终效果围着圆旋转的箭头

public class PathMeasureView extends View {
    private Paint mPaint = new Paint();
    private Paint mLinePaint = new Paint();
    private Bitmap mBitmap;

    public PathMeasureView(Context context) {
        this(context, null);
    }

    public PathMeasureView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PathMeasureView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.BLACK);
        mPaint.setStrokeWidth(4);

        mLinePaint.setStyle(Paint.Style.STROKE);
        mLinePaint.setColor(Color.RED);
        mLinePaint.setStrokeWidth(6);

        //缩小图片
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 4;
        mBitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.arrow, options);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @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.translate(getWidth() / 2, getHeight() / 2);

//        Path path = new Path();
//        path.lineTo(0, 200);
//        path.lineTo(200, 200);
//        path.lineTo(200,0);
//
//        PathMeasure pathMeasure = new PathMeasure();
//        pathMeasure.setPath(path,true);//true  或者false   影响路径测量结果
//        Log.e("", "forceClosed == true: "+pathMeasure.getLength() );//800
//        PathMeasure pathMeasure1 = new PathMeasure();
//        pathMeasure1.setPath(path,false);
//        Log.e("", "forceClosed == false: "+pathMeasure1.getLength() );//600
//
//        PathMeasure pathMeasure2 = new PathMeasure(path, false);
//        Log.e("", "直接构造方法forceClosed == false: "+pathMeasure2.getLength() );//600
//        //需要注意的是在path被更改之后需要重新调用 setPath()方法才能生效
//        path.lineTo(200,-200);
//        pathMeasure2.setPath(path,false);
//        Log.e("", "path值3更改以后forceClosed == false: "+pathMeasure2.getLength() );//600


//        Path path = new Path();
//        path.addRect(-200, -200, 200, 200, Path.Direction.CW);
//
//        Path dst = new Path();
//        dst.lineTo(-300,-300);
//        PathMeasure pathMeasure = new PathMeasure(path, false);
//        //截取一部分存入dst中,并且使用moveTo方法保持截取得到的path第一个点的位置不变
//        pathMeasure.getSegment(200, 1000, dst, false);
//
//        canvas.drawPath(path, mPaint);
//        canvas.drawPath(dst,mLinePaint);


//        Path path = new Path();
//        path.addRect(-100, -100, 100, 100, Path.Direction.CW);
//        path.addOval(-200, -200, 200, 200, Path.Direction.CW);
//        canvas.drawPath(path, mPaint);
//        PathMeasure pathMeasure = new PathMeasure(path, false);
//        Log.e("", "forceClosed == false: "+pathMeasure.getLength() );
//        //跳转到下一段曲线
//        pathMeasure.nextContour();
//        Log.e("", "forceClosed == false: "+pathMeasure.getLength() );

        mPath.reset();
        mPath.addCircle(0, 0, 200, Path.Direction.CW);
        canvas.drawPath(mPath, mPaint);


        mFloat += 0.01;
        if (mFloat >= 1) {
            mFloat = 0;
        }

//        PathMeasure pathMeasure = new PathMeasure(mPath, false);
//        pathMeasure.getPosTan(pathMeasure.getLength() * mFloat, pos, tan);
//        Log.e("", "pos[0]==" + pos[0] + "    pos[0]==" + pos[1]);
//        Log.e("", "tan[0]==" + tan[0] + "    tan[0]==" + tan[1]);
//
//        //当前点的切线与X轴的角度
//        double degrees = Math.atan2(tan[1], tan[0]) * 180.0 / Math.PI;
//        Log.e("", "角度: " + degrees);
//        mMatrix.reset();
//        //进行角度旋转
//        mMatrix.postRotate((float) degrees, mBitmap.getWidth() / 2, mBitmap.getHeight() / 2);
//        //将图片的绘制中心与当前点重合
//        mMatrix.postTranslate(pos[0] - mBitmap.getWidth() / 2, pos[1] - mBitmap.getHeight() / 2);
//        canvas.drawBitmap(mBitmap, mMatrix, mPaint);


        PathMeasure pathMeasure = new PathMeasure(mPath, false);
        //将pos信息和tan信息保存在mMatrix中
        pathMeasure.getMatrix(pathMeasure.getLength() * mFloat, mMatrix, PathMeasure.POSITION_MATRIX_FLAG | PathMeasure.TANGENT_MATRIX_FLAG);
        //将图片的旋转坐标调整到图片中心位置
        mMatrix.preTranslate(-mBitmap.getWidth() / 2, -mBitmap.getHeight() / 2);

        canvas.drawBitmap(mBitmap, mMatrix, mPaint);


        invalidate();
    }

    private Matrix mMatrix = new Matrix();
    private Path mPath = new Path();
    private float[] pos = new float[2];
    private float[] tan = new float[2];
    private float mFloat;
}

相关文章

网友评论

      本文标题:PathMeasure的使用 2019-10-30

      本文链接:https://www.haomeiwen.com/subject/nhuovctx.html