Android 动画之插值器PathInterpolator

news2024/10/4 3:30:08

Android 的View动画、属性动画都可以设置动画插值器,以此来实现不同的动画效果。

这篇文章 Android View动画整理 有介绍各种插值器的效果,这一篇专访 PathInterpolator

参考官网 添加曲线动作 ,

PathInterpolator 基于 贝塞尔曲线Path 对象。此插值器在一个 1x1 的正方形内指定一个动作曲线,定位点位于 (0,0) 和 (1,1),而控制点则使用构造函数参数指定。

简单来说就是,通过控制点来构造出 (0,0) 到 (1,1) 之间的任意曲线,让动画按照构造出的曲线来执行。

PathInterpolator 和其他动画插值器的使用是一样的,PathInterpolator 的优势是可以创建任意曲线来实现不同的动画效果,劣势是比较难绘制出满意的曲线,毕竟涉及了数学公式。

贝塞尔曲线

贝塞尔曲线的相关说明:
贝塞尔曲线_百度百科
从零开始学图形学:10分钟看懂贝塞尔曲线
曲线篇: 贝塞尔曲线

贝塞尔曲线在线测试网站:
Bezier Curve Demos
cubic-bezier
贝塞尔曲线在线绘制🚀

PathInterpolator 的构造函数,

  • PathInterpolator(Path path) :利用 Path 对象创建插值器。
  • PathInterpolator(float controlX, float controlY) :传入一个控制点坐标(controlX,controlY),构造二维贝塞尔曲线插值器。
  • PathInterpolator(float controlX1, float controlY1, float controlX2, float controlY2) :传入两个控制点坐标 (controlX1,controlY1 )、(controlX2, controlY2),构造三维贝塞尔曲线插值器。
  • PathInterpolator(Context context, AttributeSet attrs) :通过 AttributeSet 加载插值器。

PathInterpolator(Path path)

先看通过 PathInterpolator(Path path) 构建。

Path

构造函数,直接 new Path 创建即可。

	/**
     * Create an empty path
     */
    public Path() {
        mNativePath = nInit();
        sRegistry.registerNativeAllocation(this, mNativePath);
    }

Path.moveTo

移动到指定坐标

/**
     * Set the beginning of the next contour to the point (x,y).
     *
     * @param x The x-coordinate of the start of a new contour
     * @param y The y-coordinate of the start of a new contour
     */
    public void moveTo(float x, float y) {
        nMoveTo(mNativePath, x, y);
    }

Path.lineTo

从上一个点绘制一条线到给定的点 (x,y),

	/**
     * Add a line from the last point to the specified point (x,y).
     * If no moveTo() call has been made for this contour, the first point is
     * automatically set to (0,0).
     *
     * @param x The x-coordinate of the end of a line
     * @param y The y-coordinate of the end of a line
     */
    public void lineTo(float x, float y) {
        isSimplePath = false;
        nLineTo(mNativePath, x, y);
    }

看如下代码,X 轴 、Y 轴 都移动一段距离,X 轴 、Y 轴 都用 LinearInterpolator ,

ObjectAnimator animationX = ObjectAnimator.ofFloat(imageViewIcon, "translationX",imageViewIcon.getWidth() * 5);
animationX.setInterpolator(new LinearInterpolator());

ObjectAnimator animationY = ObjectAnimator.ofFloat(imageViewIcon, "translationY",imageViewIcon.getWidth() * 5);
animationY.setInterpolator(new LinearInterpolator());

AnimatorSet set = new AnimatorSet();
set.playTogether(animationX, animationY);
set.setDuration(5000).start();

动画效果, imageViewIcon 按照对角线移动。
在这里插入图片描述
动画轨迹是起点、终点之间的直线,Path.lineTo 画线正合适, 用 PathInterpolator 来实现同样的效果, Go ~

		Path path = new Path();
        path.moveTo(0f,0f);
        path.lineTo(0.25f,0.25f);
        path.lineTo(0.5f,0.5f);
        path.lineTo(0.75f,0.75f);
        path.lineTo(1f,1f);
        PathInterpolator pathInterpolator = new PathInterpolator(path);

        ObjectAnimator animationX = ObjectAnimator.ofFloat(imageViewIcon, "translationX",imageViewIcon.getWidth() * 5);
        animationX.setInterpolator(pathInterpolator);

        ObjectAnimator animationY = ObjectAnimator.ofFloat(imageViewIcon, "translationY",imageViewIcon.getWidth() * 5);
        animationY.setInterpolator(new LinearInterpolator());

        AnimatorSet set = new AnimatorSet();
        set.playTogether(animationX,animationY);
        set.setDuration(5000).start();

使用也简单,三大步:

  • 创建 Path ,选取了 5 个点(可以更多)来绘制 (0f,0f) 到 (1f,1f) 的曲线(效果是直线,直线 ∈ 曲线)。
  • 通过 Path 创建 PathInterpolator 。
  • 动画指定用创建的 pathInterpolator 。控制变量法,X 轴用新创建的 pathInterpolator , Y 轴继续用 LinearInterpolator 。

效果,
在这里插入图片描述
肉眼看不出和 LinearInterpolator 的差别。

Path.arcTo

画弧,

通过前4个参数确定矩形,从而得到椭圆圆心,

startAngle 是起始位置相对于圆心的角度,sweepAngle 是相对于圆心需要旋转的角度,两者可以确定起始角度,

有圆心,有角度,弧形不就出来了。

	/**
     * Append the specified arc to the path as a new contour. If the start of
     * the path is different from the path's current last point, then an
     * automatic lineTo() is added to connect the current contour to the
     * start of the arc. However, if the path is empty, then we call moveTo()
     * with the first point of the arc.
     *
     * @param startAngle  Starting angle (in degrees) where the arc begins
     * @param sweepAngle  Sweep angle (in degrees) measured clockwise, treated
     *                    mod 360.
     * @param forceMoveTo If true, always begin a new contour with the arc
     */
    public void arcTo(float left, float top, float right, float bottom, float startAngle,
            float sweepAngle, boolean forceMoveTo) {
        isSimplePath = false;
        nArcTo(mNativePath, left, top, right, bottom, startAngle, sweepAngle, forceMoveTo);
    }

如下代码,走个半圆的动画,
(icon 宽度和黑色线的位置是计算好距离的)

Path path = new Path();
path.arcTo(-imageViewGreen.getWidth()*4, 0, imageViewGreen.getWidth()*4, imageViewGreen.getWidth()*8, 270f, 180f, true);
ObjectAnimator animator = ObjectAnimator.ofFloat(imageViewIcon, View.X, View.Y, path);
animator.setDuration(5000);
animator.start();

运行效果,
在这里插入图片描述

附图说明,
在这里插入图片描述

图解,startAngle, sweepAngle
解释下 path.arcTo(-imageViewGreen.getWidth()*4, 0, imageViewGreen.getWidth()*4, imageViewGreen.getWidth()*8, 270f, 180f, true); 代码参数,

  • 点 A :图片原始位置,也是动画开始位置。(注意:动画开始位置可以不是图片原始位置)
  • 绿色箭头 j3 :动画轨迹,本例是半圆。
  • 点 B :动画结束位置。
  • 黄色框左上角 P1 :点 P1 由 点A 来确定,以动画开始位置为(0,0) ,它的坐标是 (-imageViewGreen.getWidth()*4, 0)。
  • 黄色框右下角 P2 :点 P2 由 点A 来确定,以动画开始位置为(0,0) ,它的坐标是 (imageViewGreen.getWidth()*4, imageViewGreen.getWidth()*8)。
  • 蓝色线交汇点 O :黄色框左上角点 P1 和黄色框右下角点 P2 确定了矩形(本例是正方向,正方形 ∈ 矩形),得到了最大内切红色椭圆(本例是正圆,正圆 ∈ 椭圆),确定了圆心点 O 。这个圆心是弧形的圆心,也就是动画旋转的中心。
  • 动画坐标系 :以点 O 为 圆心得到了动画坐标系,点O 有右方向是 0° ,下方向是 90° 。
    按照 j1 旋转为正角度,顺时针旋转为正角度,逆时针旋转为负角度。
    startAngle 是 A 点对于圆心O来说需要转 270° ,本例就是 270f 。
    sweepAngle 是需要旋转的角度,按照 j1 旋转为正角度,本例是 180f ,即顺时针旋转 180° 。
    在这里插入图片描述

很绕,捋一捋。如果错误,也请指正。

Path.quadTo 、Path.cubicTo

Path.quadTo ,绘制二阶贝塞尔曲线。起点是 (0,0), 终点是(x2,y2),控制点是(x1,y1) ,

/**
     * 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;
        nQuadTo(mNativePath, x1, y1, x2, y2);
    }

Path.cubicTo,绘制三阶阶贝塞尔曲线,起点是 (0,0), 终点是(x3,y3),控制点是(x1,y1) 和 (x2,y2) 。

	/**
     * 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;
        nCubicTo(mNativePath, x1, y1, x2, y2, x3, y3);
    }

PathInterpolator(float controlX, float controlY)

PathInterpolator(float controlX1, float controlY1, float controlX2, float controlY2)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/953347.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

cocos 2.4 版本 设置物理引擎步长 解决帧数不一致的设备 物理表现不一致问题 设置帧刷新率

官网地址Cocos Creator 3.8 手册 - 2D 物理系统 官网好像写的不太对 下面是我自己运行好使的 PhysicsManager.openPhysicsSystem()var manager cc.director.getPhysicsManager();// 开启物理步长的设置manager.enabledAccumulator true;// cc.PhysicsManagercc.PhysicsManag…

MacBookPro重装系统图文教程

关机 长按电源按钮10s即可强制关机 快捷键选择 Intel Command-R:获得安装过的最新的 macOS,但不会升级到最高版Option-Command-R:获得与 Mac 兼容的最新版 macOSShift-Option-Command-R:获得 Mac 自带的 macOS 或者与它最接近且…

pycharm安装各种包,包括scipy,pulp方法

方法1 在终端输入pip install scipy,然后就会开始下载,可能下载速度不是很快。另外,如果pip没有更新的话,也会影响,你需要在终端输入 python -m pip install --upgrade pip来更新 方法二 在 点击加号后&#xff…

idea解决maven打包不存在报错

maven打包失败 方法一方法二方法三 方法一 删除~/.m2/repository/对应目录或目录下的*.lastUpdated文件,然后再次运行maven命令 方法二 maven命令后加-U,如mvn package -U 方法三

社恐人福音!皮雀app以兴趣为链接,打造陌生人社交新模式

新一代年轻人被各种生活、工作和强社交关系充斥,面临着巨大的社交压力,因此他们在社交的选择方向上,逐渐远离线下社交,去选择线上社交,不同于有心理负担的线下社交,线上社交具有更多的选择性。基于能为年轻…

各种排序算法性能对比

C数据结构与算法 目录 冒泡排序 ​ 插入排序 ​ 选择排序 ​ 上图中最后一列为:nn*(n-1)/2 ​

SAP-MM-冲销凭证布局变更

业务场景: 仓管员在冲销物料凭证时MBST,显示行很少,只有7行,提出需求调整布局为多行,但是MBST没有调整布局功能, 解决:点击“定制本地布局-选项-字体设置”调整字体大小 跟据需求调整字体&…

12、监测数据采集物联网应用开发步骤(9.1)

监测数据采集物联网应用开发步骤(8.2) TCP/IP Server开发 在com.zxy.common.Com_Para.py中添加如下内容 #锁机制 lock threading.Lock() #本机服务端端口已被连接客户端socket list dServThreadList {} #作为服务端接收数据拦截器 ServerREFLECT_IN_CLASS "com.plug…

1.2 BEV感知算法数据形式

本文来自自动驾驶之心知识星球的国内首个BEV感知全栈系列学习教程 Birds-eye-view (BEV) Perception: A Survey and Collection 什么是图像 1. 图像是由相机生成的,是将三维世界 中的坐标点(单位为米)映射到二维 图像平面(单位像…

职场中的团队建设:超越任务,铸就默契

团队建设在职场中的重要性日益凸显。无论是初创公司还是大型企业,都需要一个高效、和谐且有创新能力的团队来推动业务发展。本文将深入探讨团队建设的活动和策略,帮助您构建一个卓越的团队。 1. 团队建设的重要性 提高团队凝聚力 团队凝聚力不仅仅是团…

python机器人编程——无人机python联动控制实现(VREP仿真)1——手搓一个类ROS机器人消息订阅发布模块

目录 一、前言二、总体设想三、系统的组成四、python代码构建构建一个MessageBroker消息代理类以下这个是常规的MessageBroker类:以下这个是引入协程的MessageBroker类:下面是使用MessageBroker消息代理类 构建一个DataProcessor消息预处理类构建一个Dat…

加入‘First Sowing’的公开部署马拉松(Deploy-a-Thon),共享巨额奖励

自从“First Sowing”启动以来已经有一段时间了,但等待终于结束!“First Sowing”传奇的精彩结局以“部署马拉松(Deploy-a-Thon)”的形式到来,我们非常高兴能与大家分享这一消息! 准备好与“First Sowing”…

【斗罗大陆】幽冥白虎再现,朱竹清后人继承衣钵,剧情逐渐高燃

Hello,小伙伴们,我是小郑继续为大家深度解析斗罗大陆! 绝世唐门最新的剧情已经更新,新生大赛再次出现了新的角色,也就是霍雨浩的哥哥戴华斌,他们也是霍雨浩冠军之路上最后的拦路虎,同样也拥有着当年叱咤风云…

MMCV安装指南

MMCV是一个开源的计算机视觉库,广泛用于基于Pytorch的深度学习项目中。本教程主要介绍MMCV的安装方法。 首先需要确认已安装Pytorch,CUDA及驱动版本。然后pip安装mmcv或从源码编译安装。需要注意 PyTorch、CUDA和MMCV版本匹配问题。 安装验证可以import mmcv测试是否成功。常…

优思学院|六西格玛中的概率分布有哪些?

为什么概率分布重要? 概率分布是统计学中一个重要的概念,它帮助我们理解随机变量的分布情况以及与之相关的概率。在面对具体问题时,了解概率分布可以帮助我们选择适当的检验或分析策略,以解决问题并做出合理的决策。 常见的概率…

Kafka系列四生产者

文章首发于个人博客,欢迎访问关注:https://www.lin2j.tech 一条消息从生产到发送成功,需要经过多个步骤,多个组件。首先要经过拦截器、序列化器、分区器对消息进行预处理,然后将消息按批次放入缓冲区,之后…

uniapp移动端h5设计稿还原

思路 动态设置html的font-size大小 实现步骤 先创建一个public.css文件,设置初始的font-size大小 /* 注意这样写 只能使用css文件, scss 是不支持的, setProperty 只适用于原生css上 */ html {--gobal-font-size: 0.45px; } .gobal-font-size {font-size: var(--g…

CRM通过哪四个特点赢得不同类型的客户

1.设置正确的目标 首先,在CRM系统中设置正确的目标是非常重要的。不同类型的客户有不同的需求和预期,需要使用不同的方法去处理。如果企业想吸引新客户,那么企业需要更加侧重于建立品牌形象和提供相关的信息。如果企业想留住老客户&#xff…

matplotlib绘图常见设置总结

绘图 官方API 头文件、画布初始化 首先要导入头文件,初始化画布 from matplotlib import pyplot as plt from matplotlib.pyplot import MultipleLocator # 从pyplot导入MultipleLocator类,这个类用于设置刻度间隔 import numpy as np # 常用的数据…

SQL求解用户连续登录天数

数据分析面试过程中,一般都逃不掉对SQL的考察,可能是笔试的形式,也可能是面试过程中面试官当场提问,当场在纸上写出,或者简单说一下逻辑。 今天,就来分享一道面试中常常被问到的一类SQL问题:连…