zoukankan      html  css  js  c++  java
  • Android Animation学习(六) View Animation介绍

    View Animation

      View animation系统可以用来执行View上的Tween animationFrame animation

      Tween animation可以在View对象上执行一系列的简单变换,比如位置、尺寸、旋转、透明度等。

      animation package 包中包含了tween animation所有的类。

      一系列的动画命令定义了一个完整的tween animation,可以用代码定义也可以用XML资源文件定义。

    XML资源文件

      XML资源文件的使用可以见:Animation Resources

      XML文件放在项目的res/anim/目录下。文件必须有一个唯一的根节点。

      这个根节点可以是:<alpha>, <scale>, <translate>, <rotate>, interpolator element, 或者是<set>。

      默认情况下,所有的动画都是并行进行的,要想使得它们顺寻发生,你必须指定startOffset属性。

      有一些值,可以指定是相对于View本身还是相对于父类容器的。

      比如pivotX,要表示相对于自身的50%,要用50%;要表示相对于父类容器的50%,则直接写50

     

    使用例子

      XML文件存储为:res/anim/hyperspace_jump.xml:

    复制代码
    <set xmlns:android="http://schemas.android.com/apk/res/android"
        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/accelerate_interpolator"
            android:startOffset="700">
            <scale
                android:fromXScale="1.4"
                android:toXScale="0.0"
                android:fromYScale="0.6"
                android:toYScale="0.0"
                android:pivotX="50%"
                android:pivotY="50%"
                android:duration="400" />
            <rotate
                android:fromDegrees="0"
                android:toDegrees="-45"
                android:toYScale="0.0"
                android:pivotX="50%"
                android:pivotY="50%"
                android:duration="400" />
        </set>
    </set>
    复制代码

      在代码中把这个动画应用于一个ImageView:

    ImageView image = (ImageView) findViewById(R.id.image);
    Animation hyperspaceJump = AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump);
    image.startAnimation(hyperspaceJump);
     

      除了调用 startAnimation() ,另一种处理方式是通过Animation.setStartTime()方法定义一个开始时间,然后通过View.setAnimation()方法把这个动画赋给控件即可。

    View Animation和Property Animation

      View Animation是API Level 1就引入的。

      View Animation在包android.view.animation中。

      动画类叫Animation

     

      Property Animation是API Level 11引入的,即Android 3.0才开始有Property Animation相关的API。

      Property Animation API在包 android.animation中。

      动画相关类叫Animator

    参考资料

      API Guides:View Animation

      http://developer.android.com/guide/topics/graphics/view-animation.html

      Tween animation的包:

      http://developer.android.com/reference/android/view/animation/package-summary.html

      Animation类:

      http://developer.android.com/reference/android/view/animation/Animation.html

      Animation Resources

      http://developer.android.com/guide/topics/resources/animation-resource.html

  • 相关阅读:
    Java自学二十六天
    Java自学三十四天
    Java自学三十二天
    Java自学三十天
    Java开学考试心得
    Java自学二十九天
    Java自学二十八天
    Java自学三十一天
    Java自学三十三天
    2020年月12日Java学习日记
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/4384215.html
Copyright © 2011-2022 走看看