2016-07-18 132 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