canvas教程

Android贝塞尔曲线原理分析(3)

字号+ 作者:H5之家 来源:H5之家 2017-06-03 16:04 我要评论( )

自定义 MyBezierCurveCubic 类的完整代码如下: package com.example.pc.mybeziercurveview.view;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.g

自定义 MyBezierCurveCubic 类的完整代码如下:

package com.example.pc.mybeziercurveview.view; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.graphics.PointF; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; /** * 三阶 * Created by Deeson on 2017/5/24. */ public class MyBezierCurveCubic extends View{ private Paint mPaint; private Path mPath; private int centerX,centerY; private PointF start,end,control1,control2; public static final int CONTROL_ONE = 0; public static final int CONTROL_TWO = 1; private int control = CONTROL_ONE; public MyBezierCurveCubic(Context context) { super(context); init(); } public MyBezierCurveCubic(Context context, AttributeSet attrs) { super(context, attrs); init(); } public MyBezierCurveCubic(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { mPaint = new Paint(); mPath = new Path(); mPaint.setColor(Color.BLACK); mPaint.setStrokeWidth(8); mPaint.setStyle(Paint.Style.STROKE); start = new PointF(0,0); end = new PointF(0,0); control1 = new PointF(0,0); control2 = 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; control1.x = centerX - 200; control1.y = centerY - 200; control2.x = centerX + 200; control2.y = centerY + 200; } @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(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); mPath.reset(); mPath.moveTo(start.x,start.y); mPath.cubicTo(control1.x,control1.y,control2.x,control2.y,end.x,end.y); canvas.drawPath(mPath,mPaint); } public void setControl(int control){ this.control = control; } @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()){ case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_MOVE: if(control == CONTROL_ONE){ control1.x = event.getX(); control1.y = event.getY(); }else{ control2.x = event.getX(); control2.y = event.getY(); } invalidate(); break; } return true; } }

二、接着来看,如何用 SurfaceView 实现如下三阶贝塞尔曲线动画效果:

关于SurfaceView 的使用,大家可以看我的上一篇文章 Android:SurfaceView 的使用(附代码模板)

三阶贝塞尔曲线show

 

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

相关文章
  • android画图实例源码下载

    android画图实例源码下载

    2017-05-30 17:03

  • Android开发学习资料,Android开发学习线路

    Android开发学习资料,Android开发学习线路

    2017-05-30 17:02

  • Android学习教程之2D绘图基础及绘制太极图

    Android学习教程之2D绘图基础及绘制太极图

    2017-05-29 18:00

  • 图像与边框在地图叠加使用android

    图像与边框在地图叠加使用android

    2017-05-28 16:00

网友点评
/