你好,游客 登录 注册 搜索
背景:
阅读新闻

Android:Animation的简单学习

[日期:2011-08-18] 来源:Linux社区  作者:huangbiao86 [字体:大 中 小]

如果我们要给每一个动画添加监听器:如下面代码 :

  1. public class MainActivity extends Activity {  
  2.     /** Called when the activity is first created. */  
  3.     private Button removeButton = null;  
  4.     private Button addButton = null;  
  5.     private ImageView imageView = null;  
  6.     private ViewGroup viewGroup = null;  
  7.     @Override  
  8.     public void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         setContentView(R.layout.main);  
  11.         removeButton = (Button)findViewById(R.id.removeButtonId);  
  12.         imageView = (ImageView)findViewById(R.id.imageViewId);  
  13.         removeButton.setOnClickListener(new RemoveButtonListener());  
  14.         viewGroup = (ViewGroup)findViewById(R.id.layoutId);//这里viewGroup是LinearLayout的id,这个viewGroup包括了下面所有的控件。   
  15.         addButton = (Button)findViewById(R.id.addButtonId);  
  16.         addButton.setOnClickListener(new AddButtonListener());  
  17.     }  
  18.     private class AddButtonListener implements OnClickListener{  
  19.         @Override  
  20.         public void onClick(View v) {  
  21.             //创建了一个淡入效果的Animation对象   
  22.             AlphaAnimation animation = new AlphaAnimation(0.0f,1.0f);  
  23.             animation.setDuration(1000);  
  24.             animation.setStartOffset(500);  
  25.             //创建一个新的ImageView   
  26.             ImageView imageViewAdd = new ImageView(MainActivity.this);  
  27.             imageViewAdd.setImageResource(R.drawable.icon);  
  28.             //将新的ImageView添加到viewGroup当中   
  29.             viewGroup.addView(imageViewAdd, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));  
  30.             //启动动画   
  31.             imageViewAdd.startAnimation(animation);  
  32.         }  
  33.           
  34.     }  
  35.       
  36.     private class RemoveButtonListener implements OnClickListener{  
  37.         @Override  
  38.         public void onClick(View v) {  
  39.             //创建一个淡出效果的Animation对象   
  40.             AlphaAnimation animation = new AlphaAnimation(1.0f,0.0f);  
  41.             //为Animation对象设置属性   
  42.             animation.setDuration(1000);  
  43.             animation.setStartOffset(500);  
  44.             //为Animation对象设置监听器   
  45.             animation.setAnimationListener(new RemoveAnimationListener());  
  46.             imageView.startAnimation(animation);  
  47.         }  
  48.     }  
  49.       
  50.     private class RemoveAnimationListener implements AnimationListener{  
  51.         //该方法在淡出效果执行结束之后被调用   
  52.         @Override  
  53.         public void onAnimationEnd(Animation animation) {  
  54.             System.out.println("end");  
  55.             //从viewGroup当中删除掉imageView控件   
  56.             viewGroup.removeView(imageView);  
  57.         }  
  58.        //该方法在动画重复执行的时候调用    
  59.         @Override  
  60.         public void onAnimationRepeat(Animation animation) {  
  61.             System.out.println("repeat");  
  62.         }  
  63.          //该方法在动画开始执行的时候,调用 。   
  64.         @Override  
  65.         public void onAnimationStart(Animation animation) {  
  66.             System.out.println("start");  
  67.         }  
  68.           
  69.     }  
  70. }  

在实际应用中,后者可能会多一些,但是由于写xml的时候,编译器很多地方不会报错,所以维护也是一个问题。

linux
相关资讯       Android开发 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款