当前位置:操作系统 > 安卓/Android >>

Android Drawable系列——Animation Drawable(动画实现)

Android的动画实现是在Animation里面实现的,在Android里面,有两种Animation模式:
其中Tween Animation是通过对场景里的对象不断做图像变换(平移、缩放、旋转)产生动画效果,即是一种渐变动画;而Frame Animation:顺序播放事先做好的图像,是一种画面转换动画。
下面是一个Android Animation的配置文件例子。相对比较简单。但是基本方法都有用到:
[html]
<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:interpolator="@android:anim/linear_interpolator" 
    > 
    <!-- 定义缩放变换 --> 
    <scale  
        android:fromXScale="1.0" 
        android:toXScale="1.4" 
        android:fromYScale="1.0" 
        android:toYScale="0.6" 
        android:pivotX="50%" 
        android:pivotY="50%" 
        android:fillAfter="true" 
        android:duration="2000" 
        /> 
     
    <!-- 定义位移变化 --> 
    <translate  
        android:fromXDelta="10" 
        android:toXDelta="130" 
        android:fromYDelta="30" 
        android:toYDelta="-80" 
        android:duration="2000"          
        /> 
         
     
</set> 
在Set里面可以定义四种类型
alpha        渐变透明度动画效果
scale        渐变尺寸伸缩动画效果
translate  画面转换位置移动动画效果
rotate      画面转移旋转动画效果
当然,Android只要支持XML的肯定支持Java代码里面写,这四个类肯定是上面的类型加Animation(AlphaAnimation等),效果一样。个人偏好XML配置。直观。
下面是两种模式Animation的实现:
1.Tween Animation
一个tween动画就是变化一系列的动作的组合。如果你有一个EditText对象,你可以移动它,旋转它,让它变大或让它变小,如果文字下面还有背景图像,背景图像也会随着文件进行转换。
使用XML来定义Tween Animation
动画的XML文件在工程中res/anim目录,这个文件必须包含一个根元素,可以使上面的四个属性值进行描述。或者是把上面的元素都放入<set>元素组中,默认情况下,所以的动画指令都是同时发生的,但是,如果要让他们按照顺序发生,则必须设置startOffset。动画的指令定义了你想要发生什么样的转换,当他们发生了,应该执行多长时间,转换可以是连续的也可以使同时的。例如,你让文本内容从左边移动到右边,然后旋转180度,或者在移动的过程中同时旋转,没个转换需要设置一些特殊的参数(开始和结束的大小尺寸的大小变化,开始和结束的旋转角度等等,也可以设置些基本的参数(例如,开始时间与周期),如果让几个转换同时发生,可以给它们设置相同的开始时间,如果按序列的话,计算开始时间加上其周期。

<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>
Tween Animation如何使用
使用AnimationUtils类的静态方法loadAnimation()来加载XML中的动画XML文件
//main.xml中的ImageView
ImageView spaceshipImage = (ImageView) findViewById(R.id.spaceshipImage);
//加载动画
Animation hyperspaceJumpAnimation =AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump);
//使用ImageView显示动画
spaceshipImage.startAnimation(hyperspaceJumpAnimation);
如何在Java代码中定义动画
//在代码中定义 动画实例对象

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, +3

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,