【问题标题】:Image toggle is not happening timely图像切换没有及时发生
【发布时间】:2018-03-12 12:07:19
【问题描述】:

我需要在 10 秒后将图像从红色切换为黄色,然后在单击按钮 3 秒后将其切换为绿色。
图像是“可绘制的”,三个图像存储在 imgesView1 数组中。

图像应该在 threadSleep 10 秒后将其状态更改为黄色,但是当与服务器的所有通信结束时,它会更改为黄色之后的绿色状态。

这是我的代码。

new SendCommandRequest().execute(parameterForURL);
   try{Thread.sleep(10000);}
   catch (InterruptedException ie){}
   imageView1.setImageResource(images1[1]);
     switch(result1) {
       case "1":
         new SendCommandRequest().execute(parameterForURL);
         imageView1.setImageResource(images1[1]);
         try{Thread.sleep(3000);}
         catch (InterruptedException ie){}
         imageView1.setImageResource(images1[1]);

只需单击一次按钮即可完成所有任务。

【问题讨论】:

    标签: java android


    【解决方案1】:

    尽量不要使用Thread.sleep,使用CountDownTimerRunnableHandler.postDelayedhttps://developer.android.com/reference/android/os/CountDownTimer.html how to use postDelayed() correctly in android studio?

    【讨论】:

    • 我需要将 UI 冻结特定秒。这就是为什么我不能使用倒数计时器或可运行的,因为两者都是并行处理任务。
    • 为什么需要冷冻?
    • 因为第一次访问服务器时,它会向微控制器发出执行某些任务的命令。但是微控制器会在 8 秒后做出响应。对于某些决定,我需要最后的回报。这就是问题
    • 如果是这样,请使用AsyncTask,它非常适合长时间的任务。
    • 实际上我正在使用 asyncTask 进行服务器通信。但我将网络连接时间设置为 3 秒。这是主要问题吗
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-29
    • 1970-01-01
    • 2021-11-24
    • 2013-05-09
    • 1970-01-01
    • 1970-01-01
    • 2012-07-15
    相关资源
    最近更新 更多