Android动画

一、背景

Android系统提供了很多丰富的API实现UI的2D和3D动画,最主要的可划分如下几类:

  • View Animation:  视图动画在古老的Android版本系统中已提供,只能被用来设置View的动画;
  • Drawable Animation: 即帧动画、Frame动画,用来一个一个的显示Drawable的Resources,就像幻灯片一样,一帧一帧的图片连接一起播放,形式完整的动态图;
  • Property Animation: 属性动画只对Android 3.0(API 11)以上版本有效,这种动画可以设置给任何Object,包括那些没有渲染到屏幕上的对象。

二、View Animation(视图动画)使用

1、视图动画,也即Tween(补间)动画,可以在一个视图容器内执行一系列的简单变换(渐变AlphaAnimation/缩放Scale/位移Translate/旋转Rotate)。补间动画通过XML或Android代码定义,建议使用XML定义,因为更具可读性和重用性。

  视图动在android.view.animation包下,具体类说明如下:

2、视图动画介绍


   1AlphaAnimation 渐变透明度


 <span style="font-size:14px;"><span style="white-space:pre">	</span>//初始化  
<span style="white-space:pre">	</span>Animation alphaAnimation = new AlphaAnimation(0.1f, 1.0f);  
<span style="white-space:pre">	</span>//设置动画时间            
<span style="white-space:pre">	</span>alphaAnimation.setDuration(3000);  
<span style="white-space:pre">	</span>this.startAnimation(alphaAnimation); </span>

       其中AlphaAnimation类第一个参数fromAlpha表示动画起始时的透明度, 第二个参数toAlpha表示动画结束时的透明度。setDuration用来设置动画持续时间。    

<span style="font-size:14px;"><span style="white-space:pre">	</span><alpha
<span style="white-space:pre">	</span>    android:fromAlpha=”0.1″
<span style="white-space:pre">	</span>    android:toAlpha=”1.0″
<span style="white-space:pre">	</span>    android:duration=”3000″ />    <!--0.0表示完全透明   1.0表示完全不透明--></span>

   2RotateAnimation 画面旋转


<span style="white-space:pre">	</span>Animation rotateAnimation = new RotateAnimation(0f, 360f);  
        rotateAnimation.setDuration(1000);  
        this.startAnimation(rotateAnimation); 
<p><span style="font-size:14px;"><span style="white-space:pre">	</span>第一个参数<span style="font-family: 'Times New Roman';">fromDegrees</span><span style="font-family: 宋体;">表示动画起始时的角度,第二个参数</span><span style="font-family: 'Times New Roman';">toDegrees</span><span style="font-family: 宋体;">表示动画结束时的角度。 </span></span></p><p><span style="font-size:14px;">     pivotX , pivotY 为动画相对于物件的<span style="font-family: 'Times New Roman';">X</span><span style="font-family: 宋体;">、</span><span style="font-family: 'Times New Roman';">Y</span><span style="font-family: 宋体;">坐标的开始位</span> 说明:以上两个属性值 从<span style="font-family: 'Times New Roman';">0%-100%</span><span style="font-family: 宋体;">中取值</span></span></p><p><span style="font-size:14px;">     50%<span style="font-family: 宋体;">为物件的</span><span style="font-family: 'Times New Roman';">X</span><span style="font-family: 宋体;">或</span><span style="font-family: 'Times New Roman';">Y</span><span style="font-family: 宋体;">方向坐标上的中点位置</span></span><span style="font-size:14px;"> </span></p>

   3ScaleAnimation 渐变尺寸缩放


<span style="white-space:pre">	</span>//初始化  
<span style="white-space:pre">	</span>Animation scaleAnimation = new ScaleAnimation(0.1f, 1.0f,0.1f,1.0f);  
<span style="white-space:pre">	</span>//设置动画时间  
<span style="white-space:pre">	</span>scaleAnimation.setDuration(500);  
        this.startAnimation(scaleAnimation); <span style="font-family: Arial, Helvetica, sans-serif;">	</span>
<span style="white-space:pre"></span><p><span style="font-size: 14px;"><span style="white-space:pre">	</span>第一个参数<span style="font-family: 'Times New Roman';">fromX ,</span><span style="font-family: 宋体;">第二个参数</span><span style="font-family: 'Times New Roman';">toX:</span><span style="font-family: 宋体;">分别是动画起始、结束时</span><span style="font-family: 'Times New Roman';">X</span><span style="font-family: 宋体;">坐标上的伸缩尺寸。</span></span></p><p><span style="font-size:14px;">     第三个参数<span style="font-family: 'Times New Roman';">fromY ,</span><span style="font-family: 宋体;">第四个参数</span><span style="font-family: 'Times New Roman';">toY:</span><span style="font-family: 宋体;">分别是动画起始、结束时</span><span style="font-family: 'Times New Roman';">Y</span><span style="font-family: 宋体;">坐标上的伸缩尺寸。</span></span></p>

   4TranslateAnimation 位置移动


<span style="white-space:pre">	</span>//初始化  
<span style="white-space:pre">	</span>Animation translateAnimation = new TranslateAnimation(0.1f, 100.0f,0.1f,100.0f);  
<span style="white-space:pre">	</span>//设置动画时间                
<span style="white-space:pre">	</span>translateAnimation.setDuration(1000);  
        this.startAnimation(translateAnimation); 

   第一个参数fromXDelta ,第二个参数toXDelta:分别是动画起始、结束时X坐标。
   第三个参数fromYDelta ,第四个参数toYDelta:分别是动画起始、结束时Y坐标。

<span style="white-space:pre">	</span><translate
<span style="white-space:pre">		</span>android:fromXDelta=”30″
<span style="white-space:pre">		</span>android:toXDelta=”-80″
<span style="white-space:pre">		</span>android:fromYDelta=”30″
<span style="white-space:pre">		</span>android:toYDelta=”300″
<span style="white-space:pre">		</span>android:duration=”2000″ />

5)新建一个动画XML文件,如何动态引用?

<span style="white-space:pre">	</span>ImageView imgView = $(R.id.imgView);
	Animation animation = AnimationUtils.loadAnimation(this, R.anim.anim);
  	imgView.startAnimation(animation);

3、如何拥有多个动画特效?

<span style="white-space:pre">	</span>//初始化 Translate动画  
<span style="white-space:pre">	</span>translateAnimation = new TranslateAnimation(0.1f, 100.0f,0.1f,100.0f);  
<span style="white-space:pre">	</span>//初始化 Alpha动画  
<span style="white-space:pre">	</span>alphaAnimation = new AlphaAnimation(0.1f, 1.0f);  
  
<span style="white-space:pre">	</span>//动画集  
<span style="white-space:pre">	</span>AnimationSet set = new AnimationSet(true);  
<span style="white-space:pre">	</span>set.addAnimation(translateAnimation);  
<span style="white-space:pre">	</span>set.addAnimation(alphaAnimation);  
  
<span style="white-space:pre">	</span>//设置动画时间 (作用到每个动画)  
<span style="white-space:pre">	</span>set.setDuration(1000);  
<span style="white-space:pre">	</span>this.startAnimation(set);  

4、Animationxml定义中的android:interpolator属性

   zoomin.xml  代码:

<span style="white-space:pre">	</span><?xml  version="1.0"  encoding="utf-8"?>     
<span style="white-space:pre">	</span><set   xmlns:Android="http://schemas.android.com/apk/res/android"   
          <span style="white-space:pre">	</span>android:interpolator="@android:anim/decelerate_interpolator"> 
   <span style="white-space:pre">		</span><scale  Android:fromXScale="2.0"  android:toXScale="1.0"     
<span style="white-space:pre">			</span>Android:fromYScale="2.0"  android:toYScale="1.0"     
<span style="white-space:pre">			</span>Android:pivotX="50%p"  android:pivotY="50%p"     
<span style="white-space:pre">			</span>Android:duration="@android:integer/config_mediumAnimTime"  /> 
<span style="white-space:pre">	</span></set>    

  interpolator定义一个动画的变化率(the rate of change)。这使得基本的动画效果(alpha, scale, translate, rotate)得以加速,减速,重复等,动画的进度使用 Interpolator 控制。  

AccelerateDecelerateInterpolator        在动画开始与介绍的地方速率改变比较慢,在中间的时侯加速

AccelerateInterpolator                          在动画开始的地方速率改变比较慢,然后开始加速

CycleInterpolator                                  动画循环播放特定的次数,速率改变沿着正弦曲线

DecelerateInterpolator                          在动画开始的地方速率改变比较慢,然后开始减速

LinearInterpolator                                  在动画的以均匀的速率改变

对于 LinearInterpolator ,变化率是个常数,即 f (x) = x。还可以定义自己的 Interpolator 子类,实现抛物线、自由落体等物理效果。


二、Frame动画  即顺序的播放事先做好的图像,与gif图片原理类似 

1.  res/drawable/ f目录下 spin_animation.xml 文件 :

<span style="white-space:pre">	</span><animation-list android:id="selected" android:oneshot="false">
     <span style="white-space:pre">		</span><item android:drawable="@drawable/wheel0" android:duration="50" />
     <span style="white-space:pre">		</span><item android:drawable="@drawable/wheel1" android:duration="50" />
     <span style="white-space:pre">		</span><item android:drawable="@drawable/wheel2" android:duration="50" />
     <span style="white-space:pre">		</span><item android:drawable="@drawable/wheel3" android:duration="50" />
  <span style="white-space:pre">	</span></animation-list>

2、如何使用?

 <span style="white-space:pre">	</span>ImageView img = (ImageView)findViewById(R.id.spinning_wheel_image);
<span style="white-space:pre">	</span>img.setBackgroundResource(R.drawable.spin_animation);
<span style="white-space:pre">	</span>AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground();
<span style="white-space:pre">	</span>frameAnimation.start()

更多动画类容详见:http://www.androidchina.net/3303.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值