Android自定义View之点赞撒花(三阶贝塞尔曲线应用)

2024-03-06 16:30

本文主要是介绍Android自定义View之点赞撒花(三阶贝塞尔曲线应用),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前言

本文参考辉哥的贝塞尔曲线 - 花束直播点赞效果,是对三阶贝塞尔曲线和对属性动画的运用,十分适合学习自定义View。

最终效果

点赞撒花

实现思路

  • 刚开始,爱心位于整个View的最底部中间位置,我们可以继承RelativeLayout并在其底部中间位置添加ImageView,设置ImageView为爱心图片即可;
  • 爱心生成时伴随着透明度和放大动画,这个比较简单,通过属性动画实现即可;
  • 然后爱心上升的运动轨迹整体是一个三阶的贝塞尔曲线,对于三阶贝塞尔曲线我们知道存在起始位置p0终止位置p3以及两个控制点p1以及p2,如下图:
    三阶贝塞尔曲线
    我们只要计算出p0p1p2p3的坐标即可绘制出爱心上升轨迹;各个点坐标如图上,其中layoutWidthlayoutHeight为整体布局的宽高,ivWidthivHeight为爱心背景图片的宽高;
    1. 对于p0点,取爱心背景对应的左上角坐标,横坐标为layoutWidth / 2 - ivWidth / 2,纵坐标为layoutHeight - ivHeight;
    2. 对于p1点,横坐标为可以取layoutWidth任意值 - ivWidth,纵坐标对应区间应该为【layoutHeight/2,layoutHeight】;
    3. 对于p2点,横坐标为可以取layoutWidth任意值 - ivWidth,纵坐标对应区间应该为【0,layoutHeight/2】;
    4. 对于p3点,横坐标为可以取layoutWidth任意值 - ivWidth,纵坐标对应区间应该为0;
  • 当我们计算出爱心上升轨迹后,不断的去更新爱心的x,y坐标,同时伴随着透明度的变化,当动画执行结束,移除此爱心,自此,整个效果便可以实现。

相关源码

自定义鲜花点赞效果FlowersLayout

package com.crystal.view.animationimport android.animation.*
import android.content.Context
import android.graphics.PointF
import android.util.AttributeSet
import android.view.ViewGroup
import android.view.animation.*
import android.widget.ImageView
import android.widget.RelativeLayout
import androidx.appcompat.content.res.AppCompatResources
import com.crystal.view.R/*** 自定义鲜花点赞效果【三阶贝塞尔曲线使用】* on 2022/11/11*/
class FlowersLayout : RelativeLayout {/*** 资源文件*/private val imageRes = intArrayOf(R.drawable.pl_blue, R.drawable.pl_red, R.drawable.pl_yellow)/*** 差值器集合,用于贝塞尔随机设置差值器*/private val interpolator = arrayListOf<Interpolator>(AccelerateDecelerateInterpolator(),AccelerateInterpolator(), DecelerateInterpolator(), LinearInterpolator())/*** 布局宽高*/private var layoutWidth = 0private var layoutHeight = 0/*** 鲜花宽高*/private var ivWidth = 0fprivate var ivHeight = 0fprivate var random = java.util.Random()private var layoutParams: LayoutParamsconstructor(context: Context) : this(context, null)constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {val drawable = AppCompatResources.getDrawable(context, R.drawable.pl_blue)!!ivWidth = drawable.intrinsicWidth.toFloat()ivHeight = drawable.intrinsicHeight.toFloat()layoutParams =LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)layoutParams.addRule(ALIGN_PARENT_BOTTOM)layoutParams.addRule(CENTER_HORIZONTAL)}override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {super.onMeasure(widthMeasureSpec, heightMeasureSpec)layoutWidth = MeasureSpec.getSize(widthMeasureSpec)layoutHeight = MeasureSpec.getSize(heightMeasureSpec)}/*** 添加鲜花到布局中*/fun addFlower() {val ivFlower = ImageView(context)ivFlower.setImageResource(imageRes[random.nextInt(imageRes.size - 1)])ivFlower.layoutParams = layoutParamsaddView(ivFlower)//执行相关动画executeAnimations(ivFlower)}private fun executeAnimations(ivFlower: ImageView) {//所有动画集合val allAnimator = AnimatorSet()//刚添加进来的时候伴随着透明度和放大效果val initAnimator = AnimatorSet()val alphaAnimator = ObjectAnimator.ofFloat(ivFlower, "alpha", 0.3f, 1f)val scaleXAnimator = ObjectAnimator.ofFloat(ivFlower, "scaleX", 0.3f, 1f)val scaleYAnimator = ObjectAnimator.ofFloat(ivFlower, "scaleY", 0.3f, 1f)initAnimator.playTogether(alphaAnimator, scaleXAnimator, scaleYAnimator)initAnimator.duration = 300allAnimator.playSequentially(initAnimator, constructBezierAnimator(ivFlower))allAnimator.addListener(object : AnimatorListenerAdapter() {override fun onAnimationEnd(animation: Animator?) {//动画执行完毕,移除鲜花viewremoveView(ivFlower)}})allAnimator.start()}/*** 构造三阶贝塞尔曲线动画*/private fun constructBezierAnimator(ivFlower: ImageView): Animator {//P0点为起始点,坐标应为(width/2 - iv.width/2,height - iv.height)val p0 = PointF(layoutWidth / 2 - ivWidth / 2,layoutHeight - ivHeight)//P1点为控制点 x坐标在屏幕范围内即可,y坐标范围应该在【height/2 ~ height】之间,这里我们均选随机数val p1 = PointF(random.nextInt(layoutWidth) - ivWidth,(random.nextInt(layoutHeight / 2) + layoutHeight / 2).toFloat())//P2点为控制点 x坐标在屏幕范围内即可,y坐标范围应该在【 0 ~ height/2 】之间,这里我们均选随机数val p2 = PointF(random.nextInt(layoutWidth) - ivWidth,random.nextInt(layoutHeight / 2).toFloat())//P3点为终点,x坐标在屏幕范围内,y坐标应该为0点val p3 = PointF(random.nextInt(layoutWidth) - ivWidth, 0f)val typeEvaluator = FlowersTypeEvaluator(p1, p2)val bezierAnimator = ObjectAnimator.ofObject(typeEvaluator, p0, p3)//随机选取差值器,效果更佳bezierAnimator.interpolator = interpolator[random.nextInt(interpolator.size - 1)]bezierAnimator.duration = 3000bezierAnimator.addUpdateListener {val point = it.animatedValue as PointF//设置三阶贝塞尔曲线获取的数据,不断移动鲜花的位置ivFlower.x = point.xivFlower.y = point.y//改变鲜花的透明度ivFlower.alpha = (1 - it.animatedFraction + 0.2f)}return bezierAnimator}private class FlowersTypeEvaluator(val p1: PointF, val p2: PointF) : TypeEvaluator<PointF> {override fun evaluate(t: Float, p0: PointF, p3: PointF): PointF {//三阶贝塞尔曲线公式:B(t) = P0 * (1-t)^3 + 3 * P1 * t * (1-t)^2 + 3 * P2 * t^2 * (1-t) + P3 * t^3, t ∈ [0,1]val point = PointF()point.x =p0.x * (1 - t) * (1 - t) * (1 - t) + 3 * p1.x * t * (1 - t) * (1 - t) + 3 * p2.x * t * t * (1 - t) + p3.x * t * t * tpoint.y =p0.y * (1 - t) * (1 - t) * (1 - t) + 3 * p1.y * t * (1 - t) * (1 - t) + 3 * p2.y * t * t * (1 - t) + p3.y * t * t * treturn point}}
}

总结

通过实现点赞撒花效果,了解了Android中三阶贝塞尔的使用方式,同时对属性动画的使用有了进一步的认知。

结语

如果以上文章对您有一点点帮助,希望您不要吝啬的点个赞加个关注,您每一次小小的举动都是我坚持写作的不懈动力!ღ( ´・ᴗ・` )

这篇关于Android自定义View之点赞撒花(三阶贝塞尔曲线应用)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Python标准库之数据压缩和存档的应用详解

《Python标准库之数据压缩和存档的应用详解》在数据处理与存储领域,压缩和存档是提升效率的关键技术,Python标准库提供了一套完整的工具链,下面小编就来和大家简单介绍一下吧... 目录一、核心模块架构与设计哲学二、关键模块深度解析1.tarfile:专业级归档工具2.zipfile:跨平台归档首选3.

使用IDEA部署Docker应用指南分享

《使用IDEA部署Docker应用指南分享》本文介绍了使用IDEA部署Docker应用的四步流程:创建Dockerfile、配置IDEADocker连接、设置运行调试环境、构建运行镜像,并强调需准备本... 目录一、创建 dockerfile 配置文件二、配置 IDEA 的 Docker 连接三、配置 Do

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

深入浅出SpringBoot WebSocket构建实时应用全面指南

《深入浅出SpringBootWebSocket构建实时应用全面指南》WebSocket是一种在单个TCP连接上进行全双工通信的协议,这篇文章主要为大家详细介绍了SpringBoot如何集成WebS... 目录前言为什么需要 WebSocketWebSocket 是什么Spring Boot 如何简化 We

Java Stream流之GroupBy的用法及应用场景

《JavaStream流之GroupBy的用法及应用场景》本教程将详细介绍如何在Java中使用Stream流的groupby方法,包括基本用法和一些常见的实际应用场景,感兴趣的朋友一起看看吧... 目录Java Stream流之GroupBy的用法1. 前言2. 基础概念什么是 GroupBy?Stream

python中列表应用和扩展性实用详解

《python中列表应用和扩展性实用详解》文章介绍了Python列表的核心特性:有序数据集合,用[]定义,元素类型可不同,支持迭代、循环、切片,可执行增删改查、排序、推导式及嵌套操作,是常用的数据处理... 目录1、列表定义2、格式3、列表是可迭代对象4、列表的常见操作总结1、列表定义是处理一组有序项目的

C#中的Converter的具体应用

《C#中的Converter的具体应用》C#中的Converter提供了一种灵活的类型转换机制,本文详细介绍了Converter的基本概念、使用场景,具有一定的参考价值,感兴趣的可以了解一下... 目录Converter的基本概念1. Converter委托2. 使用场景布尔型转换示例示例1:简单的字符串到

springboot自定义注解RateLimiter限流注解技术文档详解

《springboot自定义注解RateLimiter限流注解技术文档详解》文章介绍了限流技术的概念、作用及实现方式,通过SpringAOP拦截方法、缓存存储计数器,结合注解、枚举、异常类等核心组件,... 目录什么是限流系统架构核心组件详解1. 限流注解 (@RateLimiter)2. 限流类型枚举 (

Spring Boot Actuator应用监控与管理的详细步骤

《SpringBootActuator应用监控与管理的详细步骤》SpringBootActuator是SpringBoot的监控工具,提供健康检查、性能指标、日志管理等核心功能,支持自定义和扩展端... 目录一、 Spring Boot Actuator 概述二、 集成 Spring Boot Actuat

PyTorch中的词嵌入层(nn.Embedding)详解与实战应用示例

《PyTorch中的词嵌入层(nn.Embedding)详解与实战应用示例》词嵌入解决NLP维度灾难,捕捉语义关系,PyTorch的nn.Embedding模块提供灵活实现,支持参数配置、预训练及变长... 目录一、词嵌入(Word Embedding)简介为什么需要词嵌入?二、PyTorch中的nn.Em