Android动画之Interpolator插补器

来源:互联网 发布:落叶能知易水寒下句 编辑:程序博客网 时间:2024/05/17 10:52

在使用动画Animation或者AnimationSet的时候,有一个interpolator插补器的属性。可以使用这个属性来修饰动画运动的速率。比如加速、先加速再加速等。安卓api提供几个已经实现好的插补器:

AccelerateDecelerateInterpolator (效果)加速减速插补器(先慢后快再慢)

AccelerateInterpolator 加速插补器(先慢后快)

AnticipateInterpolator 向前插补器(先往回跑一点,再加速向前跑)

AnticipateOvershootInterpolator 向前向后插补器(先往回跑一点,再向后跑一点,再回到终点)

BounceInterpolator 反弹插补器(在动画结束的时候回弹几下,如果是竖直向下运动的话,就是玻璃球下掉弹几下的效果)

CycleInterpolator 循环插补器(按指定的路径以指定时间(或者是偏移量)的1/4、变速地执行一遍,再按指定的轨迹的相反反向走1/2的时间,再按指定的路径方向走完剩余的1/4的时间,最后回到原点。假如:默认是让a从原点往东跑100米。它会先往东跑100米,然后往西跑200米,再往东跑100米回到原点。可在代码中指定循环的次数)

DecelerateInterpolator 减速插补器(先快后慢)

LinearInterpolator 直线插补器(匀速)

OvershootInterpolator 超出插补器(向前跑直到越界一点后,再往回跑)

这些插补器的使用方法有二:

1.在资源文件的interpolator属性中指定

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set  
  3.     xmlns:android="http://schemas.android.com/apk/res/android">  
  4.     <translate  
  5.         android:interpolator="@android:anim/overshoot_interpolator"  
  6.         android:fromXDelta="0"  
  7.         android:toXDelta="200%"  
  8.         android:duration="1000"/>  
  9. </set>  

也可以在set标签里面设置,对其全部子动画都生效。但如果在set标签里面设置了,子标签里面设置的interpolator将不会生效。

set标签里面还有一个shareInterpolator属性,false表示该插补器不对全部子动画生效,true反之,如果设置了interpolator,则默认为true

2.在java类里面设置

[java] view plaincopy
  1. animation.setInterpolator(new AnticipateOvershootInterpolator());  
或者

[java] view plaincopy
  1. animation.setInterpolator(getBaseContext(),android.R.anim.anticipate_overshoot_interpolator);  

animation是一个Animation对象。

更多内容请关注本博客里系统插补器详解的相关内容。

0 0
原创粉丝点击