美文网首页
动画效果总结

动画效果总结

作者: o动感超人o | 来源:发表于2018-09-10 17:22 被阅读26次

    只是简单记录一下,动画的实现很简单,步骤不多,在Android 3.0版本之前,我们使用的是逐帧动画(frame-by-frame animation)和补间动画(tweened animation),不过这两种方式灵活性很差,只能移动view,而且移动后只是绘制的位置变了,实际所在的位置仍然在原来的地方,所以你点击移动后的view是没效果的,因为实际上这个view还在移动前的位置。现在我们用的都是属性动画(Property Animator)了。属性动画用到的就这几个:

    1. ValueAnimator
    2. ObjectAnimator
    3. Interpolator
    4. TypeEvaluator
    5. AnimatorSet
    6. ViewPropertyAnimator

    下面一个个的记录一下:

    1. ValueAnimator

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

    用法很简单,就是ofFloat、ofInt或者ofObject,其中ofObject方法的第一个参数是一个TypeEvaluator的子类,使用方法见第4条

    2. ObjectAnimator

    该类继承自ValueAnimator,多了一种方法比如

    float curTranslationX = textview.getTranslationX();
    ObjectAnimator animator = ObjectAnimator.ofFloat(textview, "translationX", curTranslationX, -500f, curTranslationX);
    animator.setDuration(5000);
    animator.start();
    

    第一个参数是要变化的view,第二个参数是要变化的属性,如果该属性这view没有,有一个办法是可以继承该view然后写一个setXXX,然后方法内部去操作XXX属性,如上面的translationX,系统会去调用这个view的setTranslationX方法

    3. Interpolator

    这个控制动画播放时的播放速度,Interpolator并不是属性动画中新增的技术,实际上从Android 1.0版本开始就一直存在Interpolator接口了,而之前的补间动画当然也是支持这个功能的。只不过在属性动画中新增了一个TimeInterpolator接口,这个接口是用于兼容之前的Interpolator的,这使得所有过去的Interpolator实现类都可以直接拿过来放到属性动画当中使用。TimeInterpolator有很多系统自带的实现类,如下


    image.png

    比如先减速在加速之类的效果,有系统自带的实现了,如果有其他复杂的,就自己继承Interpolator重写就好了

    Interpolator是控制动画速度的,TypeEvaluator是使用ofObject方法时获取该速度下的值的

    4. TypeEvaluator

    //调用方式
    Point point1 = new Point(0, 0);
    Point point2 = new Point(300, 300);
    ValueAnimator anim = ValueAnimator.ofObject(new PointEvaluator(), point1, point2);
    anim.setDuration(5000);
    anim.start();
    
    public class PointEvaluator implements TypeEvaluator<Point>{
     
        @Override
        public Object evaluate(float fraction, Point startValue, Point endValue) {
            Point startPoint = startValue;
            Point endPoint = endValue;
            float x = startPoint.getX() + fraction * (endPoint.getX() - startPoint.getX());
            float y = startPoint.getY() + fraction * (endPoint.getY() - startPoint.getY());
            Point point = new Point(x, y);
            return point;
        }
    

    因为如果是Object的话系统肯定是不知道如何更新动画进度的,所以得自己去实现如何更新,所以要传入自己的TypeEvaluator

    5. AnimatorSet

    该类可以实现组合动画,可以让多个动画一起执行,或者A在B前面,等等

    ObjectAnimator anim1 = ...; 
    ObjectAnimator anim2 = ...;
    AnimatorSet animSet = new AnimatorSet();
    animSet.play(anim).with(anim2);
    animSet.setDuration(5000);
    animSet.start();
    

    6. ViewPropertyAnimator

    ViewPropertyAnimator其实算不上什么高级技巧,它的用法格外的简单,只不过和前面所学的所有属性动画的知识不同,它并不是在3.0系统当中引入的,而是在3.1系统当中附增的一个新的功能,因此这里我们把它作为整个属性动画系列的收尾部分。

    那我们先来回顾一下之前的用法吧,比如我们想要让一个TextView从常规状态变成透明状态,就可以这样写:

    ObjectAnimator animator = ObjectAnimator.ofFloat(textview, "alpha", 0f);
    animator.start();
    

    那么下面我们就来看一下如何使用ViewPropertyAnimator来实现同样的效果,ViewPropertyAnimator提供了更加易懂、更加面向对象的API,如下所示(两个例子不对应,就是举个例子。。。):

    textview.animate().x(500).y(500).setDuration(5000)
            .setInterpolator(new BounceInterpolator());
    

    在使用ViewPropertyAnimator时,我们自始至终没有调用过start()方法,这是因为新的接口中使用了隐式启动动画的功能,只要我们将动画定义完成之后,动画就会自动启动。

    我这部分看的郭霖的博客
    https://blog.csdn.net/guolin_blog/article/details/43536355
    https://blog.csdn.net/guolin_blog/article/details/43816093
    https://blog.csdn.net/guolin_blog/article/details/44171115

    就是我总结了一下,方便回顾

    相关文章

      网友评论

          本文标题:动画效果总结

          本文链接:https://www.haomeiwen.com/subject/itnsgftx.html