利用贝塞尔曲线实现小球曲线运动

2023-10-11 14:59

本文主要是介绍利用贝塞尔曲线实现小球曲线运动,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

利用贝塞尔曲线实现小球曲线运动

之前有个项目,要求小气泡从A到沿着某一特定曲线平滑漂移到B点。下面是这个效果实现的一个demo,
本文聊聊这个效果是如何实现的。
这里写图片描述

原理

可以参考网友的这篇博文,其他还有很多。
原理简述

实现

这里写图片描述

Bubble类主要定义了小球的属性。
public class Bubble {/** 小球半径 */private int radius;/*启动延迟时间*/int delays;/** 小球x坐标 */private float x;/** 小球y坐标 */private float y;Bubble(int radius, int delays, float x, float y) {this.radius = radius;this.delays = delays;this.x = x;this.y = y;}public int getDelays() {return delays;}public void setDelays(int delays) {this.delays = delays;}public int getRadius() {return radius;}public float getY() {return y;}public float getX() {return x;}public void setRadius(int radius) {this.radius = radius;}public void setX(float x) {this.x = x;}public void setY(float y) {this.y = y;}public static class Builder {/** 小球半径 */int radius;/*启动延迟时间*/int delays;/** 小球x坐标 */float x;/** 小球y坐标 */float y;public Builder radius(int radius) {this.radius = radius;return this;}public Builder delays(int delays) {this.delays = delays;return this;}public Builder x(float x) {this.x = x;return this;}public Builder y(float y) {this.y = y;return this;}public Bubble build() {return new Bubble(radius, delays, x, y);}}
BubbleView 小球曲线运动的实现类。

对外提供的方法如下:
这里写图片描述

那么具体是如何实现曲线运动的?
我们在构造函数中对所需要的基本对象如:画笔,进行初始化。

     paint = new Paint();paint.setAlpha(200);paint.setColor(Color.RED);paint.setStyle(Paint.Style.FILL);paint.setAntiAlias(true);

然后写一个小球的创造器,等动画开始之前使用。

private void createBubbles() {Bubble bubble1 = new Bubble.Builder().radius(dip2px(mContext, 9)).delays(0).x(startX).y(startY).build();Bubble bubble2 = new Bubble.Builder().radius(dip2px(mContext, 7)).delays(100).x(startX).y(startY).build();Bubble bubble3 = new Bubble.Builder().radius(dip2px(mContext, 5)).delays(150).x(startX).y(startY).build();Bubble bubble4 = new Bubble.Builder().radius(dip2px(mContext, 4)).delays(200).x(startX).y(startY).build();bubbles.add(bubble1);bubbles.add(bubble2);bubbles.add(bubble3);bubbles.add(bubble4);}

创建小球需要的动画

 private void createAnimation() {isStoped = false;final int BALL_SIZE = bubbles.size();for (int i = 0; i < BALL_SIZE; i++) {ValueAnimator xyAnimation = ValueAnimator.ofFloat(0f, 1f);xyAnimation.setDuration(800+ i * 50);xyAnimation.setRepeatCount(0);xyAnimation.setStartDelay(bubbles.get(i).getDelays());xyAnimation.setInterpolator(new AccelerateDecelerateInterpolator());xyAnimation.addUpdateListener(new BubbleAnimatorUpdateListener(i, bubbles.get(i)));xyAnimation.start();}}

写小球动画的监听,在监听中调整小球的坐标并且通知ondraw绘制小球的坐标,形成曲线。这里小球的坐标计算我们用到了4阶的贝塞尔曲线。这里只是简单的套公式。

 class BubbleAnimatorUpdateListener implements ValueAnimator.AnimatorUpdateListener {private Bubble bubble;private int index;public BubbleAnimatorUpdateListener(int index, Bubble bubble) {this.bubble = bubble;this.index = index;}@Overridepublic void onAnimationUpdate(ValueAnimator valueAnimator) {float t = ((Float) valueAnimator.getAnimatedValue()).floatValue();float Px0 = startX;float Px1 = 0;float Px2 = 0;float Px3 = 0;float Px4 = endX;float Py0 = startY;float Py1 = startY/2 + index;float Py2 = startY/3 + index;float Py3 = startY/4 + index;float Py4 = endY;if (index == 0) {Px1 = startX + (endX - startX)/2;Px2 = startX + (endX - startX)/3;Px3 = endX + (endX - startX)/3;} else if (index == 1) {Px1 = startX + (endX - startX)/1;Px2 = startX + (endX - startX)/3;Px3 = endX + (endX - startX)/3;} else if (index == 2) {Px1 = startX - (endX - startX)/4;Px2 = startX;Px3 = endX + (endX - startX)/3;} else {Px1 = startX + (endX - startX)/1;Px2 = startX + (endX - startX)/3;Px3 = endX + (endX - startX)/3;}if (isStoped) {valueAnimator.cancel();bubble.setX(endX);bubble.setY(endY);} else {bubble.setX((float) (Px0 * Math.pow((1 - t), 4) + 4 * Px1 * t * Math.pow(1 - t, 3) + 6 * Px2 * Math.pow(t, 2) * Math.pow(1 - t, 2) + 4 * Px3 * (1 - t) * Math.pow(t, 3) + Px4 * Math.pow(t,4)));bubble.setY((float) (Py0 * Math.pow((1 - t), 4) + 4 * Py1 * t * Math.pow(1 - t, 3) + 6 * Py2 * Math.pow(t, 2) * Math.pow(1 - t, 2) + 4 * Py3 * (1 - t) * Math.pow(t, 3) + Py4 * Math.pow(t,4)));}if (1f == ((Float) valueAnimator.getAnimatedValue()).floatValue()) {bubbles.remove(bubble);if (bubbleViewListener != null && bubbles.size() == 3) {bubbleViewListener.onEndListener();}}invalidate();}}

根据坐标绘制小球

    protected void onDraw(Canvas canvas) {super.onDraw(canvas);if (bubbles != null) {for (Bubble bubble : bubbles) {canvas.save();android.util.Log.e("createAnimation", "bubble.getX() " + bubble.getX());canvas.drawCircle(bubble.getX(), bubble.getY(),bubble.getRadius(), paint);canvas.restore();}}}

小结:其实本文关于贝塞尔曲线只是套了公式,主要用了android的ValueAnimator 动画来控制坐标和动画速度,在ondraw方法中进行界面绘制。

这篇关于利用贝塞尔曲线实现小球曲线运动的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/188857

相关文章

Flutter实现文字镂空效果的详细步骤

《Flutter实现文字镂空效果的详细步骤》:本文主要介绍如何使用Flutter实现文字镂空效果,包括创建基础应用结构、实现自定义绘制器、构建UI界面以及实现颜色选择按钮等步骤,并详细解析了混合模... 目录引言实现原理开始实现步骤1:创建基础应用结构步骤2:创建主屏幕步骤3:实现自定义绘制器步骤4:构建U

SpringBoot中四种AOP实战应用场景及代码实现

《SpringBoot中四种AOP实战应用场景及代码实现》面向切面编程(AOP)是Spring框架的核心功能之一,它通过预编译和运行期动态代理实现程序功能的统一维护,在SpringBoot应用中,AO... 目录引言场景一:日志记录与性能监控业务需求实现方案使用示例扩展:MDC实现请求跟踪场景二:权限控制与

Android实现定时任务的几种方式汇总(附源码)

《Android实现定时任务的几种方式汇总(附源码)》在Android应用中,定时任务(ScheduledTask)的需求几乎无处不在:从定时刷新数据、定时备份、定时推送通知,到夜间静默下载、循环执行... 目录一、项目介绍1. 背景与意义二、相关基础知识与系统约束三、方案一:Handler.postDel

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

Python实现微信自动锁定工具

《Python实现微信自动锁定工具》在数字化办公时代,微信已成为职场沟通的重要工具,但临时离开时忘记锁屏可能导致敏感信息泄露,下面我们就来看看如何使用Python打造一个微信自动锁定工具吧... 目录引言:当微信隐私遇到自动化守护效果展示核心功能全景图技术亮点深度解析1. 无操作检测引擎2. 微信路径智能获

Python中pywin32 常用窗口操作的实现

《Python中pywin32常用窗口操作的实现》本文主要介绍了Python中pywin32常用窗口操作的实现,pywin32主要的作用是供Python开发者快速调用WindowsAPI的一个... 目录获取窗口句柄获取最前端窗口句柄获取指定坐标处的窗口根据窗口的完整标题匹配获取句柄根据窗口的类别匹配获取句

在 Spring Boot 中实现异常处理最佳实践

《在SpringBoot中实现异常处理最佳实践》本文介绍如何在SpringBoot中实现异常处理,涵盖核心概念、实现方法、与先前查询的集成、性能分析、常见问题和最佳实践,感兴趣的朋友一起看看吧... 目录一、Spring Boot 异常处理的背景与核心概念1.1 为什么需要异常处理?1.2 Spring B

Python位移操作和位运算的实现示例

《Python位移操作和位运算的实现示例》本文主要介绍了Python位移操作和位运算的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 位移操作1.1 左移操作 (<<)1.2 右移操作 (>>)注意事项:2. 位运算2.1

如何在 Spring Boot 中实现 FreeMarker 模板

《如何在SpringBoot中实现FreeMarker模板》FreeMarker是一种功能强大、轻量级的模板引擎,用于在Java应用中生成动态文本输出(如HTML、XML、邮件内容等),本文... 目录什么是 FreeMarker 模板?在 Spring Boot 中实现 FreeMarker 模板1. 环

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义