Android AlphaAnimation\TranslateAnimation 多组动画循环+单个动画结束后闪烁问题

来源:互联网 发布:悦读家园网络平台 编辑:程序博客网 时间:2024/05/28 15:06


动画循环:ImageView先淡入,等待随机时间,再淡出,等待随机时间后重复前面的操作。

ImageView Img = new ImageView(this);// 创建ImageViewImg.setImageResource(R.drawable.Img);// 指定图片AlphaAnimation alphaInAnim = new AlphaAnimation(0.0f, 1.0f);// 创建淡入动画alphaInAnim.setDuration(1000);// 指定动画持续时间alphaInAnim.getFillAfter(true);// 指定动画结束后是否使用原图在结束位置填充,防闪烁trapAlphaInAnim.setAnimationListener(new AnimationListener()// 设置动画监听{@Overridepublic void onAnimationStart(Animation animation){// TODO Auto-generated method stub}@Overridepublic void onAnimationEnd(Animation animation){// TODO Auto-generated method stublong delay = m_rand.nextLong()%2000;// 随机等待时间Message message = new Message();// 创建消息对象message.what = MSG_TRAP_IN_ANIM_END;// 设置消息内容mHandler.sendMessageDelayed(message, delay);// 发送消息,启动淡出动画}@Overridepublic void onAnimationRepeat(Animation animation){// TODO Auto-generated method stub}});AlphaAnimation alphaOutAnim = new AlphaAnimation(1.0f, 0.0f);// 创建淡出动画alphaOutAnim.setDuration(2000);// 指定动画持续时间alphaOutAnim.getFillAfter(true);// 指定动画结束后是否使用原图在结束位置填充,防闪烁alphaOutAnim.setAnimationListener(new AnimationListener()// 设置动画监听{@Overridepublic void onAnimationStart(Animation animation){// TODO Auto-generated method stub}@Overridepublic void onAnimationEnd(Animation animation){// TODO Auto-generated method stublong delay = m_rand.nextLong()%2000;// 随机等待时间Message message = new Message();// 创建消息对象message.what = MSG_TRAP_OUT_ANIM_END;// 设置消息内容mHandler.sendMessageDelayed(message, delay);// 发送消息,启动淡入动画}@Overridepublic void onAnimationRepeat(Animation animation){// TODO Auto-generated method stub}});Img.startAnimation(alphaInAnim);// 启动淡入动画public final static int MSG_TRAP_IN_ANIM_END = 9;// 消息:淡入动画结束public final static int MSG_TRAP_OUT_ANIM_END = MSG_TRAP_IN_ANIM_END+1;// 消息:淡出动画结束private Handler mHandler = new Handler()    {public void handleMessage(Message msg){    switch (msg.what)    {    case MSG_TRAP_IN_ANIM_END:    {    if (alphaOutAnim!=null && m_trapImg!=null)    {    alphaOutAnim.reset();    Img.startAnimation(alphaOutAnim);    }break;    }    case MSG_TRAP_OUT_ANIM_END:    {    if (alphaInAnim!=null && m_trapImg!=null)    {    alphaInAnim.reset();    Img.startAnimation(alphaInAnim);    }break;    }    }    super.handleMessage(msg);}    };

以上,无限循环组合动画。


防闪烁:

alphaInAnim.getFillAfter(true);// 指定动画结束后是否使用原图在结束位置填充,防闪烁

具体参考getFillAfter函数。


参考:(主要前3个)

http://fujian0910.blog.51cto.com/1706151/751552

http://hi.baidu.com/j_fo/blog/item/66b2700e1cfcedea7bcbe1a4.html

http://jakend.iteye.com/blog/1150065


http://www.cnblogs.com/feisky/archive/2010/01/11/1644482.html

http://topic.csdn.net/u/20110210/10/b6850b89-65eb-4669-bd3e-95b10fc184cc.html

http://topic.csdn.net/u/20120305/14/badd9a4c-fe23-486b-9582-362e2f180db9.html

http://lovehong0306.iteye.com/blog/1511089

http://blog.csdn.net/hellogv/article/details/6264706

原创粉丝点击