1. 程式人生 > >Android貝塞爾曲線 二階的簡單處理

Android貝塞爾曲線 二階的簡單處理

二階效果圖
控制點只有一個

這裡寫圖片描述

    private float mStartPointX;
    private float mStartPointY;

    private float mEndPointX;
    private float mEndPointY;

    private float mFlagPointX;
    private float mFlagPointY;

    private Path mPath;

    private Paint mPaintBezier;
    private Paint mPaintFlag;
    private
Paint mPaintFlagText; public SecondBezierView(Context context) { super(context); } public SecondBezierView(Context context, AttributeSet attrs) { super(context, attrs); mPaintBezier = new Paint(Paint.ANTI_ALIAS_FLAG); mPaintBezier.setStrokeWidth(8); mPaintBezier.setStyle(Paint.Style.STROKE); mPaintFlag = new
Paint(Paint.ANTI_ALIAS_FLAG); mPaintFlag.setStrokeWidth(3); mPaintFlag.setStyle(Paint.Style.STROKE); mPaintFlagText = new Paint(Paint.ANTI_ALIAS_FLAG); mPaintFlagText.setStyle(Paint.Style.STROKE); mPaintFlagText.setTextSize(20); } public SecondBezierView
(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); mStartPointX = w / 4; mStartPointY = h / 2 - 200; mEndPointX = w * 3 / 4; mEndPointY = h / 2 - 200; mFlagPointX = w / 2; mFlagPointY = h / 2 - 300; mPath = new Path(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); mPath.reset(); mPath.moveTo(mStartPointX, mStartPointY); mPath.quadTo(mFlagPointX, mFlagPointY, mEndPointX, mEndPointY); canvas.drawPoint(mStartPointX, mStartPointY, mPaintFlag); canvas.drawText("起點", mStartPointX, mStartPointY, mPaintFlagText); canvas.drawPoint(mEndPointX, mEndPointY, mPaintFlag); canvas.drawText("終點", mEndPointX, mEndPointY, mPaintFlagText); canvas.drawPoint(mFlagPointX, mFlagPointY, mPaintFlag); canvas.drawText("控制點", mFlagPointX, mFlagPointY, mPaintFlagText); canvas.drawLine(mStartPointX, mStartPointY, mFlagPointX, mFlagPointY, mPaintFlag); canvas.drawLine(mEndPointX, mEndPointY, mFlagPointX, mFlagPointY, mPaintFlag); canvas.drawPath(mPath, mPaintBezier); } @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_MOVE: mFlagPointX = event.getX(); mFlagPointY = event.getY(); invalidate(); break; } return true; }