2016-07-18 89 views
0

我试图构建一个包含倒计时计时器的Android应用程序,并且应该在每个间隔中显示三个图像以显示一段时间 - 屏蔽图像为200 ms,刺激图像为20 ms和另一个200毫秒的掩模图像。Android:强制UI刷新立即

该应用程序与两个线程一起工作 - 主UI线程和时间管理线程。

问题是,UI线程不会自行刷新,因为它睡在两者之间,因此不显示任何图像。

我已经搜索了很多小时已经找到一种方法,迫使UI线程立即刷新自己,但直到现在我不成功。 方法invalidate()或postinvalidate()例如不要做任何有用的事情。

如果有人对此问题有任何提示或解决方案,那将会很棒。

感谢您的帮助。

public class MainActivity extends AppCompatActivity implements View.OnClickListener { 

    Button button; 
    ImageView imageView; 
    TextView textViewCounter; 

    boolean buttonWasPressed = false; 
    double startTime; 
    double currentTime; 
    double timer; 
    final int INTERVALS = 2; 

    final double SECONDS_TO_NANOSECONDS_COEFFICIENT = 1000000000.0; 
    // length of the interval in seconds 
    final double INTERVAL_LENGTH = 10 * SECONDS_TO_NANOSECONDS_COEFFICIENT; 
    int intervalCounter = 0; 

    // masking time in milliseconds (200) 
    final double MASKING_TIME = 0.2 * SECONDS_TO_NANOSECONDS_COEFFICIENT; 
    // stimulus time in milliseconds (20) 
    final double STIMULUS_TIME = 0.02 * SECONDS_TO_NANOSECONDS_COEFFICIENT; 

    boolean stimuliShouldBeDisplayed = false; 
    boolean stimuliIsDisplayed = false; 
    boolean imageViewShouldBeCleared = false; 

    Handler handler; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     this.setContentView(R.layout.activity_main); 

     button = (Button) this.findViewById(R.id.button); 

     if (button != null) { 
      button.setOnClickListener(this); 
     } 

     imageView = (ImageView) this.findViewById(R.id.imageView); 
     textViewCounter = (TextView) this.findViewById(R.id.textViewCounter); 

     // messages are sent to the thread where the Handler was created 
     handler = new Handler() { 
      @Override 
      public void handleMessage(Message msg) { 

       // not sure if you must always clear the message queue 
       this.removeMessages(0); 

       double tempStartTime = System.nanoTime(); 

       // milliseconds are okay 
       textViewCounter.setText(String.valueOf(timer/1000000) + " ms"); 

       if (stimuliShouldBeDisplayed && !stimuliIsDisplayed) { 

        stimuliIsDisplayed = true; 

        // show mask 
        imageView.setImageResource(R.mipmap.mask); 

        try { 
         Thread.sleep((long) (MASKING_TIME/1000000)); 
        } catch (InterruptedException e) { 
         e.printStackTrace(

        // update our timer (milliseconds are okay) 
        timer += System.nanoTime() - tempStartTime; 
        textViewCounter.setText(String.valueOf(timer/1000000) + " ms"); 

        // show stimulus 
        imageView.setImageResource(R.mipmap.stimulus); 

        try { 
         Thread.sleep((long) (STIMULUS_TIME/1000000)); 
        } catch (InterruptedException e) { 
         e.printStackTrace(); 
        } 

        // update our timer (milliseconds are okay) 
        timer += System.nanoTime() - tempStartTime; 
        textViewCounter.setText(String.valueOf(timer/1000000) + " ms"); 

        // show mask 
        imageView.setImageResource(R.mipmap.mask); 

        try { 
         Thread.sleep((long) (MASKING_TIME/1000000)); 
        } catch (InterruptedException e) { 
         e.printStackTrace(); 
        } 

        // update our timer (milliseconds are okay) 
        timer += System.nanoTime() - tempStartTime; 
        textViewCounter.setText(String.valueOf(timer/1000000) + " ms"); 
      } 

       // clear the imageView 
       if (imageViewShouldBeCleared) { 
        imageView.setImageResource(0); 
        imageViewShouldBeCleared = false; 
        stimuliIsDisplayed = false; 
        stimuliShouldBeDisplayed = false; 
       } 
      } 
     }; 
    } 

    @Override 
    public void onClick(View v) { 

     if (v == button && !buttonWasPressed) { 

      buttonWasPressed = true; 

      // let's start our timer 
      startTime = System.nanoTime(); 

      Runnable runnableTimeManagement = new Runnable() { 
       @Override 
       public void run() { 

        while (currentTime - startTime <= INTERVAL_LENGTH && intervalCounter < INTERVALS) { 

         currentTime = System.nanoTime(); 

         timer = currentTime - startTime; 

         // next interval 
         if (timer > INTERVAL_LENGTH) { 

          intervalCounter++; 
          startTime = currentTime; 
          imageViewShouldBeCleared = true; 
         } 

         // 1 seconds extra for the communication time between TimeManagement Thread and GUI Thread 
         if (timer + SECONDS_TO_NANOSECONDS_COEFFICIENT >= INTERVAL_LENGTH - 2 * MASKING_TIME - STIMULUS_TIME) { 

          stimuliShouldBeDisplayed = true; 
         } 

         // we must always create a new empty message 
         Message message = Message.obtain(); 
         // we send message to the main UI thread 
         handler.sendMessage(message); 

         try { 
          Thread.sleep(5); 
         } catch (InterruptedException e) { 
          e.printStackTrace(); 
         } 
        } 

        // time is over 
        buttonWasPressed = false; 
        intervalCounter = 0; 
       } 
      }; 

      new Thread(runnableTimeManagement).start(); 
     } 
    } 
} 

有人知道一种方法来以另一种方式精确控制图像的显示时间吗?最好的解决方案是只显示一帧的刺激图像。但我不知道如何获得帧速率。

有没有可能强制UI线程立即刷新自己?

+1

鉴于大多数手机具有固定的刷新率 - 不确定如何实现20ms的刺激图像。请参阅:http://stackoverflow.com/questions/3966468/controlling-display-refresh-rate-on-android-device –

+0

据我所知,大多数手机刷新率为60赫兹,这将是1/60s = 16.7ms。 – MBot

+0

是的,所以您的图像显示为固定刷新率的一些倍数:16.7 ms或33.4 ms。如果这在可接受的范围内 - 那么确定它应该可以在Canvas或OpenGL ES中实现。 –

回答

0

Thread.sleep通常不是一个好主意。我不确定你需要一个辅助线程来描述你所描述的内容。你可以简单地使用Handler.sendMessageDelayed的UI更新在将来发生:

// will run as soon as possible (almost immediately) 
handler.sendMessage(handler.obtainMessage(0, R.mimap.mask, 0)); 
// will run in 200ms 
handler.sendMessageDelayed(handler.obtainMessage(0, R.mimap.stimulus, 0), 200); 
// will run in 220ms 
handler.sendMessageDelayed(handler.obtainMessage(0, R.mimap.mask, 0), 220); 

然后你的处理程序只显示它在参数消息接收到的图像:

@Override 
public void handleMessage(Message msg) { 
    imageView.setResource(msg.arg1); 
} 

但是请记住,你并不完全控制刷新率,所以这并不能保证图像显示精确到20ms。

+0

谢谢Xavier的帮助。不幸的是,图像显示精确到20毫秒很重要。 – MBot