《Android开发艺术探索》
《属性动画相关的官方文档》android
Android的属性动画是Android3.0 推出的,属性动画彻底改善了View动画的弊端,若是不是一些地方还会用到View动画属性动画早就将View动画彻底替代了。属性动画之因此被称之为属性动画,是由于属性动画经过动态地改变View的属性来实现动画效果。好比说,属性动画经过改变一个View的X轴坐标来实现移动View位置的动画,经过属性动画移动了View的位置是真正意义上的改变了View的位置,而不是仅仅是像View动画那样将View的图像显示在指定位置。app
//设置ValueAnimator,1-100的浮点数之间的动画渐变 val valueAnimator = ValueAnimator.ofFloat(1.0F, 100.0F).setDuration(1000) //设置一个监听器 valueAnimator.addUpdateListener { val value = it.animatedValue as Float Log.e("TAG", "Value=$value") } valueAnimator.start() //输出结果 07-23 15:46:00.944 13240-13240/top.littledavid.studyanimation E/TAG: Value=1.0 07-23 15:46:00.945 13240-13240/top.littledavid.studyanimation E/TAG: Value=1.0 07-23 15:46:00.978 13240-13240/top.littledavid.studyanimation E/TAG: Value=1.0705773 07-23 15:46:01.010 13240-13240/top.littledavid.studyanimation E/TAG: Value=1.609426 //............................................ 07-23 15:46:01.431 13240-13240/top.littledavid.studyanimation E/TAG: Value=47.857613 07-23 15:46:01.450 13240-13240/top.littledavid.studyanimation E/TAG: Value=50.5 07-23 15:46:01.465 13240-13240/top.littledavid.studyanimation E/TAG: Value=53.142387 07-23 15:46:01.484 13240-13240/top.littledavid.studyanimation E/TAG: Value=55.6226 07-23 15:46:01.499 13240-13240/top.littledavid.studyanimation E/TAG: Value=58.243496 //........................................... 07-23 15:46:01.915 13240-13240/top.littledavid.studyanimation E/TAG: Value=99.73422 07-23 15:46:01.931 13240-13240/top.littledavid.studyanimation E/TAG: Value=99.92943 07-23 15:46:01.948 13240-13240/top.littledavid.studyanimation E/TAG: Value=100.0
上面的代码是一个ValueAnimator的Demo,ValueAnimator是仅仅是一种对数值的动画,ofFloat()
方法支持任意参数,若是指定了那么就会在规定时间内,完成数值的渐变。输出结果上咱们能够看出来,ValueAnimator平滑的从1过渡到了100。这里的数值的变化是平滑线性的,若是你不想要这种效果,那么可使用 差值器
来加速或者减速。关于差值器咱们会在另外一章提到。框架
由于ValueAnimator不能做用于对象,仅仅是对数值作了一个动画,若是咱们想将ValueAnimator做为动画的话,须要在ValueAnimator的动画监听中来手动地更新对象。ide
/** 创建一个简单的Person类,接线来咱们将会经过ValueAnimator来更新此对象的Age */ class Person(var name: String, var age: Int) { override fun toString(): String { return "I am is $name; I am $age years old!" } } var person = Person("鲁迅认识的那只猹", 0) ValueAnimator.ofInt(1, 50).apply { duration = 3000 //动画的每一帧都会调用此监听的回调 //咱们能够在此监听中更新咱们的对象 addUpdateListener { person.age = it.animatedValue as Int Log.e("TAG", person.toString()) } start() } /** 07-23 16:11:28.331 14623-14623/top.littledavid.studyanimation E/TAG: ------------------------------------- 07-23 16:11:28.882 14623-14623/top.littledavid.studyanimation E/TAG: ------------------------------------- 07-23 16:11:31.332 14623-14623/top.littledavid.studyanimation E/TAG: */
下面是一个经过ObjectAnimator来更改View的透明度的动画学习
/** 参数解释 1. 动画做用的对象 2. 要更改的属性 3. 设置动画渐变的数值 */ ObjectAnimator.ofInt(dogIV, "alpha", 0, 255).setDuration(3000).start()
ObjectAnimator的经常使用属性和方法:动画
ObjectAnimator.RESTART
和 ObjectAnimator.REVERSE
Restart表示从新开始,Reverse表示动画翻转交替进行。属性动画也能够经过XMl文件来定义,须要放在 res/animator
目录下:this
<?xml version="1.0" encoding="utf-8"?> <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android" android:propertyName="alpha" android:repeatCount="0" android:repeatMode="restart" android:valueFrom="0" android:valueTo="255" android:valueType="intType" />
代码调用rest
var animator = AnimatorInflater.loadAnimator(this, R.animator.alpa_animator) animator.setTarget(dogIV) animator.start()
经过上面的Demo的学习,可以得知属性动画的原理以下:code
属性动画要求其做用的对象的必须为该属性提供get和set方法,属性动画经过外界传递的该属性的初始值和最终值,以动画的形式来不断地调用 set
方法赋值,随着时间的推移,随着时间的推移该值会愈来愈接近最终值,若是想要动画生效须要知足一下的条件:xml
那么如今有一个问题
经过属性动画在3秒内将Button的长度从50px改到500px.
咱们都知道Button的是没有提供setWidth方法的。因此咱们不能直接设置width,因此咱们须要对Button作一层包装,经过一个包装类来包装一下Button对象而且提供get/setWidth 方法。
fun startAlphaObjectAnimator(view: View) { ObjectAnimator.ofInt(ButtonWrapper(testBtn), "width", 50, 500) .setDuration(3000) .start() } //Kotlin的写法要比Java的简化,直接经过定义属性来解决 private class ButtonWrapper(var targetBtn: Button) { var width = targetBtn.width set(value) { targetBtn.layoutParams.width = value } }
在上面的代码中咱们为Button类作了一些封装提供了get和set方法,由于Button不可以直接设置宽度,这是一种很好的解决思路。对一个对象的不能直接设置属性动画的属性来设置属性动画有一下集中解决办法:
除非特殊声明不然,本博客文章均属 鲁迅认识的那只猹 原创,未经许可禁止转载,不然将保留追究法律责任的权利。
若是本博客损害了您的相关权益,请及时联系我,我将妥善处理。