這是一個基本應用程序,您可以在出現圖片時單擊拍攝按鈕。一張照片每5秒出現一次。但是,使用下面的代碼同時加載了所有圖像。我需要以設定的時間間隔逐個加載多個圖像
我打電話給loadImage加載圖像,然後shootHandler在5秒後將其刪除。我不確定是否需要使用AsyncTask或類似的東西。
void run() {
int i;
for(i = 0; i < imageList.length; i++) {
loadImage(i);
shootHandler(i);
}
}
private void shootHandler(final int trialNumber) {
loadImage(trialNumber);
handler.postDelayed(new Runnable() {
public void run() {
resetButtons(trialNumber);
}
}, 5000);
}
private void loadImage(int trialNumber) {
Glide.with(getApplicationContext()).load(R.drawable.red_btn).into(shoot);
int resID = getApplication().getResources().getIdentifier(imageList[trialNumber].name, "drawable", getApplicationContext().getPackageName());
Glide.with(getApplicationContext()).load(resID).into(imageList[trialNumber].image);
}
所有的圖像加載一次,因爲'的LoadImage(trialNumber);'是不在Handler中 –
將它放入處理程序中並不會產生影響,因爲postDelayed()不會阻塞調用線程。我只是試圖拖延圖像1秒,所有的圖像仍然同時出現。 – TraderJosh
你說你想要延遲,我不是在談論調用線程。你有一個沒有等待調用'loadImage'的for循環,因此「一次加載所有圖像」 –