Android path 路径相关

作者: zhaoyubetter | 来源:发表于2017-03-15 16:09 被阅读1670次

    参考:
    https://juejin.im/post/58ce7fe561ff4b006c9a63c6
    https://juejin.im/post/58c3b88a44d904006984e58d
    https://github.com/lguipeng/BubbleView
    https://ghui.me/post/2015/10/android-graphics-path/
    http://www.jianshu.com/p/d6b4a9ad022e
    弹性圆的绘制:http://www.jianshu.com/p/791d3a791ec2
    google api demo
    PathMeasure
    http://blog.csdn.net/u013831257/article/details/51565591

    贝塞尔曲线:
    https://juejin.im/post/58ce7f0461ff4b006c9a5e66

    Path##

    Path 表示路径,可使用Canvas.drawPath方法将其绘制出来,Path不仅可以使用Paint的填充模式和描边模式,也可以用画布裁剪和或者画文字。

    Path 是Android中一种绘制工具,表示路径,可以画各种复杂的图形,如:曲线、轮廓等等;

    基本方法##

    **1. moveTo(x, y) ** : 移动到起点;
    ** 2. lineTo(x, y) **: 绘制直线;
    3. arcTo(rectF, startAngle,sweepAngle): **
    在RectF中绘制弧度(rect为弧度的外切rect), 从起点角度start,绘制sweep个 角度;
    ** 4. arcTo(rectF, startAngle, sweepAngle, true):
    表示在rect绘制弧度,并且与 moveTo 的起点分离,类似于另开一个Path;文档解释为:
    If true, always begin a new contour with the arc

            mPath.reset();
            RectF rect = new RectF(100, 100, 200, 200);
            mPaint.setColor(Color.RED);
            canvas.drawRect(rect, mPaint);
            mPaint.setColor(Color.GREEN);
            mPath.arcTo(rect, 0, 90);
            canvas.drawPath(mPath, mPaint);
    

    效果图:

    arcTo

    **5. reset(): ** 表示清空 path;
    **6. addOval(rectF, direction): ** 添加一个椭圆,rectF为此椭圆的外切rect;

    canvas.translate(200, 200);
    RectF oval = new RectF(100, 100, 300, 400);
    mPaint.setColor(Color.RED);
    canvas.drawRect(oval, mPaint);
    mPaint.setColor(Color.GREEN);
    mPath.addOval(oval, Path.Direction.CW);// 外,CCW,内
    canvas.drawPath(mPath, mPaint);
    // 绘制路径上的文字
    canvas.drawTextOnPath("sdjfsklfjksiewjf快睡觉富士康福克斯司法局斯柯达", mPath, 0, 0, mTextPaint);
    canvas.restore();
    

    效果图:

    drawOval
    **7.addCircle(x, y, radius,Direction) : ** 绘制圆形;
    **8.addPath(path) : ** 添加新的path到当前path;
    9.quadTo(x1,y1,x2,y2) : 绘制二次贝塞尔曲线,其中 (x1,y1)为控制点,(x2,y2)为终点
    // 曲线
            mPath.moveTo(50, 50);
            // 贝塞尔曲线(二阶)
            mPath.quadTo(200, 200, 300, 100);   // 以(200,200)为控制点,画贝塞尔曲线
            canvas.drawPath(mPath, mPaint);
    

    图示:

    二级贝塞尔
    10.cubicTo(x1, y1, x2, y2, x3, y3):三阶贝塞尔,其中(x1,y1),(x2,y2)为控制点,(x3,y3)为终点

    rXXX方法##

    rXXX与原方法的区别是:r方法是基于当前绘制开始点的offest,比如当前paint位于 (100,100)处,则使用rLineTo(100,100)方法绘制出来的直线是从(100,100)到(200,200)的一条直线,由此可见rXXX方法方便用来基于之前的绘制作连续绘制。

            mPath.reset();
            canvas.translate(500, 500);
            mPath.moveTo(100, 100);
            mPath.lineTo(400, 400);
            mPath.lineTo(400, 100);
    
            mPath.rLineTo(100, 300);  // mPath.LineTo(100, 300)
            canvas.drawPath(mPath, mPaint);
    

    示例:

    rLineTo(100, 300) lineTo(100, 300)

    Path.op方法(API >= 19 )##

    用于将2个对象path做相应的运算组合,有点类似于数学上的集合运算

            canvas.translate(0, 200);
            Path path1 = new Path();
            path1.addCircle(150, 150, 100, Path.Direction.CW);
            Path path2 = new Path();
            path2.addCircle(200, 200, 100, Path.Direction.CW);
            path1.op(path2, Path.Op.INTERSECT); // (相交的部分)Union (inclusive-or) the two paths
            canvas.drawPath(path1, mPaint);
    

    图示:

    Path.Op.INTERSECT

    总结如下:

    1. Path.Op.DIFFERENCE 减去path1中path1与path2都存在的部分;
      path1 = (path1 - path1 ∩ path2)
    2. Path.Op.INTERSECT 保留path1与path2共同的部分;
      path1 = path1 ∩ path2
    3. Path.Op.UNION 取path1与path2的并集;
      path1 = path1 ∪ path2
    4. Path.Op.REVERSE_DIFFERENCE 与DIFFERENCE刚好相反;
      path1 = path2 - (path1 ∩ path2)
    5. Path.Op.XOR 与INTERSECT刚好相反;
      path1 = (path1 ∪ path2) - (path1 ∩ path2)

    path的例子:圆角矩形##

    在Android 中可用 canvas.drawRoundRect 来实现圆角矩形,如果不怕麻烦,
    也可以使用 path 来实现;
    如下例子:

      private static final float DEFAULT_RADIUS = 50;
    
    @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            drawRect(canvas);
        }
    
        private void drawRect(Canvas canvas) {
    
            RectF rectF = null;
            // 圆弧起点
            mPath.moveTo(100 + DEFAULT_RADIUS, 100);
            mPath.lineTo(500 - DEFAULT_RADIUS, 100);
    
            // 右上角圆角 (从 rectF 的中间开始画, path 为路径所以会连起来)
            rectF = new RectF(500 - DEFAULT_RADIUS, 100, 500, (100 + DEFAULT_RADIUS));
            mPath.arcTo(rectF, 270, 90);
    
            // 右下圆角
            mPath.lineTo(500, 300 - DEFAULT_RADIUS);
            rectF = new RectF(500 - DEFAULT_RADIUS, 300 - DEFAULT_RADIUS, 500, 300);
            mPath.arcTo(rectF, 0, 90);
    
            // 左下圆角
            mPath.lineTo(100 + DEFAULT_RADIUS, 300);
            rectF = new RectF(100, 300 - DEFAULT_RADIUS, 100 + DEFAULT_RADIUS, 300);
            mPath.arcTo(rectF, 90, 90);
    
            // 左上圆角
            mPath.lineTo(100, 100 + DEFAULT_RADIUS);
            rectF = new RectF(100, 100, 100 + DEFAULT_RADIUS, 100 + DEFAULT_RADIUS);
            mPath.arcTo(rectF, 180, 90);
    
    //      mPath.lineTo(100, 300);
            mPath.close();
    
            canvas.drawPath(mPath, mPaint);
        }
    
    

    效果图:

    path圆角

    聊天气泡形状##

    这用drawRoundRect就不能实现了。使用path,简单修改一下就可以了

                  //。。。。省略
                  // 左下圆角
            mPath.lineTo(100 + DEFAULT_RADIUS, 300);
            rectF = new RectF(100, 300 - DEFAULT_RADIUS, 100 + DEFAULT_RADIUS, 300);
            mPath.arcTo(rectF, 90, 90);
    
    
            // 小三角形,
            mPath.lineTo(100, 100 + DEFAULT_RADIUS + 50);
            mPath.lineTo(100 - 50, 100 + DEFAULT_RADIUS + 25);
            mPath.lineTo(100, 100 + DEFAULT_RADIUS);
    
            // 左上圆角
            rectF = new RectF(100, 100, 100 + DEFAULT_RADIUS, 100 + DEFAULT_RADIUS);
            mPath.arcTo(rectF, 180, 90);
    
            mPath.close();
    
            canvas.drawPath(mPath, mPaint);
    

    效果图:


    气泡轮廓图

    贝塞尔曲线##

    贝塞尔曲线需要三个点才能确定,所以quadTo方法中的四个参数分别是确定第二,第三的点的。第一个点就是path上次操作的点。

    画一条波浪线:

    int WAVE_HEIGHT = 88;
     public WaveView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            mPaint.setColor(Color.RED);
            mPaint.setStyle(Paint.Style.STROKE);
            mPath = new Path();
     }
    
    @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            mWL = getWidth();
            mCenterY = getHeight() / 2;
            startWave();
    
            // 屏幕外 画第一断波纹的第一条曲线
            mPath.moveTo(-mWL, mCenterY);
            mPath.quadTo(-mWL * 3 / 4, mCenterY + WAVE_HEIGHT, -mWL / 2, mCenterY);
            // 屏幕外 第一段波纹的第二天曲线
            mPath.quadTo(-mWL / 4, mCenterY - WAVE_HEIGHT, 0, mCenterY);
    
            // 屏幕内 画第二断波纹的第一条曲线
            mPath.quadTo(mWL / 4, mCenterY + WAVE_HEIGHT, mWL / 2, mCenterY);
            // 屏幕内 画第二断波纹的第二条曲线
            mPath.quadTo(mWL * 3 / 4, mCenterY - WAVE_HEIGHT, mWL, mCenterY);
    
            canvas.drawPath(mPath, mPaint);
        }
    

    效果图:

    波浪线

    让曲线向右平移,就可以实现波浪效果了,让曲线上每个点的x加入offset平移值(不断绘制来实现动画);

     private void startWave() {
            ValueAnimator animator = ValueAnimator.ofInt(0, mWL); //mWL是一段波纹的长度
            animator.setDuration(1000);
            animator.setRepeatCount(ValueAnimator.INFINITE);
            animator.setInterpolator(new LinearInterpolator());
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    offset = (Integer) animation.getAnimatedValue(); //offset 的值的范围在[0,mWL]之间。
                    postInvalidate();
                }
            });
            animator.start();
        }
    
    // onDraw方法中
        ...
           mPath.reset();
            // 使用 for 循环来画
            mPath.moveTo(-mWL + offset, mCenterY);   // 移动起始点
            for (int i = 0; i < 2; i++) {
                mPath.quadTo(-mWL * 3 / 4 + (i * mWL) + offset, mCenterY + WAVE_HEIGHT, -mWL / 2 + (i * mWL) + offset, mCenterY);
                mPath.quadTo(-mWL / 4 + (i * mWL) + offset, mCenterY - WAVE_HEIGHT, 0 + (i * mWL) + offset, mCenterY);
            }
            canvas.drawPath(mPath, mPaint);
    

    贴一下完整代码:

    public class WaveView extends View {
    
        private Path mPath;
        private Paint mPaint;
        private int mWL;
        private int mCenterY;
        private final int WAVE_HEIGHT = 88;
    
        public WaveView(Context context) {
            this(context, null);
        }
    
        public WaveView(Context context, @Nullable AttributeSet attrs) {
            this(context, attrs, 0);
        }
    
        public WaveView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            mPaint.setColor(Color.RED);
            mPaint.setStyle(Paint.Style.STROKE);
            mPath = new Path();
        }
    
        int offset = 0;
    
        private void startWave() {
            ValueAnimator animator = ValueAnimator.ofInt(0, mWL); //mWL是一段波纹的长度
            animator.setDuration(1000);
            animator.setRepeatCount(ValueAnimator.INFINITE);
            animator.setInterpolator(new LinearInterpolator());
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    offset = (Integer) animation.getAnimatedValue(); //offset 的值的范围在[0,mWL]之间。
                    postInvalidate();
                }
            });
            animator.start();
        }
    
    
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            if (mWL == 0) {
                mWL = getWidth();
                mCenterY = getHeight() / 2;
                startWave();
            }
    
    //        // 屏幕外 画第一断波纹的第一条曲线
    //        mPath.moveTo(-mWL, mCenterY);
    //        mPath.quadTo(-mWL * 3 / 4, mCenterY + WAVE_HEIGHT, -mWL / 2, mCenterY);
    //        // 屏幕外 第一段波纹的第二天曲线
    //        mPath.quadTo(-mWL / 4, mCenterY - WAVE_HEIGHT, 0, mCenterY);
    //
    //        // 屏幕内 画第二断波纹的第一条曲线
    //        mPath.quadTo(mWL / 4, mCenterY + WAVE_HEIGHT, mWL / 2, mCenterY);
    //        // 屏幕内 画第二断波纹的第二条曲线
    //        mPath.quadTo(mWL * 3 / 4, mCenterY - WAVE_HEIGHT, mWL, mCenterY);
    
            mPath.reset();
            // 使用 for 循环来画
            mPath.moveTo(-mWL + offset, mCenterY);   // 移动起始点
            for (int i = 0; i < 2; i++) {
                mPath.quadTo(-mWL * 3 / 4 + (i * mWL) + offset, mCenterY + WAVE_HEIGHT, -mWL / 2 + (i * mWL) + offset, mCenterY);
                mPath.quadTo(-mWL / 4 + (i * mWL) + offset, mCenterY - WAVE_HEIGHT, 0 + (i * mWL) + offset, mCenterY);
            }
            canvas.drawPath(mPath, mPaint);
        }
    }
    
    贝塞尔实现波浪效果

    cubicTo 三级曲线的使用###

    使用4条三阶曲线来画一个正圆:

    正圆
     private void drawCircle() {
            mPath.reset();
            // 4条三阶曲线来表示圆
            // 右上1/4圆
            mPath.lineTo(0, -radius);
            mPath.cubicTo(radius * sMagicNumber, -radius
                    , radius, -radius * sMagicNumber
                    , radius, 0);
            mPath.lineTo(0, 0);
    
            // 右下1/4圆
            mPath.lineTo(0, radius);
            mPath.cubicTo(radius * sMagicNumber, radius
                    , radius, radius * sMagicNumber
                    , radius, 0);
            mPath.lineTo(0, 0);
    
            // 左上1/4圆
            mPath.lineTo(0, -radius);
            mPath.cubicTo(-radius * sMagicNumber, -radius
                    , -radius, -radius * sMagicNumber
                    , -radius, 0);
            mPath.lineTo(0, 0);
    
            // 左下1/4圆
            mPath.lineTo(0, radius);
            mPath.cubicTo(-radius * sMagicNumber, radius
                    , -radius, radius * sMagicNumber
                    , -radius, 0);
            mPath.lineTo(0, 0);
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            canvas.save();
            canvas.translate(getWidth() / 2, getHeight() / 2);
            canvas.drawPath(mPath, mPaint);
            canvas.restore();
        }
    

    效果图:

    正圆

    确定点来生成圆####

    截图来住其他博客

    如上图,共12个点,如果要画曲线P0P3,需要知道 我们就要知道 P0,P1,P2,P3 这四个点的坐标,P0,P3 已经知道了,分别为P0(0, -r), P3(r, 0);
    p1 与 p2,如上,已经算出来了为:P1(rmagic, -r), P2(r, -rmagic);

    点的封装

    // 魔力数字
        private float c = 0.551915024494f;
        class HorizontalLine {
            public PointF left = new PointF(); //P7 P11
            public PointF middle = new PointF(); //P0 P6
            public PointF right = new PointF(); //P1 P5
            public HorizontalLine(float x, float y) {
                left.x = -radius * c;
                left.y = y;
                middle.x = x;
                middle.y = y;
                right.x = radius * c;
                right.y = y;
            }
            public void setY(float y) {
                left.y = y;
                middle.y = y;
                right.y = y;
            }
        }
        class VerticalLine {
            public PointF top = new PointF(); //P2 P10
            public PointF middle = new PointF(); //P3 P9
            public PointF bottom = new PointF(); //P4 P8
            public VerticalLine(float x, float y) {
                top.x = x;
                top.y = -radius * c;
                middle.x = x;
                middle.y = y;
                bottom.x = x;
                bottom.y = radius * c;
            }
            public void setX(float x) {
                top.x = x;
                middle.x = x;
                bottom.x = x;
            }
        }
    

    画圆,并与onTouchEvent绑定:

    // 实现圆
        private void updatePath(float x, float y) {
            float distance = distance(mPrevX, mPrevY, x, y);
            float longRadius = radius + distance;
            float shortRadius = radius - distance * 0.1f;
            mDegree = points2Degrees(mPrevX, mPrevY, x, y);
    
            // 修改相关参数,缩放
            mRightLine.setX(longRadius);
            mLeftLine.setX(-shortRadius);
            mTopLine.setY(-shortRadius);
            mBottomLine.setY(shortRadius);
    
            mPath.reset();
            mPath.moveTo(mTopLine.middle.x, mTopLine.middle.y);
            mPath.cubicTo(mTopLine.right.x, mTopLine.right.y,
                    mRightLine.top.x, mRightLine.top.y,
                    mRightLine.middle.x, mRightLine.middle.y);
            mPath.cubicTo(mRightLine.bottom.x, mRightLine.bottom.y,
                    mBottomLine.right.x, mBottomLine.right.y,
                    mBottomLine.middle.x, mBottomLine.middle.y);
            mPath.cubicTo(mBottomLine.left.x, mBottomLine.left.y,
                    mLeftLine.bottom.x, mLeftLine.bottom.y,
                    mLeftLine.middle.x, mLeftLine.middle.y);
            mPath.cubicTo(mLeftLine.top.x, mLeftLine.top.y,
                    mTopLine.left.x, mTopLine.left.y,
                    mTopLine.middle.x, mTopLine.middle.y);
    
            invalidate();
        }
    

    onTouchEvent:

    @Override
        public boolean onTouchEvent(MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    mPrevX = event.getX();
                    mPrevY = event.getY();
    
                    int centerX = getWidth() / 2;
                    int centerY = getHeight() / 2;
    
                    //点击位置x坐标与圆心的x坐标的距离
                    float distanceX = Math.abs(centerX - mPrevX);
                    //点击位置y坐标与圆心的y坐标的距离
                    float distanceY = Math.abs(centerY - mPrevY);
                    //点击位置与圆心的直线距离
                    int distanceZ = (int) Math.sqrt(Math.pow(distanceX, 2) + Math.pow(distanceY, 2));
                    if (distanceZ > radius) {
                        mIsDown = false;
                    } else {
                        mIsDown = true;
                        return true;
                    }
                    break;
                case MotionEvent.ACTION_MOVE:
                    if (mIsDown) {
                        updatePath(event.getX(), event.getY());
                        return true;
                    }
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:
                    if (mIsDown) {
                        float endX = event.getX();
                        float endY = event.getY();
                        mIsDown = false;
                        back(endX, endY);        // 还原
                        return true;
                    }
    
                    break;
            }
    
            return super.onTouchEvent(event);
        }
    
           // 回去
        private void back(final float x, final float y) {
            final float diffX = x - mPrevX;
            final float diffY = y - mPrevY;
    
            ValueAnimator animator = new ValueAnimator();
            animator.setDuration(200);
            animator.setFloatValues(0, 1);
            animator.setInterpolator(new AccelerateInterpolator());
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    float fract = animation.getAnimatedFraction();
                    updatePath(x - diffX * fract, y - diffY * fract);
                }
            });
            animator.start();
        }
    

    绘制:

    private float distance(float x1, float y1, float x2, float y2) {
            return (float) Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
        }
    
    private static float points2Degrees(float x1, float y1, float x2, float y2) {
            double angle = Math.atan2(y2 - y1, x2 - x1);
            return (float) Math.toDegrees(angle);
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            canvas.save();
            canvas.translate(getWidth() / 2, getHeight() / 2);
            canvas.rotate(mDegree);
            canvas.drawPath(mPath, mPaint);
            canvas.restore();
        }
    

    PathMeasure##

    PathMeasure是一个用来测量Path的类,主要有以下方法:

    构造方法:

    1. PathMeasure(): 创建一个空的PathMeasure;
    2. PathMeasure(path, forceClosed): 创建 PathMeasure 并关联一个指定的Path(Path需要已经创建完成), forceClosed 是否闭合path;

    公共方法:

    1. setPath(Path path, boolean forceClosed) : 关联一个Path;
    2. isClose(): 是否闭合;
    3. getLength(): 获取Path的长度;
    4. nextContour() : 跳转到下一个段(轮廓);
    5. getSegment(float startD, float stopD, Path dst, boolean startWithMoveTo):
      getSegment 用于获取Path的一个片段,将截取成功的路径放入 dst中
    6. getPosTan(float distance, float[] pos, float[] tan):获取指定长度的位置坐标及该点切线值;
    7. getMatrix(float distance, Matrix matrix, int flags): 获取指定长度的位置坐标及该点Matrix

    getSegment

    getSegment 用于获取Path的一个片段,将截取成功的路径放入 dst中;
    android4.4或之前,调用此方法,需关闭硬件加速;
    startWithMoveTo:如果 startWithMoveTo 为 true, 则被截取出来到Path片段保持原状,如果 startWithMoveTo 为 false,则会将截取出来的 Path 片段的起始点移动到 dst 的最后一个点,以保证 dst 的连续性,也即保证是否形变,或连续;
    被截取的片段,会加入到 dst中,而不是覆盖;

    nextContour

    Path,很有可能是由多个path片段组成,但不论是 getLength , getgetSegment 或者是其它方法,都只会在其中第一条线段上运行,而这个 nextContour 就是用于跳转到下一条曲线到方法,如果跳转成功,则返回 true, 如果跳转失败,则返回 false。

              // path 可分段
            Path path = new Path();
            path.addRect(-100, -100, 100, 100, Path.Direction.CW);
            path.addRect(-200, -200, 200, 200, Path.Direction.CW);
            path.addCircle(0, 0, 50, Path.Direction.CW);
    
            canvas.drawPath(path, mPaint);
    
            PathMeasure pathMeasure = new PathMeasure(path, false);    // 与 path关联
            Log.e("TAG", pathMeasure.getLength() + "");        // 800
            pathMeasure.nextContour();      // 获取下一个曲线
            Log.e("TAG", pathMeasure.getLength() + "");        // 1600
            pathMeasure.nextContour();
            Log.e("TAG", pathMeasure.getLength() + "");     // 313.6
    

    getPosTan

    得到路径上某一长度的位置以及该位置的正切值;

    boolean getPosTan (float distance, float[] pos, float[] tan)
    

    distance: 距离Path的起点,0<=distance<=getLength()
    pos: 该点的坐标值;
    tan: 该点的正切值,是用来判断 Path 的趋势的,即在这个位置上曲线的走向;
    如下:

    箭头沿着 path 在移动

    实现代码:

     float currentValue = 0.0f;
        float[] pos = new float[2];        // 点的时间坐标
        float[] tan = new float[2];        //   tangent值,用于计算角度
        Bitmap mInDicator;        // 指示器
        Matrix matrix = new Matrix();
    // 箭头图片
    mInDicator = BitmapFactory.decodeResource(context.getResources(), R.mipmap.indicator);
    
      @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            // 移动到屏幕中间
            canvas.translate(mWidth / 2, mHeight / 2);
    
            // 画圆
            Path path = new Path();
            path.addCircle(0, 0, 200, Path.Direction.CW);
            canvas.drawPath(path, mPaint);
    
            PathMeasure measure = new PathMeasure(path, false);
            currentValue += 0.005;
            if (currentValue >= 1) {
                currentValue = 0;
            }
    
            // pos 点的坐标值
            // tan 可用来计算点的正切值,是用来判断 Path 的趋势的,即在这个位置上曲线的走向
            measure.getPosTan(measure.getLength() * currentValue, pos, tan);
            float degree = (float) (Math.atan2(tan[1], tan[0]) * 180 / Math.PI);
    
            /* =============== 第一种方式,转canvas,点为中心选择 ================= */
            canvas.save();
            canvas.rotate(degree, pos[0],pos[1]);
    
            int halfDrawableWidth = mInDicator.getWidth() / 2;
            int halfDrawableHeight = mInDicator.getHeight() / 2;
            Rect bounds = new Rect((int) pos[0] - halfDrawableWidth, (int) pos[1] - halfDrawableHeight, (int) pos[0] + halfDrawableWidth, (int) pos[1] + halfDrawableHeight);
            canvas.drawBitmap(mInDicator, null, bounds, mPaint);
            canvas.restore();
            invalidate();
            /* =============== 第一种方式,转canvas,点为中心选择 ================= */
    
        /* =============== 第二种方式,利用Matrix选择 ================= */
            /*
            matrix.reset();
            matrix.postRotate(degree, mInDicator.getWidth() / 2, mInDicator.getHeight() / 2);    // 旋转图片
            // 偏移图片绘制中心,将绘制中心调整到与当前点重合
            matrix.postTranslate(pos[0] - mInDicator.getWidth() / 2, pos[1] - mInDicator.getHeight() / 2);
            canvas.drawBitmap(mInDicator, matrix, mPaint);
            invalidate();
            */
            /* =============== 第二种方式,利用Matrix ================= */
    
          /* =============== 第三种方式,利用getMatrix() 简洁 ================= */
            matrix.reset();
            measure.getMatrix(measure.getLength() * currentValue, matrix, PathMeasure.TANGENT_MATRIX_FLAG | PathMeasure.POSITION_MATRIX_FLAG);
            matrix.preTranslate(-mInDicator.getWidth() / 2, -mInDicator.getHeight() / 2);
            canvas.drawBitmap(mInDicator, matrix, mPaint);
            invalidate();
            /* =============== 第三种方式,利用getMatrix() ================= */
        
    

    getMatrix

    得到路径上某一长度的位置以及该位置的正切值的矩阵;上面的第三种方式;

    相关文章

      网友评论

        本文标题:Android path 路径相关

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