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

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

相关文章

基于 HTML5 Canvas 实现图片旋转与下载功能(完整代码展示)

《基于HTML5Canvas实现图片旋转与下载功能(完整代码展示)》本文将深入剖析一段基于HTML5Canvas的代码,该代码实现了图片的旋转(90度和180度)以及旋转后图片的下载... 目录一、引言二、html 结构分析三、css 样式分析四、JavaScript 功能实现一、引言在 Web 开发中,

SpringBoot中使用Flux实现流式返回的方法小结

《SpringBoot中使用Flux实现流式返回的方法小结》文章介绍流式返回(StreamingResponse)在SpringBoot中通过Flux实现,优势包括提升用户体验、降低内存消耗、支持长连... 目录背景流式返回的核心概念与优势1. 提升用户体验2. 降低内存消耗3. 支持长连接与实时通信在Sp

Conda虚拟环境的复制和迁移的四种方法实现

《Conda虚拟环境的复制和迁移的四种方法实现》本文主要介绍了Conda虚拟环境的复制和迁移的四种方法实现,包括requirements.txt,environment.yml,conda-pack,... 目录在本机复制Conda虚拟环境相同操作系统之间复制环境方法一:requirements.txt方法

Spring Boot 实现 IP 限流的原理、实践与利弊解析

《SpringBoot实现IP限流的原理、实践与利弊解析》在SpringBoot中实现IP限流是一种简单而有效的方式来保障系统的稳定性和可用性,本文给大家介绍SpringBoot实现IP限... 目录一、引言二、IP 限流原理2.1 令牌桶算法2.2 漏桶算法三、使用场景3.1 防止恶意攻击3.2 控制资源

springboot下载接口限速功能实现

《springboot下载接口限速功能实现》通过Redis统计并发数动态调整每个用户带宽,核心逻辑为每秒读取并发送限定数据量,防止单用户占用过多资源,确保整体下载均衡且高效,本文给大家介绍spring... 目录 一、整体目标 二、涉及的主要类/方法✅ 三、核心流程图解(简化) 四、关键代码详解1️⃣ 设置

Nginx 配置跨域的实现及常见问题解决

《Nginx配置跨域的实现及常见问题解决》本文主要介绍了Nginx配置跨域的实现及常见问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来... 目录1. 跨域1.1 同源策略1.2 跨域资源共享(CORS)2. Nginx 配置跨域的场景2.1

Python中提取文件名扩展名的多种方法实现

《Python中提取文件名扩展名的多种方法实现》在Python编程中,经常会遇到需要从文件名中提取扩展名的场景,Python提供了多种方法来实现这一功能,不同方法适用于不同的场景和需求,包括os.pa... 目录技术背景实现步骤方法一:使用os.path.splitext方法二:使用pathlib模块方法三

CSS实现元素撑满剩余空间的五种方法

《CSS实现元素撑满剩余空间的五种方法》在日常开发中,我们经常需要让某个元素占据容器的剩余空间,本文将介绍5种不同的方法来实现这个需求,并分析各种方法的优缺点,感兴趣的朋友一起看看吧... css实现元素撑满剩余空间的5种方法 在日常开发中,我们经常需要让某个元素占据容器的剩余空间。这是一个常见的布局需求

HTML5 getUserMedia API网页录音实现指南示例小结

《HTML5getUserMediaAPI网页录音实现指南示例小结》本教程将指导你如何利用这一API,结合WebAudioAPI,实现网页录音功能,从获取音频流到处理和保存录音,整个过程将逐步... 目录1. html5 getUserMedia API简介1.1 API概念与历史1.2 功能与优势1.3

Java实现删除文件中的指定内容

《Java实现删除文件中的指定内容》在日常开发中,经常需要对文本文件进行批量处理,其中,删除文件中指定内容是最常见的需求之一,下面我们就来看看如何使用java实现删除文件中的指定内容吧... 目录1. 项目背景详细介绍2. 项目需求详细介绍2.1 功能需求2.2 非功能需求3. 相关技术详细介绍3.1 Ja