Android贝塞尔曲线 多点

public class Towlion extends View {




    private Paint paint;
    private PointF start;
    private Point end,zhondian;
    private boolean mIsSecondPoint = false;
    private int mControlX1,mControlY1,mControlX2,
    mControlY2;






    public Towlion(Context context) {
        super(context);
        //创建一个画笔
        paint = new Paint();
        paint.setStrokeWidth(10);//设置线的宽度
        paint.setStyle(Paint.Style.STROKE);//设置样式
        paint.setTextSize(60);
        start = new PointF(0, 0);//创建一个点
        start.set(300,300);
        end = new Point(0, 0);//创建一个点
        end.set(300,1100);
        zhondian = new Point(0, 0);//创建一个点
        zhondian.set(300,700);


    }




    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //设置点的style
        paint.setColor(Color.RED);
        paint.setStrokeWidth(5);


        //加入点
        canvas.drawPoint(start.x, start.y, paint);
        canvas.drawText("起点",start.x, start.y, paint);
        canvas.drawPoint(zhondian.x, zhondian.y, paint);
        canvas.drawText("中点",zhondian.x, zhondian.y, paint);
        canvas.drawPoint(end.x, end.y, paint);
        canvas.drawText("结束点",end.x, end.y, paint);
        /**
         * 俩点连线
         */
        canvas.drawLine(start.x,start.y,mControlX1,mControlY1,paint);
        canvas.drawLine(mControlX1,mControlY1,zhondian.x,zhondian.y,paint);


        canvas.drawLine(zhondian.x,zhondian.y,mControlX2, mControlY2,paint);
        canvas.drawLine(mControlX2, mControlY2,end.x, end.y,paint);


        paint.setColor(Color.GRAY);
        paint.setStrokeWidth(10);
        /**
         * 抛物线
         */
        Path path = new Path();//创建路径
        //起点
        path.moveTo(start.x, start.y);
        path.quadTo(mControlX1,mControlY1, zhondian.x,zhondian.y);
        canvas.drawPath(path, paint);




        Path paths = new Path();//创建路径
        paths.moveTo(zhondian.x,zhondian.y);
        paths.quadTo(mControlX2, mControlY2, end.x, end.y);
        canvas.drawPath(paths, paint);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {


        switch (event.getAction() & MotionEvent.ACTION_MASK) {//多点触控
            case MotionEvent.ACTION_POINTER_DOWN:
                mIsSecondPoint = true;
                break;
            case MotionEvent.ACTION_POINTER_UP:
                mIsSecondPoint = false;
                break;
            case MotionEvent.ACTION_BUTTON_PRESS:
                Toast.makeText(getContext(), "123123", Toast.LENGTH_SHORT).show();


                break;
            case MotionEvent.ACTION_MOVE:
                mControlX1 = (int) event.getX(0);//获取控制点1的横纵坐标
                mControlY1 =(int) event.getY(0);
                if(mIsSecondPoint){
                    mControlX2 = (int)event.getX(1);//获取控制点2的横纵坐标
                    mControlY2 = (int)event.getY(1);
                }
                invalidate();
                break;
        }
        return true;
    }
}

你可能感兴趣的:(Android贝塞尔曲线 多点)