安卓程序中的睡眠功能

2022-09-03 01:07:38

有一些问题让我的程序进入睡眠我试图做的是,当btnStart被按下冷杉随机设置图片到12个ImageButtons然后我希望它暂停5秒,然后将第一个ImageButton更改为另一张图片

我的代码看起来像这样,现在按下按钮时它会立即暂停...

       btnStart.setOnClickListener(new View.OnClickListener() {
        @Override

        public void onClick(View v) {
            // TODO Auto-generated method stub

            Collections.shuffle(pic);
            int time=1;
            press=true;

            pic.get(0).setImageDrawable(getResources().getDrawable(R.drawable.memgreen));
            pic.get(1).setImageDrawable(getResources().getDrawable(R.drawable.memgreen));
            pic.get(2).setImageDrawable(getResources().getDrawable(R.drawable.memred));
            pic.get(3).setImageDrawable(getResources().getDrawable(R.drawable.memred));
            pic.get(4).setImageDrawable(getResources().getDrawable(R.drawable.memblue));
            pic.get(5).setImageDrawable(getResources().getDrawable(R.drawable.memblue));
            pic.get(6).setImageDrawable(getResources().getDrawable(R.drawable.memwhite));
            pic.get(7).setImageDrawable(getResources().getDrawable(R.drawable.memwhite));
            pic.get(8).setImageDrawable(getResources().getDrawable(R.drawable.mempurple));
            pic.get(9).setImageDrawable(getResources().getDrawable(R.drawable.mempurple));
            pic.get(10).setImageDrawable(getResources().getDrawable(R.drawable.memyellow));
            pic.get(11).setImageDrawable(getResources().getDrawable(R.drawable.memyellow));

            try {
                Thread.sleep(5000);

                pic.get(0).setImageDrawable(getResources().getDrawable(R.drawable.coin));

            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
     }
  });

答案 1

好吧,如果您希望UI保持响应,则无法阻止UI线程。Thread.sleep

创建一个新线程,然后将其置于休眠状态。休眠后,运行用于更改 UI 线程上可绘制视图的方法

new Thread(new Runnable() {
    @Override
    public void run() {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                pic.get(0).setImageDrawable(getResources().getDrawable(R.drawable.coin));
            }
        });
    }
}).start();

答案 2

创建图像数组并设置处理程序。使用 postDelayed 不会阻塞线程。

            int[] imageArray = { R.drawable.img_1, R.drawable.img_2,
            R.drawable.img_3, R.drawable.img_4,
            R.drawable.img_5};

            Handler handler = new Handler();
            Runnable runnable = new Runnable() {
                int i = 0;

                public void run() {
                    imageView1.setImageResource(imageArray[i]);
                    i++;
                    if (i > imageArray.length - 1) {
                        i = 0;
                    }
                    handler.postDelayed(this, 5000); //displays every 5 seconds
                }
            };
            handler.postDelayed(runnable, 5000);

推荐