[英]putting a pause in code to show image after image?
我是android的完整初学者。 我在提出申请时陷入了困境。 我将图像存储在hashmap中,将输入作为输入的行根据空间分成单独的单词,并提取其对应的图像。 但是我不希望这些图像立即显示,但是它们应该一个接一个地显示,并且每个图像之间应该有将近一秒钟的停顿才能显示出来。 但是由于我的经验不足,我似乎陷入了困境。 在代码中我应该在哪里停顿? 因为当我在代码中的任何地方使用Thread.sleep
时,它只会在每次开始时暂停。
textlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
Speech.setText("You said " + matches_text.get(position));
selectedFromList = (matches_text.get(position));
String[] separated = selectedFromList.split(" ");
// ImageView iv;
final int[] imageViews = { R.id.imageView1,
R.id.imageView2, R.id.imageView3, R.id.imageView4,
R.id.imageView5, R.id.imageView6, R.id.imageView7,
R.id.imageView8, R.id.imageView9, R.id.imageView10,
R.id.imageView11, R.id.imageView12,
R.id.imageView13, R.id.imageView14,
R.id.imageView15, R.id.imageView16,
};
final_length = separated.length;
int b = 0;
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("apple",R.drawable.apple);
maps pol=new maps();
pol.map_A();
pol.map_B();
pol.map_C();
pol.map_D();
pol.map_E();
for (int i = 0; i < separated.length; i++) {
iv = (ImageView) findViewById(imageViews[i]);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
100, 100);
iv.setLayoutParams(layoutParams);
if(pol.map_a.containsKey(separated[i].toLowerCase())){
iv.setImageResource(pol.map_a.get(separated[i].toLowerCase()));
}
else if(pol.map_b.containsKey(separated[i].toLowerCase())){
iv.setImageResource(pol.map_b.get(separated[i].toLowerCase()));
}
else if(pol.map_c.containsKey(separated[i].toLowerCase())){
iv.setImageResource(pol.map_c.get(separated[i].toLowerCase()));
}
else if(pol.map_d.containsKey(separated[i].toLowerCase())){
iv.setImageResource(pol.map_d.get(separated[i].toLowerCase()));
}
else if(pol.map_e.containsKey(separated[i].toLowerCase())){
iv.setImageResource(pol.map_e.get(separated[i].toLowerCase()));
}
}}
如下更改您的for循环:
Handler handler1 = new Handler();
for (int i = 0; i < separated.length; i++) {
final int iDupe = i;
handler1.postDelayed(new Runnable() {
public void run() {
iv = (ImageView) findViewById(imageViews[iDupe]);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(100, 100);
iv.setLayoutParams(layoutParams);
if(pol.map_a.containsKey(separated[iDupe].toLowerCase())) {
iv.setImageResource(pol.map_a.get(separated[iDupe].toLowerCase()));
}
else if(pol.map_b.containsKey(separated[iDupe].toLowerCase())) {
iv.setImageResource(pol.map_b.get(separated[iDupe].toLowerCase()));
}
else if(pol.map_c.containsKey(separated[iDupe].toLowerCase())) {
iv.setImageResource(pol.map_c.get(separated[iDupe].toLowerCase()));
}
else if(pol.map_d.containsKey(separated[iDupe].toLowerCase())) {
iv.setImageResource(pol.map_d.get(separated[iDupe].toLowerCase()));
}
else if(pol.map_e.containsKey(separated[iDupe].toLowerCase())) {
iv.setImageResource(pol.map_e.get(separated[iDupe].toLowerCase()));
}
}
}, i * 1000);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.