美文网首页
Android PathMeasure小案例-箭头沿环形路径旋转

Android PathMeasure小案例-箭头沿环形路径旋转

作者: 刘小厨 | 来源:发表于2020-04-02 20:55 被阅读0次

概述

本文承接Android PathMeasure总结

下述小案例仅为PathMeasure的api的使用总结,非项目使用demo!!!
先看下效果~

PathMeasure小案例

贴下代码,有问题可见:Android PathMeasure总结

注:注释部分代码与下面使用Matrix代码效果相同

public class PathMeasureView extends View {

    private Paint mPaint = new Paint();
    private Paint mLinePaint = new Paint(); //坐标系
    private Bitmap mBitmap;
    private Matrix mMatrix = new Matrix();
    private float[] pos = new float[2];
    private float[] tan = new float[2];
    private Path mPath = new Path();
    private float mFloat;

    public PathMeasureView(Context context) {
        super(context);
        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(getResources(), R.mipmap.arrow,options);
    }

    @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);
        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("TAG", "onDraw: pos[0]="+pos[0]+";pos[1]="+pos[1]);
////        Log.e("TAG", "onDraw: tan[0]="+tan[0]+";tan[1]="+tan[1]);
////
//        //计算出当前的切线与x轴夹角的度数
//        double degrees = Math.atan2(tan[1], tan[0]) * 180.0 / Math.PI;
////        Log.e("TAG", "onDraw: degrees="+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信息保存在m中
        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();
    }
}

相关文章

网友评论

      本文标题:Android PathMeasure小案例-箭头沿环形路径旋转

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