废话不说了,直接上代码!
public class BezierThiredView extends View { // 开始坐标 private float startXPoint; private float startYPoint; // 结束坐标 private float endXPoint; private float endYPoint; // 两个控制点 private float conOneXPoint; private float conOneYPoint; private float conTwoXPoint; private float conTwoYPoint; // 画笔、路径 private Path mPath; private Paint mPaint; private Paint mPaintCon; private Paint mPaintText; private boolean isPointDown; // 手指是否按下 public BezierThiredView(Context context) { super(context); } public BezierThiredView(Context context, AttributeSet attrs) { super(context, attrs); mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mPaint.setStyle(Paint.Style.STROKE); mPaint.setColor(Color.RED); mPaint.setStrokeWidth(8); mPaintCon = new Paint(Paint.ANTI_ALIAS_FLAG); mPaintCon.setStyle(Paint.Style.STROKE); mPaintCon.setStrokeWidth(3); mPaintText = new Paint(Paint.ANTI_ALIAS_FLAG); mPaintText.setStyle(Paint.Style.STROKE); mPaintText.setTextSize(20); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); startXPoint = w / 4; startYPoint = h / 2; endXPoint = w * 3 / 4; endYPoint = h / 2; conOneXPoint = w / 2 - 100; conOneYPoint = h / 2 - 400; conTwoXPoint = w / 2 + 100; conTwoYPoint = h / 2 - 400; mPath = new Path(); } public BezierThiredView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); mPath.reset(); mPath.moveTo(startXPoint,startYPoint); mPath.cubicTo(conOneXPoint,conOneYPoint,conTwoXPoint,conTwoYPoint,endXPoint,endYPoint); canvas.drawPoint(startXPoint,startYPoint,mPaintCon); canvas.drawText("P0",startXPoint-50,startYPoint,mPaintText); canvas.drawPoint(endXPoint,endYPoint,mPaintCon); canvas.drawText("P3",endXPoint+50,endYPoint,mPaintText); canvas.drawPoint(conOneXPoint, conOneYPoint,mPaintCon); canvas.drawText("P1", conOneXPoint-50, conOneYPoint,mPaintText); canvas.drawPoint(conTwoXPoint, conTwoYPoint,mPaintCon); canvas.drawText("P2", conTwoXPoint+50, conTwoYPoint,mPaintText); canvas.drawLine(startXPoint,startYPoint, conOneXPoint, conOneYPoint,mPaintCon); canvas.drawLine(conOneXPoint, conOneYPoint,conTwoXPoint,conTwoYPoint,mPaintCon); canvas.drawLine(conTwoXPoint, conTwoYPoint,endXPoint,endYPoint,mPaintCon); canvas.drawPath(mPath,mPaint); } @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_POINTER_DOWN: isPointDown = true; break; case MotionEvent.ACTION_POINTER_UP: isPointDown = false; break; case MotionEvent.ACTION_MOVE: conOneXPoint = event.getX(0); conOneYPoint = event.getY(0); if (isPointDown) { conTwoXPoint = event.getX(1); conTwoYPoint = event.getY(1); } invalidate(); break; } return true; } }~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
我们两个手指一起操作来对贝塞尔三阶曲线进行各种男人的撕扯吧!我不知道使用什么东西来录制屏幕,只能给大家上一张经过我拉扯后的图。
好了,这次这篇就到这里吧,请大家继续关注我,以后还会去更新贝塞尔曲线的文章。
题外话:如果谁知道用什么软件可以录制手机屏幕并生成gif图的可以在下方留言。 哈哈,好的我们下期再见。
小奋斗文章
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~