作者a1854 (朋友的朋友)
看板AndroidDev
標題Re: [問題] 想請問一下有關delay怎麼使用
時間Thu Aug 11 19:25:06 2011
※ 引述《tp6u045 (小黃~)》之銘言:
: 小弟我現在有4張圖片a1.a2.a3.a4
: 想要把第一張圖片顯示一秒後換第二張...
: 以此類推
: 做一種類似小動畫...
: 但是用Thread.sleep的話會變成整個程式Delay
: 有辦法解決嗎@@?
: 還是有其他可以做這種小動畫的方法
: 請各位大大幫忙了.....
想要做動畫可以直接用 Frame Animation
在 res/drawable/ 新增一 xml
如 img.xml:
<animation-list
xmlns:android="
http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/img0" android:duration="100" />
<item android:drawable="@drawable/img1" android:duration="100" />
<item ...>
</animation-list>
要用到動畫的地方用以下的 code:
ImageView image = (ImageView)row.findViewById( ... );
image.setImageBackgroundResource(R.drawable.img);
AnimationDrawable anim = (AnimationDrawable) image.getBackground();
anim.start();
或者可以用 Handler implement 你要的 delay
handler = new Handler();
handler.post(changeImg);
Runnable changeImg {
void run() {
image.setImageResource( ... );
handler.postDelayed(changeImg, 100);
}
}
如果要用 Thread.sleep()
要利用 Timer 或新增 Thread 去做
這樣才不會因為 UI Thread 停住了造成 AMR
要利用 Timer 或新 Thread update ImageView
也必須透過 Handler 來做
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.113.212.23
1F:推 tp6u045:恩恩感謝你唷=)非常仔細!!我會在TEST跟找資料研究一下 08/11 20:05
2F:推 tp6u045:剛剛測試出來可以了!但如果要好幾個動畫可以設定它的名稱 08/11 20:37
3F:→ tp6u045:來呼叫他嗎!? 08/11 20:37
4F:→ a1854:好幾個動畫就用不同的 xml 表示吧 08/11 22:39
5F:推 milochen:推 Handler 08/12 11:18