首先扯点别的:今天看了看十月一回家的火车票,只能说艰难,只剩下一张无座的票了,而且还是从下午一点到次日凌晨4点的慢车,太漫长了。难道临沂到上海就不能修一条高铁吗!看样这次得坐汽车了。

继续总结Android 属性动画的使用

经过前面的学习,我总结了,无论是使用ValueAnimator还是ObjectAnimator,它们之所以能够完成从初始状态到结束状态的过渡,是因为必须有一个继承自TypeEvaluator的一个evaluator。ofInt方法是内置的IntEvaluator,ofFloat方法是内置的FloatEvaluator,而我们使用ofObject方法的时候就得自定义一个继承自TypeEvaluator来完成动画的过渡。

这就好坐火车从菏泽(起点)去上海(终点),TypeEvaluator就好比是火车,可以让你在规定时间内到达上海。但是这个火车是怎么跑的就不知道了,它可能在一个站停的时间长一点,在另外一个站停的时间短一点。那么是什么控制火车的这些行为呢?就是接下来说的Interpolator。

Interpolator

Interpolator:翻译过来是差值器,可以把它想象为汽车上的档位,挂不同的档速度是不一样的,但是必须得保证在规定的时间内跑到终点

不同类型的差值器都间接实现TimeInterpolator接口,这个接口的定义是这样的:

//一个时间差值器决定动画的变化速率。使得动画可以做非线性运动,例如加速或者减速。A time interpolator defines the rate of change of an animation. This allows animations to have non-linear motion, such as acceleration and deceleration.

一些系统定义好的Interpolator,系统默认的差值器是AccelerateDecelerateInterpolator。

// The time interpolator to be used if none is set on the animationprivate static final TimeInterpolator sDefaultInterpolator =         new AccelerateDecelerateInterpolator();

举个例子来看看下面的差值器的效果,我想改变button的translationY属性,让button向下移动1200px

 tY = button.getTranslationY(); o = ObjectAnimator.ofFloat(button, "translationY", tY, 1200); //不断更换差值器的类型 o.setInterpolator(new AccelerateInterpolator()); o.setDuration(2000); o.start();

//系统默认的方式,现加速后减速,动画在开始和结束的时候变化很慢,在中间阶段变化很快
AccelerateDecelerateInterpolator An interpolator where the rate of
change starts and ends slowly but accelerates through the middle.

//动画在开始的时候变化很慢,然后做加速运动
AccelerateInterpolator An interpolator where the rate of change starts
out slowly and and then accelerates.

//button会先向上移动一段距离,然后加速向下移动1200px
AnticipateInterpolator An interpolator where the change starts
backward then flings forward.

//button会先向上移动一段距离,然后加速向下,超过1200px的位置,然后最终停在1200px的
位置
AnticipateOvershootInterpolator An interpolator where the change starts
backward then flings forward and overshoots the target value and finally goes back to the final value.

//button 加速向下到达终点,然后弹起,落下,弹起,落下,最后停在终点
BounceInterpolator An interpolator where the change bounces at the
end.

//o.setInterpolator(new CycleInterpolator(2));button向下,向上(超过初始位
置),向下,再向上,然后回到原来的位置。
CycleInterpolator Repeats the animation for a specified number of
cycles.

//开始的时候变化很快,先后减速到达终点
DecelerateInterpolator An interpolator where the rate of change starts
out quickly and and then decelerates.

//加速很快,并一直保持加速直到终点
FastOutLinearInInterpolator Interpolator corresponding to
fast_out_linear_in.

//加速很快,但是减速很慢
FastOutSlowInInterpolator Interpolator corresponding to fast_out_slow_in.

//匀速直线运动
LinearInterpolator An interpolator where the rate of change is constant

//从最快的峰值速度逐渐减慢
LinearOutSlowInInterpolator Interpolator corresponding to linear_out_slow_in.

//加速超过终点,然后回到终点
OvershootInterpolator An interpolator where the change flings forward
and overshoots the last value then comes back.

//这个没有试
PathInterpolator An interpolator that can traverse a Path that extends
from Point (0, 0) to (1, 1).

如果想要实现自己的动画速率变换方式,可以自定义差值器

public class MyTimeInterpolator implements TimeInterpolator {    @Override    public float getInterpolation(float input) {        float result;        //intput值[0,1]        if (input <= 0.5) {            result = (float) (Math.sin(Math.PI * input)) / 2;        } else {            result = (float) (2 - Math.sin(Math.PI * input)) / 2;        }        return result;    }}
//使用自定义的差值器anim.setInterpolator(new MyTimeInterpolator ());  

ViewPropertyAnimator的用法(专为View提供的快捷方便的属性动画)

1 改变透明度

 button.animate()         .alpha(0)         .setDuration(2000)         .start();

2 组合动画

 button1.animate()        .scaleX(0.5f)        .alpha(0.5f)        .setDuration(3000)        .setInterpolator(new LinearInterpolator()).start();

参考链接:

  • Android属性动画完全解析(下),Interpolator和ViewPropertyAnimator的用法
  • TimeInterpolator
  • R.interpolator

结尾:该吃中午饭了。昨天发现一个很有趣的事情,我经常在家里说‘艰难’两个字,结果跟我合租房子的一个姐也学会了说这两个字,哈哈,当时就感觉到英吹思婷了!

更多相关文章

  1. Android动画浅析
  2. Android(安卓)自绘动画实现与优化实战——以 Tencent OS 录音机
  3. Qt Quick实现的疯狂算数游戏
  4. Android实现登陆界面动画
  5. 做一个有上下滚动效果的TextView
  6. Android下的动画处理
  7. Android(安卓)动画效果(一): 四种动画基础(Alpha、Translate、Rotate
  8. Android启动页面定时跳转的三种方法
  9. Android(安卓)实现书籍翻页效果----升级篇

随机推荐

  1. Android-->build.gradle-->packagingOpti
  2. Android版本号的识别——$(PLATFORM_VERS
  3. Android 在代码中设置ProgressBar的style
  4. android audio
  5. Android(安卓)Things简介
  6. android之RecycleView适配器添加点击事件
  7. Android(安卓)关于休眠的几个坑点
  8. [Android]listview图文混排
  9. android导入项目遇到的问题
  10. Android知识体系总结之Android部分之Toas