动画——视图动画(View Animation)

来源:互联网 发布:mac book怎么样 编辑:程序博客网 时间:2024/05/17 21:41

动画——View Animation

  • 简介:

    视图动画又称补间动画,是android中比较古老的一种动画,补间动画执行完以后
    并没有改变View的真是位置属性。

  • AlphaAnimation:透明度动画

    xml属性 Java方法 解释 android:fromAlpha AlphaAnimation(float) 开始的透明度 android:toAlpha AlphaAnimation(float) 结束的透明度
  • RotateAnimation:旋转动画

    xml属性 Java方法 解释 android:fromDegrees RotateAnimation(float) 开始旋转的角度,正表示顺时针 android:toDegrees RotateAnimation(float) 结束旋转的角度 android:pivotX RotateAnimation(float) 放缩起点X坐标(可以是数值、百分数、百分数p) android:pivotY RotateAnimation(float) 同上:数值表示加多少px作为起点,百分数表示等比扩大
  • ScaleAnimation:伸缩动画

    xml属性 Java方法 解释 android:fromXScale ScaleAnimation 的构造函数 起始时X轴放缩比例 android:toXScale ScaleAnimation 的构造函数 结束时X放缩比例 android:fromYScale ScaleAnimation 的构造函数 起始时Y轴放缩比例 android:toYScale ScaleAnimation 的构造函数 结束时Y放缩比例 android:pivotX ScaleAnimation 的构造函数 放缩起点X坐标(可以是数值、百分数) android:pivotY ScaleAnimation 的构造函数 同上:数值表示加多少px作为起点,百分数表示等比扩大
  • TranslateAnimation:位移动画

    xml属性 Java方法 解释 android:fromXDelta TranslateAnimation 的构造函数 起始时X轴坐标 android:toXDelta TranslateAnimation 的构造函数 结束时X放缩坐标 android:fromYDelta TranslateAnimation 的构造函数 起始时Y轴坐标 android:toYDelta TranslateAnimation 的构造函数 结束时Y轴坐标

    数值说明:坐标点可以是数值x,百分数x%,或者是百分数x%p
    数值x:表示以当前View左上角坐标加xpx为初始点
    百分数x%:表示以当前View的左上角加上当前View宽高的50%做为初始点
    百分数x%p:以当前View的左上角加上父控件宽高的50%做为初始点

  • AnimationSet:混合动画

    是四种动画的组合,没有自己的属性,在xml中使用的是set标签

  • 以上动画的基类:Animation
    位于android.view.animation包下,主要的属性:

    xml属性 Java方法 解释 android:detachWallpaper setDetachWallpaper 是否在壁纸上运行 android:duration setDuration(long) 动画持续时间,毫秒为单位 android:fillAfter setFillAfter(boolean) 动画结束时是否保持动画最后状态 android:fillBefore setFillBefore(boolean) 动画结束时是否保持开始前的状态 android:fillEnabled setFillEnabled(boolean) 与android:fillBefore效果相同 android:interpolator setInterpolator(Interpolator) 设定插值器(指定的动画效果,譬如回弹等) android:repeatCount setRepeatCount(int) 重复次数 android:repeatMode setRepeatMode(int) 重复类型:restart重头开始;reverse:倒序开始 android:startOffset setStartOffset(long) 调用开始后等待的时间 android:zAdjustment setZAdustment(int) Z轴上的位置
  • 基类:Animation的类方法

    除去很多的get、set以及重写的方法以后

    Animation类的方法 解释 reset() 重置Animation的初始化 cancel() 取消Animation动画 start() 开始Animation动画 setAnimationListener(AnimationListener listener)) 给当前Animation设置动画监听 hasStarted() 是否开始 hasEnded() 是否结束
  • View中操作动画的方法

    startAnimation(Animation animation):开始动画
    clearAnimation():清除动画

  • Interpolator插值器详解

    Java类 xml id 解释 AccelerateDecelerateInterpolator @android:anim/accelerate_decelerate_interpolator 动画始末速率较慢,中间加速 AccelerateInterpolator @android:anim/accelerate_interpolator 加速度 AnticipateInterpolator @android:anim/anticipate_interpolator 开始的时候从后向前甩 AnticipateOvershootInterpolator @android:anim/anticipate_overshoot_interpolator 类似上面 BounceInterpolator @android:anim/bounce_interpolator 类似真是的弹跳效果 CycleInterpolator @android:anim/bounce_interpolator 循环播放速率改变为正弦曲线 DecelerateInterpolator @android:anim/decelerate_interpolator 减速度 LinearInterpolator @android:anim/linear_interpolator 匀速 OvershootInterpolator @android:anim/overshoot_interpolator 向前弹出一定值之后回到原来位置 PathInterpolator 新增,定义路径坐标后按照路径坐标来跑。
  • 实例:
    anim:view_animation1

    <?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"     android:duration="5000"     android:interpolator="@android:anim/overshoot_interpolator"     android:shareInterpolator="true"    >    <alpha android:fromAlpha="0.5"           android:toAlpha="1"/>    <rotate android:fromDegrees="0"            android:toDegrees="500"        android:pivotY="100"/>    <translate android:fromXDelta="0"               android:toXDelta="500"        /></set>
  • 自定义插值器

    • 概述:

      原理:根据动画的进度,计算当前属性值的百分比。
      具体操作:方法一:通过xml重设已有插值器的某些属性,这种方法的自定义比较有限。
      方法二:通过Java代码实现Interpolator\TimeInterpolator接口,并重写getInterpolator(float input)
      方法,input的范围是[0,1]。补间动画实现Interpolator接口,属性动画实现TimeInterpolator接口。
      TimeInterpolator接口是属性动画中新增的,用于兼容Interpolator接口,这使得所有过去的Interpolator
      实现类都可以直接在属性动画使用。

    • xml法定义

      使用步骤:

      • 在res/anim/目录下创建filename.xml文件。
      • 修改你准备自定义的插值器如下:

        <?xml version="1.0" encoding="utf-8"?>
        <InterpolatorName xmlns:android="http://schemas.android.com/apk/res/android"
        android:attribute_name="value"/>

      • 在你的补间动画文件中引用该文件即可。

        accelerateDecelerateInterpolator:无可自定义的attribute。

        accelerateInterpolator:android:factor 浮点值,加速速率(默认值为1)

        anticipateInterpolator:android:tension 浮点值,起始点后拉的张力数(默认值为2)

        anticipateOvershootInterpolator:android:tension 浮点值,起始点后拉的张力数(默认值为2)。
        android:extraTension 浮点值,拉力的倍数(默认值为1.5)。

        bounceInterpolator:无可自定义的attribute。

        cycleInterpolator:android:cycles 整形,循环的个数(默认为1)。

        decelerateInterpolator:android:factor 浮点值,减速的速率(默认为1)。

        linearInterpolator:无可自定义属性

        overshootInterpolator:android:tension 浮点值,超出终点后的张力(默认为2)

    • java代码自定义插值器:

      • 内置插值器解析:

        public class AccelerateDecelerateInterpolator implements Interpolator, NativeInterpolatorFactory {
        // 仅贴出关键代码
        ...
        public float getInterpolation(float input) {
        return (float)(Math.cos((input + 1) * Math.PI) / 2.0f) + 0.5f;
        // input的运算逻辑如下:
        // 使用了余弦函数,因input的取值范围是0到1,
        //那么cos函数中的取值范围就是π到2π。
        // 而cos(π)的结果是-1,cos(2π)的结果是1
        // 所以该值除以2加上0.5后,getInterpolation()方法最终返回的
        //结果值还是在0到1之间。只不过经过了余弦运算之后,最终的
        //结果不再是匀速增加的了,而是经历了一个先加速后减速的过程
        // 所以最终,fraction值 = 运算后的值 = 先加速后减速
        // 所以该差值器是先加速再减速的
        }
        }

      • 自定义插值器:

        public class DecelerateAccelerateInterpolator implements Interpolator {
        @Override
        public float getInterpolation(float input) {
        float result;
        if (input <= 0.5) {
        result = (float) (Math.sin(Math.PI * input)) / 2;
        // 使用正弦函数来实现先减速后加速的功能,逻辑如下:
        // 因为正弦函数初始弧度变化值非常大,刚好和余弦函数是相反的
        // 随着弧度的增加,正弦函数的变化值也会逐渐变小,这样也就实现了减速的效果。
        // 当弧度大于π/2之后,整个过程相反了过来,现在正弦函数的弧度变化值非常小,
        // 渐渐随着弧度继续增加,变化值越来越大,弧度到π时结束,这样从0过度到π,
        // 也就实现了先减速后加速的效果
        } else {
        result = (float) (2 - Math.sin(Math.PI * input)) / 2;
        }
        return result;
        // 返回的result值 = 随着动画进度呈先减速后加速的变化趋势
        }
        }


致谢:

http://blog.csdn.net/yanbober/article/details/46481171

http://blog.csdn.net/carson_ho/article/details/72863901

原创粉丝点击