美文网首页Android自定义View
动画(View Animation)

动画(View Animation)

作者: 一字节溢出 | 来源:发表于2016-11-25 16:29 被阅读19次

    概述:

    View Animation 可以使视图执行补间动画。即给定两个关键帧,然后中间部分按照一定的算法自动计算补充。
    Android给出了下面几个实现类,通常我们用AnimationSet将其他几个组合使用,来达到相应的效果。

    • Animation (基类)
    • AlphaAnimation(透明度渐变动画)
    • RotateAnimation(旋转动画)
    • ScaleAnimation(缩放动画)
    • TranslateAnimation(位移动画)
    • AnimationSet(动画集合)

    详解:

    Animation (基类)

    定义了一些基础方法,如:设置动画持续时间,设置动画插播器,设置动画是否重复执行等。这样子类只需关注自己的特色方法,不必重复实现这些公共方法了。

    AlphaAnimation(透明度渐变动画)

    java代码实现:

    AlphaAnimation(float fromAlpha, float toAlpha)
    

    xml实现:

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android" >
    <alpha
    android:fromAlpha="0.1"
    android:toAlpha="1.0"
    android:duration="3000"/>
    </set>
    

    参数解释:
    fromAlpha:动画开始时view的透明度(0f~1f)
    toAlpha:动画结束时view的透明度(0f~1f)

    RotateAnimation(旋转动画)

    java代码实现:

    RotateAnimation(float fromDegrees, float toDegrees)
    RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY)
    RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
    

    xml实现:

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
    <rotate 
            android:interpolator="@android:anim/accelerate_decelerate_interpolator"
            android:fromDegrees="0" 
            android:toDegrees="+350"
            android:pivotX="50%" 
            android:pivotY="50%"
            android:duration="3000" />
    </set>
    

    参数解释:
    fromDegrees:开始转动时的角度,可以大于360 也可以小0。
    toDegrees:结束转动的角度。
    pivotXType,pivotYType:参考类型。有三种Animation.ABSOLUTE,Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT
    pivotXValue,pivotYValue:当参考类型为ABSOLUTE时为数值其他为百分比

    ScaleAnimation(缩放动画)

    java代码实现:

    ScaleAnimation(float fromXScale, float toXScale, float fromYScale, float toYScale)
    ScaleAnimation(float fromXScale, float toXScale, float fromYScale, float toYScale, float pivotX, float pivotY)
    ScaleAnimation(float fromXScale, float toXScale, float fromYScale, float toYScale, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
    

    xml实现:

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
       <scale
              android:interpolator="@android:anim/accelerate_decelerate_interpolator"
              android:fromXScale="0"
              android:toXScale="1"
              android:fromYScale="0"
              android:toYScale="3.3"
              android:pivotX="100%"
              android:pivotY="100%"
              android:duration="1000" />
    </set>
    

    参数解释:
    fromXScale,fromYScale,toXScale, toYScale:X,Y坐标上开始和结束时缩放的尺寸
    pivotXType, pivotXValue, pivotYType, pivotYValue:参考类型和距离参考物值和上面的RotateAnimation类似。

    TranslateAnimation(位移动画)

    java代码实现:

    TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
    TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue)
    

    xml实现:

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
    android:fromXDelta="30"
    android:toXDelta="80"
    android:fromYDelta="30"
    android:toYDelta="300"
    android:duration="2000"/>
    </set>
    

    参数解释:
    fromXDelta,toXDelta, fromYDelta,toYDelta,x,y坐标上开始和结束点的位置
    fromXType,fromXValue...参考类型和距离参考物的值

    示例:

    目前,View Animation主要应用有下面几点,我不在这里列举了,用的时候再sdk和apidemo中可以找到,如下图是sdk中的动画定义,自己拷贝出来随意修改下即可,后期有时间会补充。
    1,窗口(Activity,Dialog,popupwindow)的打开和关闭时的动画。
    2,独立View对象的出现,消失以及翻转等。

    Paste_Image.png

    结语:

    在使用View Animation时我们可能容易忽略下面几点,需要注意下。
    1,View Animation动画执行以后,视图虽然发生了改变,但是View的真实位置并没有发生改变。
    2,android:fillAfter="true"等有些属性在xml中要生效,必须直接放在set标签下。
    3,Android中自带了很多动画实现,我们要善于查找和使用。

    上篇:View, ViewGroup, Layout

    相关文章

      网友评论

        本文标题:动画(View Animation)

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