canvas教程

Android贝塞尔曲线 曲线进阶(2)

字号+ 作者:H5之家 来源:H5之家 2017-01-28 15:00 我要评论( )

废话不说了,直接上代码! publicclassBezierThiredViewextendsView{//开始坐标privatefloatstartXPoint;privatefloatstartYPoint;//结束坐标privatefloatendXPoint;privatefloatendYPoint;//两个控制点privatefloat

废话不说了,直接上代码!

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图的可以在下方留言。 哈哈,好的我们下期再见。

小奋斗文章

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • Android硬件加速原理与实现简介

    Android硬件加速原理与实现简介

    2017-01-26 11:05

  • -_--___---_-

    -_--___---_-

    2017-01-26 11:03

  • Android学习札记二

    Android学习札记二

    2017-01-26 10:02

  • 双缓冲技术实现Android 画板应用

    双缓冲技术实现Android 画板应用

    2017-01-24 15:03

网友点评