动画循环: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