animation listerner 在动画开始、重复、结束后设置通知

Q群: 241359063 更精彩,欢迎共同走向创业学习之旅。
原创:kylin_zeng  http://blog.chinaunix.net/uid/23795897.html在此感谢mars 老师的帮助。转载请注明原创出处,尊重他人的劳动成果。

1、animationListener 监听器,它是在动画执行的各个阶段会得到通知,从而调用相应的方法:
  1.1 onAnimationEnd(Animation animation
)   //动画执行结束
  1.2
onAnimationRepeat(Animation animation)   //动画重新执行
  1.3
onAnimationStart(Animation animation)
   //动画开始

2、例如:

点击(此处)折叠或打开

  1. package mars.animation07;

  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.view.ViewGroup;
  6. import android.view.View.OnClickListener;
  7. import android.view.ViewGroup.LayoutParams;
  8. import android.view.animation.AlphaAnimation;
  9. import android.view.animation.Animation;
  10. import android.view.animation.Animation.AnimationListener;
  11. import android.widget.Button;
  12. import android.widget.ImageView;

  13. public class MainActivity extends Activity {
  14.     /** Called when the activity is first created. */
  15.     private Button removeButton = null;
  16.     private Button addButton = null;
  17.     private ImageView imageView = null;
  18.     private ViewGroup viewGroup = null;
  19.     @Override
  20.     public void onCreate(Bundle savedInstanceState) {
  21.         super.onCreate(savedInstanceState);
  22.         setContentView(R.layout.main);
  23.         removeButton = (Button)findViewById(R.id.removeButtonId);
  24.         imageView = (ImageView)findViewById(R.id.imageViewId);
  25.         removeButton.setOnClickListener(new RemoveButtonListener());
  26.         viewGroup = (ViewGroup)findViewById(R.id.layoutId);
  27.         addButton = (Button)findViewById(R.id.addButtonId);
  28.         addButton.setOnClickListener(new AddButtonListener());
  29.     }
  30.     private class AddButtonListener implements OnClickListener{
  31.         @Override
  32.         public void onClick(View v) {
  33.             //创建了一个淡入效果的Animation对象
  34.             AlphaAnimation animation = new AlphaAnimation(0.0f,1.0f);
  35.             animation.setDuration(1000);
  36.             animation.setStartOffset(500);
  37.             //创建一个新的ImageView
  38.             ImageView imageViewAdd = new ImageView(MainActivity.this);
  39.             imageViewAdd.setImageResource(R.drawable.icon);
  40.             //将新的ImageView添加到viewGroup当中
  41.             viewGroup.addView(imageViewAdd, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
  42.             //启动动画
  43.             imageViewAdd.startAnimation(animation);
  44.         }
  45.         
  46.     }
  47.     
  48.     private class RemoveButtonListener implements OnClickListener{

  49.         @Override
  50.         public void onClick(View v) {
  51.             //创建一个淡出效果的Animation对象
  52.             AlphaAnimation animation = new AlphaAnimation(1.0f,0.0f);
  53.             //为Animation对象设置属性
  54.             animation.setDuration(1000);
  55.             animation.setStartOffset(500);
  56.             //为Animation对象设置监听器
  57.             animation.setAnimationListener(new RemoveAnimationListener());
  58.             imageView.startAnimation(animation);
  59.         }
  60.     }
  61.     
  62.     private class RemoveAnimationListener implements AnimationListener{
  63.         //该方法在淡出效果执行结束之后被调用
  64.         @Override
  65.         public void onAnimationEnd(Animation animation) {
  66.             System.out.println("end");
  67.             //从viewGroup当中删除掉imageView控件
  68.             viewGroup.removeView(imageView);
  69.         }

  70.         @Override
  71.         public void onAnimationRepeat(Animation animation) {
  72.             System.out.println("repeat");
  73.         }

  74.         @Override
  75.         public void onAnimationStart(Animation animation) {
  76.             System.out.println("start");
  77.         }
  78.         
  79.     }
  80. }
17_animations07.rar

<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(122) | 评论(0) | 转发(0) |
0

上一篇:ramdisk.img

下一篇:将博客搬至CSDN

给主人留下些什么吧!~~
评论热议
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值