属性动画

属性动画基础用法
1.介绍
Android系统为我们提供了三种动画效果的实现方式:
  • 补间动画(Tween Animation):只能对View进行动画操作,支持移动、旋转、透明、缩放四种效果,有一定的局限性。
  • 逐帧动画(Frame Animation):把一个动画分成多张图片,然后把这些图片连贯起来播放,原理和动画片类似。
  • 属性动画(Property Animation):通过改变对象的属性值来实现的动画效果,不局限使用在View上。
由于补间动画的局限性,Android团队在3.0版本中引入了属性动画,它的功能相当强大,弥补了补间动画的不足,几乎可以完全替代补间动画了。
如果你想在自定义控件的某一个点上使用动画,给View设置一个平滑的颜色过渡,或者使用3D旋转动画,那么属性动画一定是你最佳的选择,下面就让我们一起来看看属性动画的基本用法吧。
A、属性动画的特性:
  • a.支持对所有View能更新的属性的动画(需要属性的setXxx()和getXxx())。
  • b. 更改的是View实际的属性,所以不会影响其在动画执行后所在位置的正常使用。
  • c. Android3.0(API11)及以后出现的功能,3.0之前的版本可使用github第三方开源库nineoldandroids.jar进行支持。
B、属性动画的优缺点:
  • 缺点:(3.0+API出现)向下兼容问题
  • 优点:易定制,效果强

2.ValueAnimator
ValueAnimator是整个属性动画机制中最核心的一个类,因为属性动画就是通过不断的改变对象的属性值来实现的动画效果,初始值和结束值之间的动画过渡就是由ValueAnimator这个类来完成计算的。
ValueAnimator的用法还是比较简单的,比如想要把一个值从0平滑的过渡到1,间隔300ms,就可以这样写:

ValueAnimator animator = ValueAnimator.ofFloat(0f, 1f);
animator.setDuration(300);animator.start();

三行代码搞定,是不是很简单,但是这样运行起来是看不出任何效果的,还需要加上一个 监听器

ValueAnimator animator = ValueAnimator.ofFloat(0f, 1f);animator.setDuration(300);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

@Override
public void onAnimationUpdate(ValueAnimator animation) {
float currentValue = (float) animation.getAnimatedValue();
Log.i("onAnimationUpdate", "currentValue:" + currentValue);
}
});
animator.start();

运行一下,看下打印:
09-25 09:27:38.786 3419-3419/com.bwei.com.shuxinainmotion D/OpenGLRenderer: Enabling debug mode 0
09-25 09:27:38.796 3419-3419/com.bwei.com.shuxinainmotion I/onAnimationUpdate: currentValue:0.5
09-25 09:27:38.856 3419-3419/com.bwei.com.shuxinainmotion I/onAnimationUpdate: currentValue:0.7500001
09-25 09:27:38.876 3419-3419/com.bwei.com.shuxinainmotion I/onAnimationUpdate: currentValue:0.818712
09-25 09:27:38.886 3419-3419/com.bwei.com.shuxinainmotion I/onAnimationUpdate: currentValue:0.881898
09-25 09:27:38.896 3419-3419/com.bwei.com.shuxinainmotion I/onAnimationUpdate: currentValue:0.9330126
09-25 09:27:38.916 3419-3419/com.bwei.com.shuxinainmotion I/onAnimationUpdate: currentValue:0.968641
09-25 09:27:38.936 3419-3419/com.bwei.com.shuxinainmotion I/onAnimationUpdate: currentValue:0.99209785
09-25 09:27:38.946 3419-3419/com.bwei.com.shuxinainmotion I/onAnimationUpdate: currentValue:1.0


可以看到ValueAnimator帮我们计算了从0到1的过渡值,ofFloat方法是可以传入多个参数的,比如在1s内,从0过渡到6,再过渡到3,然后再过渡到10,就可以这样写:

ValueAnimator animator = ValueAnimator.ofFloat(0f, 6f, 3f, 10f);
animator.setDuration(1000);animator.start();

如果你不想要小数值过渡的话,还可以使用ofInt方法,使用和ofFloat方法相同,ValueAnimator中比较常用的就是ofFloat和oFInt方法了,还有一个ofObject,我会在下篇文章中讲解。
可以看到上述代码中,可以设置动画的播放时间,除此之外还有一些常见的功能:

// 设置动画的重复次数
// 直接传入int型数值,INFINITE代表无限循环
animator.setRepeatCount(ValueAnimator.INFINITE);
// 设置动画的重复模式
// RESTART代表重新播放,REVERSE代表倒序播放 animator.setRepeatMode(ValueAnimator.RESTART);animator.setRepeatMode(ValueAnimator.REVERSE);
// 设置动画的延迟播放时间
animator.setStartDelay(1000);



3.ObjectAnimator
与ValueAnimator相比ObjectAnimator才是我们最常用到的类,ObjectAnimator继承于ValueAnimator,其内部的动画机制是依靠ValueAnimator来完成的,由于是继承关系,在ObjectAnimator中也可以使用ValueAnimator中的一些方法,两者的用法也很类似,下面就让我们来看看ObjectAnimator的一些常见用法。
移动
1.把一张图片沿X轴向右移动100px,然后向左移动回到原位:
// 沿X轴向移动100px,然后向左移动回到原位// 沿Y轴移动,ofFloat第二个参数传入translationY
ObjectAnimator animator = ObjectAnimator.ofFloat(image, "translationX", 0f, 100f, 0f);
// 动画执行时长2s,默认300ms
animator.setDuration(2000);
animator.start();
2.3D旋转
把一张图片垂直旋转360度:
// 垂直旋转360度// 水平旋转,ofFloat第二个参数传入rotationX
ObjectAnimator animator = ObjectAnimator.ofFloat(image, "rotationY", 0f, 360f);
animator.setDuration(2000);
animator.start();
3.透明
把一张图片的透明度从0调节到1:
// 透明度从0到1
ObjectAnimator animator = ObjectAnimator.ofFloat(image, "alpha", 0f, 1f);
animator.setDuration(2000);
animator.start();

4.缩放
把一张图片水平缩放0.5倍,然后恢复:
// 水平缩放0.5倍,然后恢复// 垂直缩放,ofFloat第二个参数传入scaleY
ObjectAnimator animator = ObjectAnimator.ofFloat(image, "scaleX", 1f, 0.5f, 1f);
animator.setDuration(2000);
animator.start();
 

5.组合
属性动画中图片的移动是实现了图片真正意义上的移动,改变位置后可以点击,与此相比,补间动画只是改变了图片的显示效果,移动后不能点击,这也是属性动画和补间动画的一个重要区别。
ofFloat方法的第二个参数可以传入任意对象的属性名,ObjectAnimator内部的工作原理就是通过在View中查找属性名对应的get、set方法来设置对应的属性值的。
通俗的来讲,就是假如属性名是alpha,ObjectAnimator就会在ImageView中查找getAlpha方法和setAlpha方法,然后进行透明度设置的。


4.组合动画
组合动画需要借助AnimatorSet这个类来完成,AnimatorSet提供了play(Animator anim)方法,传入Animator对象之后返回一个AnimatorSet.Builder实例,AnimatorSet.Builder中有下面几个方法:
  • with(Animator anim):将现有的动画和传入的动画同时执行。
  • before(Animator anim):将现有的动画在传入的动画之前执行。
  • after(Animator anim):将现有的动画在传入的动画之后执行。
  • after(long delay):将现有的动画延时执行。
举个栗子:
// 图片从屏幕左侧移动到右侧,再回到原位,同时透明度从0调节到1,然后垂直旋转360度
ObjectAnimator trans = ObjectAnimator.ofFloat(image, "translationX", -100f, 100f, 0f);
ObjectAnimator alpha = ObjectAnimator.ofFloat(image, "alpha", 0f, 1f);
ObjectAnimator rotation = ObjectAnimator.ofFloat(image, "rotationY", 0f, 360f);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.play(trans).with(alpha).before(rotation);
animatorSet.setDuration(5000);animatorSet.start();

                   

5.第二种方法:使用xml编写动画
在res目录下创建一个animator文件夹,然后新建一个属性动画的xml文件,xml中可以使用如下三种标签:
  • animator:对应代码中的ValueAnimator类。
  • objectAnimator:对应代码中的ObjectAnimator类。
  • set:对应代码中的AnimatorSet类。
以上面的组合动画为例,使用xml来编写下:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:ordering="sequentially">

<set android:ordering="together">
<set android:ordering="sequentially">

<objectAnimator
android:duration="2000"
android:propertyName="translationX"
android:valueFrom="-100"
android:valueTo="100"
android:valueType="floatType" />

<objectAnimator
android:duration="2000"
android:propertyName="translationX"
android:valueFrom="100"
android:valueTo="0"
android:valueType="floatType" />
</set>

<objectAnimator
android:duration="2000"
android:propertyName="alpha"
android:valueFrom="0"
android:valueTo="1"
android:valueType="floatType" />
</set>

<objectAnimator
android:duration="2000"
android:propertyName="rotationY"
android:valueFrom="0"
android:valueTo="360"
android:valueType="floatType" />
</set>

ordering属性可以设置为sequentially(顺序执行)、together(同时执行)。
在代码中调用:
// 图片从屏幕左侧移动到右侧,再回到原位,同时透明度从0调节到1,然后垂直旋转360度
Animator animator = AnimatorInflater.loadAnimator(this, R.animator.combine);
animator.setTarget(image);animator.start();










  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值