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


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

		ImageView Img = new ImageView(this);// 创建ImageView
		Img.setImageResource(R.drawable.Img);// 指定图片
		AlphaAnimation alphaInAnim = new AlphaAnimation(0.0f, 1.0f);// 创建淡入动画
		alphaInAnim.setDuration(1000);// 指定动画持续时间
		alphaInAnim.getFillAfter(true);// 指定动画结束后是否使用原图在结束位置填充,防闪烁
		trapAlphaInAnim.setAnimationListener(new AnimationListener()// 设置动画监听
		{
			@Override
			public void onAnimationStart(Animation animation)
			{
				// TODO Auto-generated method stub
			}
			@Override
			public void onAnimationEnd(Animation animation)
			{
				// TODO Auto-generated method stub
				long delay = m_rand.nextLong()%2000;// 随机等待时间
				Message message = new Message();// 创建消息对象
				message.what = MSG_TRAP_IN_ANIM_END;// 设置消息内容
				mHandler.sendMessageDelayed(message, delay);// 发送消息,启动淡出动画
			}

			@Override
			public 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()// 设置动画监听
		{
			@Override
			public void onAnimationStart(Animation animation)
			{
				// TODO Auto-generated method stub
			}

			@Override
			public void onAnimationEnd(Animation animation)
			{
				// TODO Auto-generated method stub
				long delay = m_rand.nextLong()%2000;// 随机等待时间
				Message message = new Message();// 创建消息对象
				message.what = MSG_TRAP_OUT_ANIM_END;// 设置消息内容
				mHandler.sendMessageDelayed(message, delay);// 发送消息,启动淡入动画
			}

			@Override
			public 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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GrimRaider

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值