Android AlphaAnimation\TranslateAnimation 多組動畫迴圈+單個動畫結束後閃爍問題

來源:互聯網
上載者:User

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.