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

相关文章

CSS中的Static、Relative、Absolute、Fixed、Sticky的应用与详细对比

《CSS中的Static、Relative、Absolute、Fixed、Sticky的应用与详细对比》CSS中的position属性用于控制元素的定位方式,不同的定位方式会影响元素在页面中的布... css 中的 position 属性用于控制元素的定位方式,不同的定位方式会影响元素在页面中的布局和层叠关

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项

如何自定义一个log适配器starter

《如何自定义一个log适配器starter》:本文主要介绍如何自定义一个log适配器starter的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录需求Starter 项目目录结构pom.XML 配置LogInitializer实现MDCInterceptor

Python使用Tkinter打造一个完整的桌面应用

《Python使用Tkinter打造一个完整的桌面应用》在Python生态中,Tkinter就像一把瑞士军刀,它没有花哨的特效,却能快速搭建出实用的图形界面,作为Python自带的标准库,无需安装即可... 目录一、界面搭建:像搭积木一样组合控件二、菜单系统:给应用装上“控制中枢”三、事件驱动:让界面“活”

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

如何确定哪些软件是Mac系统自带的? Mac系统内置应用查看技巧

《如何确定哪些软件是Mac系统自带的?Mac系统内置应用查看技巧》如何确定哪些软件是Mac系统自带的?mac系统中有很多自带的应用,想要看看哪些是系统自带,该怎么查看呢?下面我们就来看看Mac系统内... 在MAC电脑上,可以使用以下方法来确定哪些软件是系统自带的:1.应用程序文件夹打开应用程序文件夹

Druid连接池实现自定义数据库密码加解密功能

《Druid连接池实现自定义数据库密码加解密功能》在现代应用开发中,数据安全是至关重要的,本文将介绍如何在​​Druid​​连接池中实现自定义的数据库密码加解密功能,有需要的小伙伴可以参考一下... 目录1. 环境准备2. 密码加密算法的选择3. 自定义 ​​DruidDataSource​​ 的密码解密3

spring-gateway filters添加自定义过滤器实现流程分析(可插拔)

《spring-gatewayfilters添加自定义过滤器实现流程分析(可插拔)》:本文主要介绍spring-gatewayfilters添加自定义过滤器实现流程分析(可插拔),本文通过实例图... 目录需求背景需求拆解设计流程及作用域逻辑处理代码逻辑需求背景公司要求,通过公司网络代理访问的请求需要做请

Python Flask 库及应用场景

《PythonFlask库及应用场景》Flask是Python生态中​轻量级且高度灵活的Web开发框架,基于WerkzeugWSGI工具库和Jinja2模板引擎构建,下面给大家介绍PythonFl... 目录一、Flask 库简介二、核心组件与架构三、常用函数与核心操作 ​1. 基础应用搭建​2. 路由与参

Spring Boot中的YML配置列表及应用小结

《SpringBoot中的YML配置列表及应用小结》在SpringBoot中使用YAML进行列表的配置不仅简洁明了,还能提高代码的可读性和可维护性,:本文主要介绍SpringBoot中的YML配... 目录YAML列表的基础语法在Spring Boot中的应用从YAML读取列表列表中的复杂对象其他注意事项总