zoukankan      html  css  js  c++  java
  • 【android】动画效果研究(View)【1】

        Android 平台提供了两类动画,一类是 Tween 动画,即通过对场景里的对象不断做图像变换 ( 平移、缩放、旋转 ) 产生动画效果;第二类是 Frame 动画,即顺序播放事先做好的图像,跟电影类似。本次讲解的是Tween动画。

    一、View

    (1)对于控件View,android自身提供了动画效果Animation

    参考文献:http://gundumw100.iteye.com/blog/850338

    http://blog.csdn.net/rhljiayou/article/details/7194483

    Animation是以XML格式定义的,定义好的XML文件存放在res\anim中。

    Tween Animation,其由4种类型:
        Alpha:渐变透明度动画效果
        Scale:渐变尺寸伸缩动画效果
        Translate:画面转换位置移动动画效果
        Rotate:画面转换位置移动动画效果

    在介绍以上4种类型前,先介绍Tween Animation共同的节点属性。

    属性[类型] 功能
    Duration[long] 属性为动画持续时间 时间以毫秒为单位
    fillAfter [boolean] 当设置为true ,该动画转化在动画结束后被应用
    fillBefore[boolean] 当设置为true ,该动画转化在动画开始前被应用
    interpolator 指定一个动画的插入器,有一些常见的插入器。accelerate_decelerate_interpolator:加速-减速 动画插入器;accelerate_interpolator:加速-动画插入器;decelerate_interpolator:减速- 动画插入器,其他的属于特定的动画效果
    repeatCount[int] 动画的重复次数
    repeatMode[String] 定义重复的行为 1:"restart" 2:"reverse"   eg: android:repeatMode="reverse"
    startOffset[long] 动画之间的时间间隔,从上次动画停多少时间开始执行下个动画
    zAdjustment[int]

    定义动画的Z Order的改变 0:保持Z Order不变,1:保持在最上层,-1:保持在最下层

    下面分别举例说明4种类型的动画的定义:

    a、alpha渐变透明度动画效果

    <alpha
    android:fromAlpha=”0.1″
    android:toAlpha=”1.0″
    android:duration=”3000″ />

    fromAlpha属性为动画起始时透明度
    toAlpha 属性为动画结束时透明度
    0.0表示完全透明,1.0表示完全不透明,以上值取0.0-1.0之间的float数据类型的数字。

    b、scale渐变尺寸伸缩动画效果

    <scale
    android:interpolator= “@android:anim/accelerate_decelerate_interpolator”
    android:fromXScale=”0.0″
    android:toXScale=”1.4″
    android:fromYScale=”0.0″
    android:toYScale=”1.4″
    android:pivotX=”50%”
    android:pivotY=”50%”
    android:fillAfter=”false”
    android:startOffset=“700”
    android:duration=”700″
    android:repeatCount=”10″ />

    fromXScale[float] fromYScale[float] 为动画起始时,X、Y坐标上的伸缩尺寸
    toXScale [float] toYScale[float] 为动画结束时,X、Y坐标上的伸缩尺寸
    0.0表示收缩到没有,1.0表示正常无伸缩,值小于1.0表示收缩,值大于1.0表示放大
    pivotX[float] pivotY[float] 为动画相对于物件的X、Y坐标的开始位置
    属性值说明:从0%-100%中取值,50%为物件的X或Y方向坐标上的中点位置

    c、translate画面转换位置移动动画效果

    <translate
    android:fromXDelta=”30″
    android:toXDelta=”-80″
    android:fromYDelta=”30″
    android:toYDelta=”300″
    android:duration=”2000″ />

    fromXDelta toXDelta 为动画、结束起始时 X坐标上的位置
    fromYDelta toYDelta 为动画、结束起始时 Y坐标上的位置

    d、rotate 画面转移旋转动画效果

    <rotate
    android:interpolator=”@android:anim/accelerate_decelerate_interpolator”
    android:fromDegrees=”0″
    android:toDegrees=”+350″
    android:pivotX=”50%”
    android:pivotY=”50%”
    android:duration=”3000″ />

    fromDegrees 为动画起始时物件的角度 说明
    toDegrees 属性为动画结束时物件旋转的角度 可以大于360度
    当角度为负数——表示逆时针旋转
    当角度为正数——表示顺时针旋转
    (负数from——to正数:顺时针旋转)
    (负数from——to负数:逆时针旋转)
    (正数from——to正数:顺时针旋转)
    (正数from——to负数:逆时针旋转)
    pivotX pivotY 为动画相对于物件的X、Y坐标的开始位
    说明:以上两个属性值 从0%-100%中取值,50%为物件的X或Y方向坐标上的中点位置

        按照上面的讲述学习完了Tween Animation的定义,对Tween Animation有了详细的了解,再去了解下Android SDK的animation package(android.view.animation),其提供了操作Tween Animation所有的类。

    下面举例说明Tween Animation如何使用

    Android SDK提供了2种方法:

    a、直接从XML资源中读取Animation

    下面给出一个完整的XML定义(SDK提供)
    <set android:shareInterpolator="false" xmlns:android="http://schemas.android.com/apk/res/android">
       <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资源中读取Animation,按照应用程序开发的过程,介绍整个使用的过程,如下:
    创建Android工程;导入一张图片资源;
    在res\layout\main.xml中添加一个 ImageView Widget;
    在res下创建新的文件夹且命名为:anim,并在此文件夹下面定义 Animation XML 文件;
    修改OnCreate()中的代码,显示动画资源;

    基本的调用xml方法如下:

    //main.xml中的ImageView
    ImageView spaceshipImage = (ImageView) findViewById(R.id.spaceshipImage);
    //加载动画
    Animation hyperspaceJumpAnimation =
    AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump);
    //使用ImageView显示动画
    spaceshipImage.startAnimation(hyperspaceJumpAnimation);

    至于其他的load动画的方法,请参考相关SDK

    b、使用Animation子类的构造函数来初始化Animation对象

    //在代码中定义 动画实例对象 
    
    private Animation myAnimation_Alpha; 
    
    private Animation myAnimation_Scale; 
    
    private Animation myAnimation_Translate; 
    
    private Animation myAnimation_Rotate; 
    
    //根据各自的构造方法来初始化一个实例对象 
    
    myAnimation_Alpha=new AlphaAnimation(0.1f, 1.0f); 
    
    myAnimation_Scale =new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f, 
    
    Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 
    
    myAnimation_Translate=new TranslateAnimation(30.0f, -80.0f, 30.0f, 300.0f); 
    
    myAnimation_Rotate=new RotateAnimation(0.0f, +350.0f,
    
    Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
    下面开始简要介绍怎样控制动画:

         对于控制动画显示,Android也提供了相应的接口。动画的进度使用 Interpolator 控制。Interpolator 定义了动画的变化速度,可以实现匀速、正加速、负加速、无规则变加速等。Interpolator 是基类,封装了所有 Interpolator 的共同方法,它只有一个方法,即 getInterpolation (float input),该方法 maps a point on the timeline to a multiplier to be applied to the transformations of an animation。Android 提供了几个 Interpolator 子类,实现了不同的速度曲线,如下:

    AccelerateDecelerateInterpolator 在动画开始与介绍的地方速率改变比较慢,在中间的时候加速
    AccelerateInterpolator 在动画开始的地方速率改变比较慢,然后开始加速
    CycleInterpolator 动画循环播放特定的次数,速率改变沿着正弦曲线
    DecelerateInterpolator 在动画开始的地方速率改变比较慢,然后开始减速
    LinearInterpolator 在动画的以均匀的速率改变

    对于 LinearInterpolator ,变化率是个常数,即 f (x) = x.
    public float getInterpolation(float input) {
    return input;
    }
    Interpolator其他的几个子类,也都是按照特定的算法,实现了对变化率。还可以定义自己的 Interpolator 子类,实现抛物线、自由落体等物理效果。

    接着简要介绍下动画的运行模式

    独占模式,即程序主线程进入一个循环,根据动画指令不断刷新屏幕,直到动画结束
    中断模式,即有单独一个线程对时间计数,每隔一定的时间向主线程发通知,主线程接到通知后更新屏幕

    运行模式是由UI代码自行控制的

    相关例子demo:myAnimation.zip

  • 相关阅读:
    SVN服务器搭建(一)
    排序算法二:冒泡排序
    【LeetCode】136. Single Number
    【LeetCode】217. Contains Duplicate
    【LeetCode】189. Rotate Array
    【LeetCode】122. Best Time to Buy and Sell Stock II
    【LeetCode】26. Remove Duplicates from Sorted Array
    【LeetCode】20. Valid Parentheses
    【LeetCode】680. Valid Palindrome II
    【LeetCode】345. Reverse Vowels of a String
  • 原文地址:https://www.cnblogs.com/Amandaliu/p/2325623.html
Copyright © 2011-2022 走看看