当前位置: 首页 > news >正文

ScaleAnimation开始结束位置分析

做项目的时候,需要用到动画,大小和位置都不一样。刚开始想到的是ScaleAnimation和TranslateAnimation进行组合,但实验后发现,目标位置始终不对,只用TranslateAnimation是没有问题,所以ScaleAnimation应该不只是进行了缩放

经过查找资料,发现ScaleAnimation还进行起始位置的移动。ScaleAnimation分为两种情况,从本身的位置缩放到另一个位置和从另一个位置缩放到本身的位置

先看一下处理后的效果

看一下ScaleAnimation的构造函数

    /*** fromX 在x轴方向,起始缩放比例* toX 在x轴上,目标缩放比例* fromY 在y轴方向,起始缩放比例* toY 在y轴上,目标缩放比例* pivotX 缩放的中心轴位置,这个跟我们自己的理解不一样,要通过算法算出来,这两种情况的算法还不一样* pivotY*/public ScaleAnimation(float fromX, float toX, float fromY, float toY,float pivotX, float pivotY) {mResources = null;mFromX = fromX;mToX = toX;mFromY = fromY;mToY = toY;mPivotXType = ABSOLUTE;mPivotYType = ABSOLUTE;mPivotXValue = pivotX;mPivotYValue = pivotY;initializePivotPoint();}


fromX, toX, fromY, toY这4个参数很好理解,我们重点看一下pivotX,pivotY是怎么计算的


- 从本身的位置缩放到另一个位置
这种情况下,我们关心的是缩放后的目标位置,这里有几个值需要先了解一些,目标view的右边(targetRight),初始view左边的距离(sourceLeft),pivotX,初始view的宽(sourceWidth),放大的值(toX),他们的关系如下
targetRight - sourceLeft = pivotX - (pivotX - sourceWidth) * toX,那么pivotX的值是pivotX = (targetRight - sourceLeft - sourceWidth * toX) / (1 - toX)

pivotY的值类似,就不在描述了。

- 从另一个位置缩放到本身的位置
这种情况我们关心的是开始的位置,它们的关系是sourceLeft - targetLeft = pivotX * (1 - scaleX),那么pivotX = (sourceLeft - targetLeft) / (1 - fromX)理清楚这个后,动画效果有缩放和移动的,只需要一个ScaleAnimation就能完成。

 

为了方便后期使用写了一个帮助类

public class TranslateAnimHelper {public static Animation tanslateScaleAnim(boolean fromOrigin, Rect sourceRect, Rect targetRect){Animation anim = null;float sx = targetRect.width() * 1.0f / sourceRect.width();float sy = targetRect.height() * 1.0f / sourceRect.height();boolean isScale = sx != 1 || sy != 1;if(isScale){anim = scaleAnim(fromOrigin, sourceRect, targetRect);}else{if(fromOrigin){int fromDeltaX = 0;int toDeltaX = targetRect.left - sourceRect.left;int fromDeltaY = 0;int toDeltaY = targetRect.top - sourceRect.top;anim = new TranslateAnimation(fromDeltaX, toDeltaX, fromDeltaY, toDeltaY);}else {int fromDeltaX = -(targetRect.left - sourceRect.left);int toDeltaX = 0;int fromDeltaY = -(targetRect.top - sourceRect.top);int toDeltaY = 0;anim = new TranslateAnimation(fromDeltaX, toDeltaX, fromDeltaY, toDeltaY);}}return anim;}public static Animation scaleAnim(boolean fromOrigin, Rect sourceRect, Rect targetRect){float sx = targetRect.width() * 1.0f / sourceRect.width();float sy = targetRect.height() * 1.0f / sourceRect.height();Animation animation = null;if(fromOrigin){float fromX = 1;float toX = sx;float fromY = 1;float toY = sy;float px = (targetRect.right - sourceRect.left - sourceRect.width() * sx) / (1 - toX);float py = (targetRect.bottom - sourceRect.top - sourceRect.height() * sy) / (1 - toY);animation = new ScaleAnimation(fromX, toX, fromY, toY, px, py);}else{float fromX =  1 / sx;float toX = 1;float fromY = 1 / sy;float toY = 1;float px = (sourceRect.left - targetRect.left) / (1 - fromX);float py = (sourceRect.top - targetRect.top) / (1 - fromY);animation = new ScaleAnimation(fromX, toX, fromY, toY, px, py);}return animation;}
}

github下载地址

http://www.xdnf.cn/news/813421.html

相关文章:

  • Ajax:万字总结黑马笔记,学懂Ajax看这一篇就够了
  • jQuery的基本使用
  • Eclipse Android开发环境搭建
  • 域渗透—域用户枚举与口令爆破
  • 6个月Python学习计划 Day 13 - 文件操作基础
  • 如何获取显卡的GPU占用率和显存占用情况
  • 新手小白零基础,该怎样学习编程呢?零基础入门到精通,收藏这一篇就够了
  • 搜索引擎的工作原理
  • 什么是a站、b站、c站、d站、e站、f站、g站、h站、i站、j站、k站、l站、m站、n站?
  • 【python】深度学习中一个保证训练测试样本数据均衡性的重要函数:StratifiedShuffleSplit
  • 网刻工具大全:四款软件优缺评析(转)
  • 漂亮的个人主页HTML源码
  • mkdirs与createNewFile区别,Java复制文件工具类
  • 素数环
  • 微信小程序-wxml和wxss样式
  • Linux常用命令_(文件权限)
  • [笔记]vs2015 编写汇编masm32之使用MASM32库
  • 天龙八部万象归一第8版单机安装教程+GM工具+虚拟机一键端
  • Ant下载及配置安装
  • 这回,数据库事务4种隔离级别及7种传播行为,终于说明清楚了
  • 机器学习实战(入门级) ------ Kaggle 泰坦尼克号幸存者预测 (随机森林,KNN,SVM)
  • 因 Cannot resolve com.lowagie:itext:2.1.7.js6,选择手动安装 Jar 包进 Maven 仓库
  • linux:账号管理
  • C语言:从零基础到精通—轻松写出第一个C语言程序
  • QuickTime专业版 pro 注册码
  • Windows 2000 Server系统下载!全套镜像下载!
  • 简单的鱼群算法实现
  • 数据采集网关的功能和应用场景
  • 用户'NT AUTHORITY/NETWORK SERVICE' 登录失败解决办法
  • adb shell 命令详解