美文网首页
自定义View项目实战(三):Path与贝塞尔曲线

自定义View项目实战(三):Path与贝塞尔曲线

作者: bug音音 | 来源:发表于2020-12-24 11:00 被阅读0次

    原理

    贝塞尔曲线的点

    类型 作用
    数据点 确定曲线的起始和结束位置
    控制点 确定曲线的弯曲程度

    一阶曲线

    一阶曲线其实就是前面讲解过的lineTo。

    image

    二阶曲线

    图示

    二阶曲线对应的方法是quadTo。

    image

    示例

    image
    public class Bezier extends View {
    
        private Paint mPaint;
        private int centerX, centerY;
    
        private PointF start, end, control;
    
        public Bessel1(Context context) {
            super(context);
            mPaint = new Paint();
            mPaint.setColor(Color.BLACK);
            mPaint.setStrokeWidth(8);
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setTextSize(60);
    
            start = new PointF(0,0);
            end = new PointF(0,0);
            control = new PointF(0,0);
        }
    
        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            super.onSizeChanged(w, h, oldw, oldh);
            centerX = w/2;
            centerY = h/2;
    
            // 初始化数据点和控制点的位置
            start.x = centerX-200;
            start.y = centerY;
            end.x = centerX+200;
            end.y = centerY;
            control.x = centerX;
            control.y = centerY-100;
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            // 根据触摸位置更新控制点,并提示重绘
            control.x = event.getX();
            control.y = event.getY();
            invalidate();
            return true;
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
    
            // 绘制数据点和控制点
            mPaint.setColor(Color.GRAY);
            mPaint.setStrokeWidth(20);
            canvas.drawPoint(start.x,start.y,mPaint);
            canvas.drawPoint(end.x,end.y,mPaint);
            canvas.drawPoint(control.x,control.y,mPaint);
    
            // 绘制辅助线
            mPaint.setStrokeWidth(4);
            canvas.drawLine(start.x,start.y,control.x,control.y,mPaint);
            canvas.drawLine(end.x,end.y,control.x,control.y,mPaint);
    
            // 绘制贝塞尔曲线
            mPaint.setColor(Color.RED);
            mPaint.setStrokeWidth(8);
    
            Path path = new Path();
    
            path.moveTo(start.x,start.y);
            path.quadTo(control.x,control.y,end.x,end.y);
    
            canvas.drawPath(path, mPaint);
        }
    }
    
    

    三阶曲线

    图示

    三阶曲线对应的方法是cubicTo。

    image

    示例

    image
    public class Bezier2 extends View {
    
        private Paint mPaint;
        private int centerX, centerY;
    
        private PointF start, end, control1, control2;
        private boolean mode = true;
    
        public Bezier2(Context context) {
            this(context, null);
    
        }
    
        public Bezier2(Context context, AttributeSet attrs) {
            super(context, attrs);
    
            mPaint = new Paint();
            mPaint.setColor(Color.BLACK);
            mPaint.setStrokeWidth(8);
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setTextSize(60);
    
            start = new PointF(0, 0);
            end = new PointF(0, 0);
            control1 = new PointF(0, 0);
            control2 = new PointF(0, 0);
        }
    
        public void setMode(boolean mode) {
            this.mode = mode;
        }
    
        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            super.onSizeChanged(w, h, oldw, oldh);
            centerX = w / 2;
            centerY = h / 2;
    
            // 初始化数据点和控制点的位置
            start.x = centerX - 200;
            start.y = centerY;
            end.x = centerX + 200;
            end.y = centerY;
            control1.x = centerX;
            control1.y = centerY - 100;
            control2.x = centerX;
            control2.y = centerY - 100;
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            // 根据触摸位置更新控制点,并提示重绘
            if (mode) {
                control1.x = event.getX();
                control1.y = event.getY();
            } else {
                control2.x = event.getX();
                control2.y = event.getY();
            }
            invalidate();
            return true;
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            //drawCoordinateSystem(canvas);
    
            // 绘制数据点和控制点
            mPaint.setColor(Color.GRAY);
            mPaint.setStrokeWidth(20);
            canvas.drawPoint(start.x, start.y, mPaint);
            canvas.drawPoint(end.x, end.y, mPaint);
            canvas.drawPoint(control1.x, control1.y, mPaint);
            canvas.drawPoint(control2.x, control2.y, mPaint);
    
            // 绘制辅助线
            mPaint.setStrokeWidth(4);
            canvas.drawLine(start.x, start.y, control1.x, control1.y, mPaint);
            canvas.drawLine(control1.x, control1.y,control2.x, control2.y, mPaint);
            canvas.drawLine(control2.x, control2.y,end.x, end.y, mPaint);
    
            // 绘制贝塞尔曲线
            mPaint.setColor(Color.RED);
            mPaint.setStrokeWidth(8);
    
            Path path = new Path();
    
            path.moveTo(start.x, start.y);
            path.cubicTo(control1.x, control1.y, control2.x,control2.y, end.x, end.y);
    
            canvas.drawPath(path, mPaint);
        }
    }
    
    

    降阶与升阶

    类型 释义 变化
    降阶 在保持曲线形状与方向不变的情况下,减少控制点数量,即降低曲线阶数 方法变得简单,数据点变多,控制点可能减少,灵活性变弱
    升阶 在保持曲线形状与方向不变的情况下,增加控制点数量,即升高曲线阶数 方法更加复杂,数据点不变,控制点增加,灵活性变强

    贝塞尔曲线使用实例

    使用贝塞尔曲线的情形

    序号 内容 用例
    1 事先不知道曲线状态,需要实时计算时 天气预报气温变化的平滑折线图
    2 显示状态会根据用户操作改变时 QQ小红点,仿真翻书效果
    3 一些比较复杂的运动状态(配合PathMeasure使用) 复杂运动状态的动画效果

    贝塞尔曲线----圆

    image

    贝塞尔曲线----心形

    image

    实例----圆形变心形

    image
    public class Bezier3 extends View {
        private static final float C = 0.551915024494f;     // 一个常量,用来计算绘制圆形贝塞尔曲线控制点的位置
    
        private Paint mPaint;
        private int mCenterX, mCenterY;
    
        private PointF mCenter = new PointF(0,0);
        private float mCircleRadius = 200;                  // 圆的半径
        private float mDifference = mCircleRadius*C;        // 圆形的控制点与数据点的差值
    
        private float[] mData = new float[8];               // 顺时针记录绘制圆形的四个数据点
        private float[] mCtrl = new float[16];              // 顺时针记录绘制圆形的八个控制点
    
        private float mDuration = 1000;                     // 变化总时长
        private float mCurrent = 0;                         // 当前已进行时长
        private float mCount = 100;                         // 将时长总共划分多少份
        private float mPiece = mDuration/mCount;            // 每一份的时长
    
        public Bezier3(Context context) {
            this(context, null);
    
        }
    
        public Bezier3(Context context, AttributeSet attrs) {
            super(context, attrs);
    
            mPaint = new Paint();
            mPaint.setColor(Color.BLACK);
            mPaint.setStrokeWidth(8);
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setTextSize(60);
    
            // 初始化数据点
    
            mData[0] = 0;
            mData[1] = mCircleRadius;
    
            mData[2] = mCircleRadius;
            mData[3] = 0;
    
            mData[4] = 0;
            mData[5] = -mCircleRadius;
    
            mData[6] = -mCircleRadius;
            mData[7] = 0;
    
            // 初始化控制点
    
            mCtrl[0]  = mData[0]+mDifference;
            mCtrl[1]  = mData[1];
    
            mCtrl[2]  = mData[2];
            mCtrl[3]  = mData[3]+mDifference;
    
            mCtrl[4]  = mData[2];
            mCtrl[5]  = mData[3]-mDifference;
    
            mCtrl[6]  = mData[4]+mDifference;
            mCtrl[7]  = mData[5];
    
            mCtrl[8]  = mData[4]-mDifference;
            mCtrl[9]  = mData[5];
    
            mCtrl[10] = mData[6];
            mCtrl[11] = mData[7]-mDifference;
    
            mCtrl[12] = mData[6];
            mCtrl[13] = mData[7]+mDifference;
    
            mCtrl[14] = mData[0]-mDifference;
            mCtrl[15] = mData[1];
        }
    
        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            super.onSizeChanged(w, h, oldw, oldh);
            mCenterX = w / 2;
            mCenterY = h / 2;
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
             drawCoordinateSystem(canvas);       // 绘制坐标系
    
            canvas.translate(mCenterX, mCenterY); // 将坐标系移动到画布中央
            canvas.scale(1,-1);                 // 翻转Y轴
    
            drawAuxiliaryLine(canvas);
    
            // 绘制贝塞尔曲线
            mPaint.setColor(Color.RED);
            mPaint.setStrokeWidth(8);
    
            Path path = new Path();
            path.moveTo(mData[0],mData[1]);
    
            path.cubicTo(mCtrl[0],  mCtrl[1],  mCtrl[2],  mCtrl[3],     mData[2], mData[3]);
            path.cubicTo(mCtrl[4],  mCtrl[5],  mCtrl[6],  mCtrl[7],     mData[4], mData[5]);
            path.cubicTo(mCtrl[8],  mCtrl[9],  mCtrl[10], mCtrl[11],    mData[6], mData[7]);
            path.cubicTo(mCtrl[12], mCtrl[13], mCtrl[14], mCtrl[15],    mData[0], mData[1]);
    
            canvas.drawPath(path, mPaint);
    
            mCurrent += mPiece;
            if (mCurrent < mDuration){
    
                mData[1] -= 120/mCount;
                mCtrl[7] += 80/mCount;
                mCtrl[9] += 80/mCount;
    
                mCtrl[4] -= 20/mCount;
                mCtrl[10] += 20/mCount;
    
                postInvalidateDelayed((long) mPiece);
            }
        }
    
        // 绘制辅助线
        private void drawAuxiliaryLine(Canvas canvas) {
            // 绘制数据点和控制点
            mPaint.setColor(Color.GRAY);
            mPaint.setStrokeWidth(20);
    
            for (int i=0; i<8; i+=2){
                canvas.drawPoint(mData[i],mData[i+1], mPaint);
            }
    
            for (int i=0; i<16; i+=2){
                canvas.drawPoint(mCtrl[i], mCtrl[i+1], mPaint);
            }
    
            // 绘制辅助线
            mPaint.setStrokeWidth(4);
    
            for (int i=2, j=2; i<8; i+=2, j+=4){
                canvas.drawLine(mData[i],mData[i+1],mCtrl[j],mCtrl[j+1],mPaint);
                canvas.drawLine(mData[i],mData[i+1],mCtrl[j+2],mCtrl[j+3],mPaint);
            }
            canvas.drawLine(mData[0],mData[1],mCtrl[0],mCtrl[1],mPaint);
            canvas.drawLine(mData[0],mData[1],mCtrl[14],mCtrl[15],mPaint);
        }
    
        // 绘制坐标系
        private void drawCoordinateSystem(Canvas canvas) {
            canvas.save();                      // 绘制做坐标系
    
            canvas.translate(mCenterX, mCenterY); // 将坐标系移动到画布中央
            canvas.scale(1,-1);                 // 翻转Y轴
    
            Paint fuzhuPaint = new Paint();
            fuzhuPaint.setColor(Color.RED);
            fuzhuPaint.setStrokeWidth(5);
            fuzhuPaint.setStyle(Paint.Style.STROKE);
    
            canvas.drawLine(0, -2000, 0, 2000, fuzhuPaint);
            canvas.drawLine(-2000, 0, 2000, 0, fuzhuPaint);
    
            canvas.restore();
        }
    }
    

    相关文章

      网友评论

          本文标题:自定义View项目实战(三):Path与贝塞尔曲线

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