[转帖]Android中TweenAnimation四种动画切换效果_Android, Python及开发编程讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Android, Python及开发编程讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 3385 | 回复: 0   主题: [转帖]Android中TweenAnimation四种动画切换效果        下一篇 
周逸涵
注册用户
等级:少校
经验:871
发帖:83
精华:0
注册:2013-7-8
状态:离线
发送短消息息给周逸涵 加好友    发送短消息息给周逸涵 发消息
发表于: IP:您无权察看 2013-7-9 9:38:26 | [全部帖] [楼主帖] 楼主

点击每个按钮都会有对应的动画显示

北京联动北方科技有限公司

activity代码:

[java]view plaincopy

  1. package com.tmacsky; 
  2. import android.app.Activity; 
  3. import android.os.Bundle; 
  4. import android.util.Log; 
  5. import android.view.View; 
  6. import android.view.View.OnClickListener; 
  7. import android.view.animation.AlphaAnimation; 
  8. import android.view.animation.Animation; 
  9. import android.view.animation.AnimationSet; 
  10. import android.view.animation.AnimationUtils; 
  11. import android.view.animation.ScaleAnimation; 
  12. import android.view.animation.TranslateAnimation; 
  13. import android.view.animation.Animation.AnimationListener; 
  14. import android.view.animation.RotateAnimation; 
  15. import android.widget.Button; 
  16. import android.widget.ImageView; 
  17. public class AnimationDemoActivity extends Activity { 
  18.        private ImageView imageView; 
  19.        /** Called when the activity is first created. */ 
  20.        @Override 
  21.        public void onCreate(Bundle savedInstanceState) { 
  22.              super.onCreate(savedInstanceState); 
  23.              setContentView(R.layout.main); 
  24.              //定义四个动画的button属性 
  25.              imageView = (ImageView)findViewById(R.id.imageView); 
  26.              Button alpha = (Button)findViewById(R.id.Alpha); 
  27.              alpha.setOnClickListener(new AnimationClickListener(AnimationType.Alpha)); 
  28.              Button rotate = (Button)findViewById(R.id.Rotate); 
  29.              rotate.setOnClickListener(new AnimationClickListener(AnimationType.Rotate)); 
  30.              Button scale = (Button)findViewById(R.id.Scale); 
  31.              scale.setOnClickListener(new AnimationClickListener(AnimationType.Scale)); 
  32.              Button translate = (Button)findViewById(R.id.Translate); 
  33.              translate.setOnClickListener(new AnimationClickListener(AnimationType.Translate)); 
  34.              Button complex = (Button)findViewById(R.id.Complex); 
  35.              complex.setOnClickListener(new AnimationClickListener(AnimationType.Complex)); 
  36.        } 
  37.        //定义animationType属性 
  38.        enum AnimationType{ 
  39.              Alpha, 
  40.              Rotate, 
  41.              Scale, 
  42.              Translate, 
  43.              Complex 
  44.        } 
  45.        //定义一个函数 
  46.        class AnimationClickListener implements OnClickListener{ 
  47.              private AnimationType animationType; 
  48.              public AnimationClickListener(AnimationType animType){ 
  49.                    animationType = animType; 
  50.              } 
  51.              public void onClick(View v) { 
  52.                    // TODO Auto-generated method stub 
  53.                    switch (animationType) { 
  54.                          case Alpha: 
  55.                          //定义渐变动画,重复5次,持续1分钟 
  56.                          /*AlphaAnimation _animation = new AlphaAnimation(1f, 0.1f); 
  57.                          _animation.setDuration(3000); 
  58.                          _animation.setRepeatCount(5); 
  59.                          //设置循环 
  60.                          _animation.setRepeatMode(Animation.REVERSE); 
  61.                          _animation.setAnimationListener(new AnimationListener() { 
  62.                                //设置animation监听,依次是启动,重复,然后结束 
  63.                                public void onAnimationStart(Animation animation) { 
  64.                                      // TODO Auto-generated method stub 
  65.                                      Log.i("log", "animation Start"); 
  66.                                } 
  67.                                public void onAnimationRepeat(Animation animation) { 
  68.                                      // TODO Auto-generated method stub 
  69.                                      Log.i("log", "animation Repeat"); 
  70.                                } 
  71.                                public void onAnimationEnd(Animation animation) { 
  72.                                      // TODO Auto-generated method stub 
  73.                                      Log.i("log", "animation End"); 
  74.                                } 
  75.                          }); 
  76.                          //启动动画 
  77.                          imageView.startAnimation(_animation);*/ 
  78.                          AlphaAnimation alphaAnimation = (AlphaAnimation)AnimationUtils.loadAnimation(AnimationDemoActivity.this, R.anim.alpha); 
  79.                          imageView.startAnimation(alphaAnimation); 
  80.                          break; 
  81.                          case Rotate: 
  82.                          //定义旋转动画,旋转一周持续1分钟,重复三次,在物体的中心位置 
  83.                          RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 
  84.                          rotateAnimation.setDuration(3000); 
  85.                          rotateAnimation.setRepeatCount(3); 
  86.                          //启动动画 
  87.                          imageView.startAnimation(rotateAnimation); 
  88.                          break; 
  89.                          case Scale: 
  90.                          //定义缩放动画,从中心坐标开始,缩放1.5倍大小,持续1分钟,重复三次 
  91.                          ScaleAnimation scaleAnimation = new ScaleAnimation(1, 1.5f, 1, 1.5f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 
  92.                          scaleAnimation.setDuration(3000); 
  93.                          scaleAnimation.setRepeatCount(3); 
  94.                          //启动动画 
  95.                          imageView.startAnimation(scaleAnimation); 
  96.                          break; 
  97.                          case Translate: 
  98.                          //定义移动动画,都从自身坐标开始,移动2个位置,持续1分钟,重复三次 
  99.                          TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 2, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 2); 
  100.                          translateAnimation.setDuration(3000); 
  101.                          translateAnimation.setRepeatCount(3); 
  102.                          //启动动画 
  103.                          imageView.startAnimation(translateAnimation); 
  104.                          break; 
  105.                         
  106.                          case Complex: 
  107.                          //设置复杂的操作步骤,点击按钮complex后,会运行四种动画效果叠加 
  108.                          AnimationSet sets = new AnimationSet(false); 
  109.                          //定义渐变动画 
  110.                          AlphaAnimation _animation1 = new AlphaAnimation(1f, 0.1f); 
  111.                          _animation1.setDuration(3000); 
  112.                          //定义旋转动画,在物体的中心位置 
  113.                          RotateAnimation rotateAnimation1 = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 
  114.                          rotateAnimation1.setDuration(3000); 
  115.                          //定义缩放动画,从中心坐标开始,缩放1.5倍大小 
  116.                          ScaleAnimation scaleAnimation1 = new ScaleAnimation(1, 1.5f, 1, 1.5f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 
  117.                          scaleAnimation1.setDuration(3000); 
  118.                          //定义移动动画,都从自身坐标开始,移动2个位置 
  119.                          TranslateAnimation translateAnimation1 = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 2, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 2); 
  120.                          translateAnimation1.setDuration(3000); 
  121.                          //启动动画 
  122.                          sets.addAnimation(_animation1); 
  123.                          sets.addAnimation(rotateAnimation1); 
  124.                          sets.addAnimation(scaleAnimation1); 
  125.                          sets.addAnimation(translateAnimation1); 
  126.                          imageView.startAnimation(sets); 
  127.                          break; 
  128.                          default: 
  129.                          break; 
  130.                    } 
  131.              } 
  132.        } 



layout文件:

[html]view plaincopy

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.     <TextView  
  7.         android:layout_width="fill_parent"  
  8.         android:layout_height="wrap_content"  
  9.         android:text="@string/hello" />  
  10.     <ImageView  
  11.         android:id="@+id/imageView"  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:src="@drawable/qa" />  
  15.     <Button  
  16.         android:id="@+id/Alpha"  
  17.         android:layout_width="wrap_content"  
  18.         android:layout_height="wrap_content"  
  19.         android:text="Alpha" />  
  20.     <Button  
  21.         android:id="@+id/Rotate"  
  22.         android:layout_width="wrap_content"  
  23.         android:layout_height="wrap_content"  
  24.         android:text="Rotate" />  
  25.     <Button  
  26.         android:id="@+id/Scale"  
  27.         android:layout_width="wrap_content"  
  28.         android:layout_height="wrap_content"  
  29.         android:text="Scale" />  
  30.     <Button  
  31.         android:id="@+id/Translate"  
  32.         android:layout_width="wrap_content"  
  33.         android:layout_height="wrap_content"  
  34.         android:text="Translate" />  
  35.     <Button  
  36.         android:id="@+id/Complex"  
  37.         android:layout_width="wrap_content"  
  38.         android:layout_height="wrap_content"  
  39.         android:text="Complex" />  
  40. </LinearLayout>  



资源anim文件alpha.xml

[html]view plain




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论