安卓四种补间动画

Android的动画分为两大类:补间动画,帧动画。

补间动画又分为四大类:移动补间动画,缩放补间动画,旋转补间动画,透明补间动画。


这四种补间动画都是Animation的子类。

移动补间动画:TranslateAnimation

eg:

Animation  animation = new TranslateAnimation(0,50,0,50);

参数1:x轴的起始位置

参数2:x轴的终止位置

参数3:  y轴的起始位置

参数4:y轴的终止位置

相对于原图位置的原点(图片的右上角为0,0),如果不想用这个点作为参照点,可以使用其他构造

TranslateAnimation(fromXType, fromXValue, toXType, toXValue, fromYType, fromYValue, toYType, toYValue)

参数1,参数3,参数5,参数7就是设置参照点的方式

可以通过Animation类的常量进行设置例如:Animation.RELATIVE_TO_SELF

缩放补间动画:ScaleAnimation

eg:

Animation   animation = new ScaleAnimation(1f,0.2f,1f,0.2f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f); 

参数1:x方向起始大小(1f表示原图大小)

参数2:x方向终止大小(0.2f表示原图的0.2倍)

参数3:y方向起始大小(1f表示原图大小)

参数4:y方向终止大小(0.2f表示原图的0.2倍)

参数5:缩放中心点x轴取值的参照方式

参数6:中心点x轴的取值(0.5f表示相对与原图的0.5倍)

参数7:缩放中心点y轴取值参照方式

参数8:中心点y轴的取值(0.5f表示相对与原图的0.5倍)

 旋转补间动画:RotateAnimation

eg:

 Animation animation  = new RotateAnimation(360,0,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f); 

参数1:旋转的起始角度

参数2:旋转的终止角度

参数3:旋转中心的x轴取值参照方式

参数4:中心点x轴的取值

参数5:旋转中心的y轴取值参照方式

参数6:中心点y轴的取值

透明补间动画: AlphaAnimation

eg:

Animation animation = new AlphaAnimation(1f,0.1f);

参数1: 起始透明度;

参数2: 目标透明度;

 

 

每种动画都有很多种重载,可以根据需求进行选择,如果想让动画有效果还得设置动画的时间

//设置动画持续时间
   animation.setDuration(2000);

以毫秒为单位

对于动画还可以设置渲染器

eg:

   //渲染器  android系统提供了很多渲染器资源 通过android.R.anim.的方式使用
   animation.setInterpolator(Main.this,android.R.anim.anticipate_overshoot_interpolator);

如果想要多个动画效果同时使用,可以通过AnimationSet 实现:

AnimationSet animationSet = new AnimationSet(false);
   animationSet.addAnimation(animation);

得到动画对象之后就是使用了,每个view都有startAnimation(animation)方法

因为AnimationSet 继承自Animation类所以该方法的参数既可以是动画对象(Animation)也可以是动画集(AnimationSet )对象

--------------------------------------------------分割线-------------------------------------------------------

有两种方式创建补间动画--采用布局文件方式和Java代码方式。下面分别给出两种方式创建动画的Demo

1.用布局文件创建:


a)在res/anim文件夹下面创建四个xml文件,分别对应平移、透明度、旋转、缩放四种补间动画

anim_alpha.xml

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"  
  3. android:fillEnabled="true"  
  4. android:fillAfter="true"  
  5.    >    
  6.     <alpha  
  7.         android:duration="2000"  
  8.         android:fromAlpha="1"  
  9.         android:repeatCount="1"  
  10.         android:repeatMode="reverse"  
  11.         android:toAlpha="0" />  
  12. </set>  

anim_rotate.xml

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.   
  4.     <rotate  
  5.         android:duration="2000"  
  6.         android:fromDegrees="0"  
  7.         android:interpolator="@android:anim/accelerate_interpolator"  
  8.         android:pivotX="50%"  
  9.         android:pivotY="50%"  
  10.         android:toDegrees="720" >  
  11.     </rotate>  
  12.   
  13.     <rotate  
  14.         android:duration="2000"  
  15.         android:fromDegrees="360"  
  16.         android:interpolator="@android:anim/accelerate_interpolator"  
  17.         android:pivotX="50%"  
  18.         android:pivotY="50%"  
  19.         android:startOffset="2000"  
  20.         android:toDegrees="0" >  
  21.     </rotate>  
  22.   
  23. </set>  

anim_scale.xml

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set  xmlns:android="http://schemas.android.com/apk/res/android">  
  3. <scale android:fromXScale="1"  
  4.     android:interpolator="@android:anim/decelerate_interpolator"  
  5.     android:fromYScale="1"  
  6.     android:toXScale="2.0"  
  7.     android:toYScale="2.0"  
  8.     android:pivotX="50%"  
  9.     android:pivotY="50%"  
  10.     android:fillAfter="true"  
  11.     android:repeatCount="1"  
  12.     android:repeatMode="reverse"  
  13.     android:duration="2000"/>  
  14. </set>  

anim_translate.xml

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">  
  3. <translate   
  4.     android:fromXDelta="0"  
  5.     android:toXDelta="860"  
  6.     android:fromYDelta="0"  
  7.     android:toYDelta="0"  
  8.     android:fillAfter="true"  
  9.     android:repeatMode="reverse"  
  10.     android:repeatCount="1"  
  11.     android:duration="2000">  
  12. </translate>   
  13. </set>  

java代码:

Animate2Activity.java

[java]  view plain copy
  1. package com.xzy.demo;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.View.OnClickListener;  
  7. import android.view.animation.Animation;  
  8. import android.view.animation.AnimationUtils;  
  9. import android.widget.Button;  
  10. import android.widget.ImageView;  
  11.   
  12. public class Animate2Activity extends Activity {  
  13.     private Button b1, b2, b3, b4;  
  14.     private boolean flag = true;  
  15.   
  16.     @Override  
  17.     protected void onCreate(Bundle savedInstanceState) {  
  18.         // TODO Auto-generated method stub  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.bujian);  
  21.         final Animation rotate = AnimationUtils.loadAnimation(this,  
  22.                 R.anim.anim_rotate);  
  23.         final Animation translate = AnimationUtils.loadAnimation(this,  
  24.                 R.anim.anim_translate);  
  25.         final Animation scale = AnimationUtils.loadAnimation(this,  
  26.                 R.anim.anim_scale);  
  27.         final Animation alpha = AnimationUtils.loadAnimation(this,  
  28.                 R.anim.anim_alpha);  
  29.         final ImageView iv = (ImageView) findViewById(R.id.imageview);  
  30.   
  31.         b1 = (Button) findViewById(R.id.btnAlpha);  
  32.   
  33.         b2 = (Button) findViewById(R.id.btnRotate);  
  34.   
  35.         b3 = (Button) findViewById(R.id.btnScale);  
  36.   
  37.         b4 = (Button) findViewById(R.id.btnTranslate);  
  38.   
  39.         b1.setOnClickListener(new OnClickListener() {  
  40.   
  41.             public void onClick(View v) {  
  42.                 // TODO Auto-generated method stub  
  43.                 if (flag) {  
  44.                     iv.startAnimation(alpha);  
  45.   
  46.                 }  
  47.             }  
  48.         });  
  49.         b2.setOnClickListener(new OnClickListener() {  
  50.   
  51.             public void onClick(View v) {  
  52.                 // TODO Auto-generated method stub  
  53.                 if (flag) {  
  54.                     iv.startAnimation(rotate);  
  55.   
  56.                 }  
  57.             }  
  58.         });  
  59.         b3.setOnClickListener(new OnClickListener() {  
  60.   
  61.             public void onClick(View v) {  
  62.                 // TODO Auto-generated method stub  
  63.                 if (flag) {  
  64.                     iv.startAnimation(scale);  
  65.   
  66.                 }  
  67.             }  
  68.         });  
  69.         b4.setOnClickListener(new OnClickListener() {  
  70.   
  71.             public void onClick(View v) {  
  72.                 // TODO Auto-generated method stub  
  73.                 if (flag) {  
  74.                     iv.startAnimation(translate);  
  75.   
  76.                 }  
  77.             }  
  78.         });  
  79.     }  
  80.   
  81. }  
效果图:



另一种方式是通过java代码实现,功能完全一样。代码就不赘述了。只给出demo就好了~~

补间动画java实现:http://pan.baidu.com/s/1oVH8J

补间动画xml布局文件实现:http://pan.baidu.com/s/1st3Vh

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值