我想通過兩個靜態圖像做一個動態圖像,但是這個代碼只是一個靜態圖像的閃光,現在我想分別在4秒內閃爍兩個圖像。不要改變resoucre圖像
<ImageSwitcher
android:id="@+id/imageswitcherID"
<!-- insert another value to the view like layout width and height or margin -->
android:inAnimation="@anim/fade_in"
android:outAnimation="@anim/fade_out"
>
<ImageView
android:id="@+id/imageview1"
<!-- another value here -->
android:background="@drawable/your_drawable01"
/>
<ImageView
android:id="@+id/imageview2"
<!-- another value here -->
android:background="@drawable/your_drawable02"
/>
</ImageSwitcher>
,現在繼續您的活動,創建線程循環,持續4秒
int seconds = 0;
ImageSwitcher imgswitch;
...
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
imgswitch = (ImageSwitcher)findViewById(R.id.imageswitcherID);
SwitchingImages();
}
...
private void SwitchingImages(){
Thread SwImg = new thread(){
@Override
public void run(){
try{
while(seconds <= 4){
sleep(1000); //sleep for 1 seconds
runOnUiThread(new Runnable(){
@Override
public void run(){
imgswitch.showNext(); //will switch images every 1 seconds
if(seconds >= 5){
return; //stop the thread when 4 seconds elapsed
}
seconds += 1;
}
});
}
}catch(InterruptedException e){
e.printStackTrace();
}
}
};
SwImg.start();
}
你只是想讓圖像在4秒後或者每4秒重複一次? –
我只是想讓它切換一次4秒。但代碼只顯示一個圖像。我想分別在4秒後顯示2張圖像。 – giangdaughtry