動畫迴圈: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