【问题标题】:putting a pause in code to show image after image?在代码中暂停以显示图像后的图像?
【发布时间】:2015-08-05 14:07:17
【问题描述】:

我是一个完整的 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()));
                             }
}}

【问题讨论】:

  • 试试 Thread.sleep(1000);?
  • @Dev 不推荐或使用 Thread.sleep() 除非您在非 UI 线程上工作。在 UI 线程上使用 Thread.sleep() 会中断用户对应用的操作,最终会导致 ANR。

标签: java android


【解决方案1】:

改变你的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);
}

【讨论】:

  • 如果可行,请接受并投票。这将阻止更多的人花时间在这个问题上。谢谢
猜你喜欢
  • 1970-01-01
  • 2014-05-14
  • 2021-07-28
  • 2020-07-22
  • 2015-08-06
  • 1970-01-01
  • 2015-08-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多