Android贝塞尔曲线的使用
来源:互联网 发布:php论坛 编辑:程序博客网 时间:2024/05/16 11:04
什么是贝塞尔曲线?
是应用于二维图形应用程序的数学曲线一般在软件开发中我们使用它进行精确的绘制平滑的曲线。
如何绘制一条简单的曲线?
首先我们来看看一些简单的图形
我们可以看到上面三张图片都是一些比较简单的曲线,那么我们如何使用编程的方式将其是现在界面上了,首先我们对这几张图片进行分析
我们发现所有曲线都由起始点,控制点(可以有多个),结束点三个部分组成,只要我们能够在显示器上精确的定位这三个部分就能实现曲线的绘制
Android对贝塞尔曲线的支持
在Android中的Path类对贝塞尔曲线进行支持
1 . 对于单个控制点的贝塞尔曲线:
/** * Add a quadratic bezier from the last point, approaching control point * (x1,y1), and ending at (x2,y2). If no moveTo() call has been made for * this contour, the first point is automatically set to (0,0). * * @param x1 The x-coordinate of the control point on a quadratic curve * @param y1 The y-coordinate of the control point on a quadratic curve * @param x2 The x-coordinate of the end point on a quadratic curve * @param y2 The y-coordinate of the end point on a quadratic curve */ public void quadTo(float x1, float y1, float x2, float y2) { isSimplePath = false; native_quadTo(mNativePath, x1, y1, x2, y2); }
其中起始点为此Path的上一个连接点,x1,y1为此曲线的控制点,x2,y2为结束点
2 . 对于多个控制点的贝塞尔曲线:
/** * Add a cubic bezier from the last point, approaching control points * (x1,y1) and (x2,y2), and ending at (x3,y3). If no moveTo() call has been * made for this contour, the first point is automatically set to (0,0). * * @param x1 The x-coordinate of the 1st control point on a cubic curve * @param y1 The y-coordinate of the 1st control point on a cubic curve * @param x2 The x-coordinate of the 2nd control point on a cubic curve * @param y2 The y-coordinate of the 2nd control point on a cubic curve * @param x3 The x-coordinate of the end point on a cubic curve * @param y3 The y-coordinate of the end point on a cubic curve */ public void cubicTo(float x1, float y1, float x2, float y2, float x3, float y3) { isSimplePath = false; native_cubicTo(mNativePath, x1, y1, x2, y2, x3, y3); }
其中起始点为此Path的上一个连接点,x1,y1为此曲线的第一个控制点,x2,y2为第二个控制点,x3,y3为结束点
3 .实现案例一
案例分析:
1.我们可以将此效果看为是一个波形图
波峰 波长
2.是由多个贝塞尔曲线链接的效果
案例实现:
1.新建一个自定义View,并且重写onDraw方法
public class TestBizerView extends View { public TestBizerView(Context context) { this(context,null); } public TestBizerView(Context context, @Nullable AttributeSet attrs) { this(context, attrs,0); } public TestBizerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); }}
2.重写onDraw方法并且初始化画笔设置
/** * 初始化画笔 */ Paint mPaint; /** * 曲线起始点的坐标 */ private static int START_X_POINT=0; private static int START_Y_POINT=200; /** * 波长度 */ private static int WAVE_LENGTH=200; /** *波峰高度 */ private static int WAVE_HEIGHT=50; /** * 初始化画笔 */ public void init(){ mPaint=new Paint(); //1.设置画笔颜色 mPaint.setColor(Color.RED); //2.设置抗锯齿 mPaint.setAntiAlias(true); //3.设置画笔风格 mPaint.setStyle(Paint.Style.STROKE); } @Override protected void onDraw(Canvas canvas) { }
3.简单的绘制一个波长(在onDraw中)
@Override protected void onDraw(Canvas canvas) { //1.初始化一个Path路径 Path p=new Path(); //2.设置路径的起始点(Bizer的起始点) p.moveTo(START_X_POINT,START_Y_POINT); //3.绘制一个波长的贝塞尔曲线 //控制点坐标:WAVE_LENGTH/4,START_Y_POINT-WAVE_HEIGHT //结束点坐标:WAVE_LENGTH/2,START_Y_POINT p.quadTo(WAVE_LENGTH/4,START_Y_POINT-WAVE_HEIGHT,WAVE_LENGTH/2,START_Y_POINT); //以上一次的结束点为起始点 p.quadTo(WAVE_LENGTH*3/4,START_Y_POINT+WAVE_HEIGHT,WAVE_LENGTH,START_Y_POINT); //4.绘制路径 canvas.drawPath(p,mPaint); }
4.在布局中使用此自定义View
<com.example.hasee.testbizerapp.TestBizerView android:layout_width="match_parent" android:layout_height="match_parent"/>
5.绘制多个波长
需要对OnDraw方法中的代码进行修改
@Override protected void onDraw(Canvas canvas) { //1.初始化一个Path路径 Path p=new Path(); //2.设置路径的起始点(Bizer的起始点) p.moveTo(START_X_POINT,START_Y_POINT); //3.绘制一个波长的贝塞尔曲线// //控制点坐标:WAVE_LENGTH/4,START_Y_POINT-WAVE_HEIGHT// //结束点坐标:WAVE_LENGTH/2,START_Y_POINT// p.quadTo(WAVE_LENGTH/4,START_Y_POINT-WAVE_HEIGHT,WAVE_LENGTH/2,START_Y_POINT);// //以上一次的结束点为起始点// p.quadTo(WAVE_LENGTH*3/4,START_Y_POINT+WAVE_HEIGHT,WAVE_LENGTH,START_Y_POINT); for (int i = 0; i <20 ; i++) { p.quadTo(WAVE_LENGTH(i*2+1)/4,i%2==0?START_Y_POINTWAVE_HEIGHT:START_Y_POINT+WAVE_HEIGHT,WAVE_LENGTH(i+1)/2,START_Y_POINT); } //4.绘制路径 canvas.drawPath(p,mPaint); }
代码链接:https://github.com/huangyiCode/TestBizerApp.git
3 .实现案例二
代码链接:https://github.com/huangyiCode/TestBizerTwo
阅读全文
1 0
- Android贝塞尔曲线的使用
- Android中贝塞尔曲线的使用
- 贝塞尔曲线的使用
- Android 使用贝塞尔曲线将多点连成一条平滑的曲线
- android使用贝塞尔曲线做一个简单的玩意
- Android -- 贝塞尔曲线公式的推导和简单使用
- Android 贝塞尔曲线的魅力
- Android 贝塞尔曲线的魅力
- iOS--贝塞尔曲线的使用
- 在Android上的使用《贝赛尔曲线》!
- 在Android上的使用 贝赛尔曲线
- Android 绘图贝塞尔曲线简单使用
- android贝塞尔曲线的简单应用
- 【Android】贝塞尔曲线的艺术(一)
- Android贝塞尔曲线的理解与应用
- Android 画一条简单的贝塞尔曲线
- iOS 简单的贝塞尔(UIBezierPath)曲线使用
- Android属性动画高阶用法-Interpolator,TypeEvaluatory以及贝塞尔曲线公式的使用
- Redis批量删除详解
- 不要给自己找理由
- 正则表达式
- 关注国情民情,从身边做起
- 表单验证插件 validata
- Android贝塞尔曲线的使用
- MATLAB大中小括号()[]{}的区别
- js 停留几秒后页面再跳转
- PHP的引入require和include的区别
- 数据结构实验之图论四:迷宫探索(邻接表与DFS)
- HDU 6093 Rikka with Number
- JavaScript 疲劳终极指南:我们行业的真相
- 【真题】京东2017校招编程题 幸运数
- C++ 文件夹下文件监控