Android --- Tween动画示例


1、在res/anim目录下新建XML文件:tween_anim.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:Android="http://schemas.android.com/apk/res/android">  
  3.     <alpha  
  4.         android:fromAlpha="0.2"  
  5.         android:toAlpha="1.0"  
  6.         android:duration="3000"  
  7.         android:repeatMode="reverse"  
  8.         android:repeatCount="10" />  
  9.     <scale  
  10.         android:fromXScale="0.2"  
  11.         android:toXScale="1.0"  
  12.         android:fromYScale="0.2"  
  13.         android:toYScale="1.0"  
  14.         android:pivotX="50%"  
  15.         android:pivotY="50%"  
  16.         android:duration="3000"  
  17.         android:repeatMode="reverse"  
  18.         android:repeatCount="10" />  
  19.     <translate  
  20.         android:fromXDelta="50"  
  21.         android:toXDelta="100"  
  22.         android:fromYDelta="50"  
  23.         android:toYDelta="100"  
  24.         android:duration="3000"  
  25.         android:repeatMode="restart"  
  26.         android:repeatCount="10" />  
  27.     <rotate  
  28.         android:fromDegrees="0"  
  29.         android:toDegrees="360"  
  30.         android:duration="3000"  
  31.         android:repeatMode="restart"  
  32.         android:repeatCount="10" />  
  33. </set>  
2、在res/layout目录下新建XML文件:tween_anim_layout.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:orientation="vertical"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent">  
  7.     <ImageView  
  8.         android:id="@+id/imgTween"  
  9.         android:src="@drawable/c01"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_weight="1.0" />  
  13.     <Button  
  14.         android:id="@+id/btnControl"  
  15.         android:text="开始"  
  16.         android:layout_width="match_parent"  
  17.         android:layout_height="wrap_content" />  
  18.   
  19. </LinearLayout>  
3、Activity里面添加代码:
  1. package com.bison;  
  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 TweenAnimationDemo extends Activity {  
  13.     // 声明一个开始停止的标识符   
  14.     private boolean flags = true;  
  15.     private ImageView imgTween;  
  16.     private Button btnCtrl;  
  17.   
  18.     /** 初始化 */  
  19.     public void init() {  
  20.         imgTween = (ImageView) findViewById(R.id.imgTween);  
  21.         // 声明Tween动画   
  22.         final Animation anim = AnimationUtils.loadAnimation(this,  
  23.                 R.anim.tween_anim);  
  24.   
  25.         btnCtrl = (Button) findViewById(R.id.btnControl);  
  26.         btnCtrl.setOnClickListener(new OnClickListener() {  
  27.   
  28.             public void onClick(View v) {  
  29.                 if (flags) {  
  30.                     btnCtrl.setText("停止");  
  31.                     imgTween.startAnimation(anim);  
  32.                     flags = false;  
  33.                 } else {  
  34.                     btnCtrl.setText("开始");  
  35.                     imgTween.clearAnimation();  
  36.                     flags = true;  
  37.                 }  
  38.   
  39.             }  
  40.         });  
  41.   
  42.     }  
  43.   
  44.     @Override  
  45.     protected void onCreate(Bundle savedInstanceState) {  
  46.         super.onCreate(savedInstanceState);  
  47.         setContentView(R.layout.tween_anim_layout);  
  48.         init();  
  49.     }  
  50. }  
PS:Tween动画有alpha(透明)、scale(缩放)、translate(移动)、rotate(旋转),每个有不同的定义,仔细研究。
  • 1
  • 2
  • 下一页
【内容导航】
第1页:XML定义的动画 第2页:代码中定义的动画

相关内容