scaleanimationandroid之animator 和animation 的区别

scaleanimation  时间:2021-06-08  阅读:()

android之animator 和animation 的区别

一、 前言 Animator框架是Android 4.0中新添加的一个动画框架,和之前的Animation框架相比,Animator可以进行更多和更精细化的32313133353236313431303231363533e4b893e5b19e31333361306435动画控制,而且比之前更简单和更高效。

在4.0源码中随处都可以看到Animator的使用。

二、 Animation和Animator比较 如下图,是Animation和Animator两个类继承图的对比。

C:Object C:Object C:Animation C:Animator C:AlphaAnimation C:AnimatorSet C:AnimationSet C:ValueAnimator C:DummyAnimation C:ObjectAnimator C:Rotate3dAnimation C:TimeAnbimator C:RotateAniamtion C:ScaleAnimation C:TranslateAnimation Animation框架定义了透明度,旋转,缩放和位移几种常见的动画,而且控制的是一个整个View动画,实现原理是每次绘制视图时View所在的ViewGroup中的drawChild函数获取该View的Animation的Transformation值,然后调用canvas.concat(transformToApply.getMatrix()),通过矩阵运算完成动画帧,如果动画没有完成,继续调用invalidate()函数,启动下次绘制来驱动动画,动画过程中的帧之间间隙时间是绘制函数所消耗的时间,可能会导致动画消耗比较多的CPU资源。

在Animator框架中使用最多的是AnimatorSet和ObjectAnimator配合,使用ObjectAnimator进行更精细化控制,只控制一个对象的一个属性值,多个ObjectAnimator组合到AnimatorSet形成一个动画。

而且ObjectAnimator能够自动驱动,可以调用setFrameDelay(longframeDelay)设置动画帧之间的间隙时间,调整帧率,减少动画过程中频繁绘制界面,而在不影响动画效果的前提下减少CPU资源消耗。

三、 关键接口介绍 1. ObjectAnimator介绍 Animator框架封装得比较完美,对外提供的接口非常简单,创建一个ObjectAnimator只需通过如下图所示的静态工厂类直接返回一个ObjectAnimator对象。

传的参数包括一个对象和对象的属性名字,但这个属性必须有get和set函数,内部会通过java反射机制来调用set函数修改对象属性值。

还包括属性的初始值,最终值,还可以调用setInterpolator设置曲线函数。

2. AnimatorSet介绍 AnimatorSet主要是组合多个AnimatorSet和ObjectAnimator形成一个动画,并可以控制动画的播放顺序,其中还有个辅助类通过调用play函数获得。

3. AnimatorUpdateListner介绍 通过实现AnimatorUpdateListner,来获得属性值发生变化时的事件,在这个回调中发起重绘屏幕事件。

四、 使用实例 在Android4.0中的ApiDemo中有个BouncingBalls实例,描述了Animator框架的使用,当点击屏幕时,绘制一个球从点击位置掉到屏幕底部,碰到底部时球有压扁的效果,然后回弹到点击位置再消失。

代码如下: ShapeHolder newBall =addBall(event.getX(), event.getY()); // Bouncing animation with squash and stretch float startY = newBall.getY(); float endY = getHeight() - 50f; float h = (float)getHeight(); float eventY = event.getY(); int duration = (int)(500 * ((h - eventY)/h)); ValueAnimator bounceAnim = ObjectAnimator.ofFloat(newBall, "y", startY, endY); bounceAnim.setDuration(duration); bounceAnim.setInterpolator(new elerateInterpolator()); ValueAnimator squashAnim1 = ObjectAnimator.ofFloat(newBall, "x", newBall.getX(), newBall.getX() - 25f); squashAnim1.setDuration(duration/4); squashAnim1.setRepeatCount(1); squashAnim1.setRepeatMode(ValueAnimator.REVERSE); squashAnim1.setInterpolator(new DecelerateInterpolator()); ValueAnimator squashAnim2 = ObjectAnimator.ofFloat(newBall, "width", newBall.getWidth(), newBall.getWidth() + 50); squashAnim2.setDuration(duration/4); squashAnim2.setRepeatCount(1); squashAnim2.setRepeatMode(ValueAnimator.REVERSE); squashAnim2.setInterpolator(new DecelerateInterpolator()); ValueAnimator stretchAnim1 = ObjectAnimator.ofFloat(newBall, "y", endY, endY + 25f); stretchAnim1.setDuration(duration/4); stretchAnim1.setRepeatCount(1); stretchAnim1.setInterpolator(new DecelerateInterpolator()); stretchAnim1.setRepeatMode(ValueAnimator.REVERSE); ValueAnimator stretchAnim2 = ObjectAnimator.ofFloat(newBall, "height", newBall.getHeight(),newBall.getHeight() - 25); stretchAnim2.setDuration(duration/4); stretchAnim2.setRepeatCount(1); stretchAnim2.setInterpolator(new DecelerateInterpolator()); stretchAnim2.setRepeatMode(ValueAnimator.REVERSE); ValueAnimator bounceBackAnim = ObjectAnimator.ofFloat(newBall, "y", endY, startY); bounceBackAnim.setDuration(duration); bounceBackAnim.setInterpolator(newDecelerateInterpolator()); // Sequence the down/squash&stretch/upanimations AnimatorSet bouncer = new AnimatorSet(); bouncer.play(bounceAnim).before(squashAnim1); bouncer.play(squashAnim1).with(squashAnim2); bouncer.play(squashAnim1).with(stretchAnim1); bouncer.play(squashAnim1).with(stretchAnim2); bouncer.play(bounceBackAnim).after(stretchAnim2); // Fading animation - remove the ball when theanimation is done ValueAnimator fadeAnim = ObjectAnimator.ofFloat(newBall, "alpha", 1f, 0f); fadeAnim.setDuration(250); fadeAnim.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animatoranimation) { balls.remove(((ObjectAnimator)animation).getTarget()); } }); // Sequence the two animations to play oneafter the other AnimatorSet animatorSet = new AnimatorSet(); animatorSet.play(bouncer).before(fadeAnim); // Start the animation animatorSet.start();

pacificrack:$12/年-1G内存/1核/20gSSD/500g流量/1Gbps带宽

pacificrack在最新的7月促销里面增加了2个更加便宜的,一个月付1.5美元,一个年付12美元,带宽都是1Gbps。整个系列都是PR-M,也就是魔方的后台管理。2G内存起步的支持Windows 7、10、Server 2003\2008\2012\2016\2019以及常规版本的Linux!官方网站:https://pacificrack.com支持PayPal、支付宝等方式付款7月秒杀VP...

湖北50G防御物理服务器( 199元/月 ),国内便宜的高防服务器

4324云是成立于2012年的老牌商家,主要经营国内服务器资源,是目前国内实力很强的商家,从价格上就可以看出来商家实力,这次商家给大家带来了全网最便宜的物理服务器。只能说用叹为观止形容。官网地址 点击进入由于是活动套餐 本款产品需要联系QQ客服 购买 QQ 800083597 QQ 2772347271CPU内存硬盘带宽IP防御价格e5 2630 12核16GBSSD 500GB​30M​1个IP...

Hosteons:新上1Gbps带宽KVM主机$21/年起,AMD Ryzen CPU+NVMe高性能主机$24/年起_韩国便宜服务器

我们在去年12月分享过Hosteons新上AMD Ryzen9 3900X CPU及DDR4内存、NVMe硬盘的高性能VPS产品的消息,目前商家再次发布了产品更新信息,暂停新开100M带宽KVM套餐,新订单转而升级为新的Budget KVM VPS(SSD)系列,带宽为1Gbps端口,且配置大幅升级,目前100M带宽仅保留OpenVZ架构产品可新订购,所有原有主机不变,用户一直续费一直可用。Bud...

scaleanimation为你推荐
印度尼西亚国家代码印尼身份证号的编码规则是什么?(比如中国的1-6位是地址代码,7-14位是出生日期码等)遗传算法实例求助fortran语言编写的混合遗传算法例子那位大哥大姐有?kjava通用KJava是什么意思河北云办税厅用小度怎么打开河北教育资讯云平台?网络备份如何自运远程备份点心os点心 OS 用户界面(UI)的设计理念是什么?embed函数c语言“sqrt函数”的用法是什么?web推送WebSocket 是什么原理?如何实现消息实时推送web推送javaweb写的聊天网页是如何向客户端发送消息?病毒分析网站电脑中病毒了,应该去哪个网站?
域名是什么 互联网域名管理办法 分销主机 linode payoneer 老左博客 天猫双十一秒杀 圣诞促销 idc资讯 cdn联盟 游戏服务器出租 lamp什么意思 徐州电信 创速 云销售系统 阿里云邮箱怎么注册 windowsserver2008r2 时间服务器 godaddy中文 优惠服务器 更多