美文网首页
Android UI-属性动画(一)

Android UI-属性动画(一)

作者: twinsnan | 来源:发表于2016-11-19 18:24 被阅读0次

概述

自定义控件中,如果要做比较好的动画效果,除了理解动画的类型和动画的效果,深入理解动画的原理也是必不可少的一环。Android中动画分为几种形式,一种是View动画(包括补间动画和帧动画),还有一种是属性动画。这两种动画的实现的代码都比较简单,但是如果要做出更加复杂的动画,那么深入理解原理、运用插值、估值和回调监听是必不可少的。首先,我们来简单了解下View动画的的原理和使用,然后介绍属性动画。

View动画的种类

View动画有四种,分别是平移、缩放、旋转、和透明度的变换。当然除了四种之外,还要一个集合类型,可以见各种动画进行组合。在Android的官方开发文档中可以看档相应的继承关系。
<a>https://developer.android.com/reference/android/view/animation/Animation.html</a>


View动画的使用

动画可以通过XML方式调用,若下所示:

<set android:shareInterpolator="false">
    <scale
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:fromXScale="1.0"
        android:toXScale="1.4"
        android:fromYScale="1.0"
        android:toYScale="0.6"
        android:pivotX="50%"
        android:pivotY="50%"
        android:fillAfter="false"
        android:duration="700" />
    <set android:interpolator="@android:anim/decelerate_interpolator">
        <scale
           android:fromXScale="1.4"
           android:toXScale="0.0"
           android:fromYScale="0.6"
           android:toYScale="0.0"
           android:pivotX="50%"
           android:pivotY="50%"
           android:startOffset="700"
           android:duration="400"
           android:fillBefore="false" />
        <rotate
           android:fromDegrees="0"
           android:toDegrees="-45"
           android:toYScale="0.0"
           android:pivotX="50%"
           android:pivotY="50%"
           android:startOffset="700"
           android:duration="400" />
    </set>
</set>

也可以通过代码的方式去调用我们写的XML文件,如下:

// 找到View
ImageView spaceshipImage = (ImageView) findViewById(R.id.spaceshipImage);
// 通过XML布局加载Animation类
Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump);
// View.startAnimation()方法
spaceshipImage.startAnimation(hyperspaceJumpAnimation);

当然了,如果想更加方便,更加灵活的使用动画的话,那么就直接在代码中写动画,如下:

        ImageView view = (ImageView) findViewById(R.id.iv_animation);
        ScaleAnimation outerScaleAnimation
                = new ScaleAnimation(1.0f, 1.4f, 1.0f, 0.6f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        outerScaleAnimation.setFillAfter(false);
        outerScaleAnimation.setDuration(700);
        outerScaleAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
        ScaleAnimation innerScaleAnimation
                = new ScaleAnimation(1.4f, 0.0f, 0.6f, 0.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        innerScaleAnimation.setStartOffset(700);
        innerScaleAnimation.setDuration(400);
        innerScaleAnimation.setFillBefore(false);
        RotateAnimation innerRotateAnimation = new RotateAnimation(0.0f, -45.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        AnimationSet innerSet = new AnimationSet(true);
        innerSet.setInterpolator(new DecelerateInterpolator());
        innerSet.addAnimation(innerScaleAnimation);
        innerSet.addAnimation(innerRotateAnimation);
        AnimationSet outerSet = new AnimationSet(false);
        outerSet.addAnimation(outerScaleAnimation);
        outerSet.addAnimation(innerSet);
        view.startAnimation(outerSet);

效果如下图所示:


1.gif

简单介绍了下该如何使用动画,如果想具体了解各种动画的用法,可以去看Google的API,在API的除了可以使用父类的XML属性来设置外,也可以找到子类的各个属性的set方法,这些set方法都会在XML布局中有对应的字段。

属性动画介绍

其实Google对Android动画已经做了非常详细的解释,我们去看下Google官方文档是如何说明属性动画的。
首选来看下API中属性动画的集成关系,可以看到属性动画继承自Animator类。Animator类还有一个Set子类,主要是用来创建动画集合的。ValueAnimator类有一个重要也是常见的子类ObjectAnimator。


image.png

属性动画主要可以做5件是事情:
1.设置动画的时间
2.设置时间插值器
3.设置动画的行为,如是否重复和重复次数等
4.设置动画集合
5.更新动画帧的频率

属性动画的原理

image.png

先看下官方给的图,动画的时间是40ms,每隔一定的时间就根据时间重新计算下控件目前所应该在的位置。等到40ms结束,控件也就到了最终的位置上。可以发现这是匀速移动的动画,用的方法使线性插值。
如果我们不想要这么简单的动画呢,我们想要先加速后减速呢?这张图清楚的表达了这一点,在前面的10ms内,移动的距离只有6,热庵后加速,到达终点之前再减速。


image.png

ValueAnimator会根据时间,当有变化时会调用设置的AnimatorUpdateListener。在listener中会通过Animator来获得我们我们已经进行到了哪一步(调用animator.getAnimatedValue那么我们会得到一个0-100的值,调用getAnimattedFraction会得到一个0-1的值,表示当前动画的进度值。这个值的效果在每一个动画中都一样,因为时间流逝的快慢对每一个控件都是一样的。ValueAnimtor中持有一个插值器TimeInterpolator(这个插值器可以自己定义),通过这个插值器,我们可以获得插值后的值。然后会通过TypeEvaluator等获得最终要改变的值(前面计算出来的是进度值和插值,然后需要换算成我们想要改变的属性的值),改变控件的属性。然后重新调用listener中的方法,直到动画结束。


image.png

属性动画的调用

属性动画的调用并不复杂,可以像如下代码一样使用ValueAnimator:

ValueAnimator animation = ValueAnimator.ofFloat(0f, 1f);
animation.setDuration(1000);
animation.start();

上述代码的意思是,执行一个值从0-1变化的属性动画,执行事件时1000ms。
也可以自定义TypeEvaluator:

ValueAnimator animation = ValueAnimator.ofObject(new MyTypeEvaluator(), startPropertyValue, endPropertyValue);
animation.setDuration(1000);
animation.start();

但是大家应该已经发现了,ValueAnimator没有操作任何对象。其实他是一个空实现,我们自己可以通过他来做动画或者其他一些事情。
Android已经给我们封装好了一个可以对View的属性进行操作的类ObjectAnimator。代码一样非常简单:

ObjectAnimator anim = ObjectAnimator.ofFloat(foo, "alpha", 0f, 1f);
anim.setDuration(1000);
anim.start();

需要非常注意的一点是,属性动画只能改变有公开的get和set方法的属性。如果是动画效果,可能需要调用invalidate()方法。

最后和View动画一样,也有动画的集合这里便不再过多介绍,可以查看google的官方API:

AnimatorSet bouncer = new AnimatorSet();
bouncer.play(bounceAnim).before(squashAnim1);
bouncer.play(squashAnim1).with(squashAnim2);
bouncer.play(squashAnim1).with(stretchAnim1);
bouncer.play(squashAnim1).with(stretchAnim2);
bouncer.play(bounceBackAnim).after(stretchAnim2);
ValueAnimator fadeAnim = ObjectAnimator.ofFloat(newBall, "alpha", 1f, 0f);
fadeAnim.setDuration(250);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.play(bouncer).before(fadeAnim);
animatorSet.start();
ObjectAnimator animX = ObjectAnimator.ofFloat(myView, "x", 50f);
ObjectAnimator animY = ObjectAnimator.ofFloat(myView, "y", 100f);
AnimatorSet animSetXY = new AnimatorSet();
animSetXY.playTogether(animX, animY);
animSetXY.start();

我们也可以去监听属性动画,只要设置Animator.AnimatorListener就可以了,其中有四个方法onAnimationStart() 、onAnimationEnd() 、 onAnimationRepeat() 、onAnimationCancel() 分别在开始、结束、重复和取消时调用。
最后,属性动画也可以在XML中使用:

<set android:ordering="sequentially">
    <set>
        <objectAnimator
            android:propertyName="x"
            android:duration="500"
            android:valueTo="400"
            android:valueType="intType"/>
        <objectAnimator
            android:propertyName="y"
            android:duration="500"
            android:valueTo="300"
            android:valueType="intType"/>
    </set>
    <objectAnimator
        android:propertyName="alpha"
        android:duration="500"
        android:valueTo="1f"/>
</set>

其他还要一些如KeyFrame的定义,LayoutAnimator的使用就不在介绍,有兴趣可以查找相应的API。

总结

这一篇主要讲的该如何使用View动画和属性动画的使用。下一篇将主要介绍动画的源码和插值器的使用。

相关文章

网友评论

      本文标题:Android UI-属性动画(一)

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