倒计时器麻烦android

时间:2013-02-07 01:15:43

标签: java android timer

我有一个问题。我有一个看起来像进度条向后移动的计时器。现在它运行良好,或者我应该在较旧的手机上说得好,但是较新的一款带有高清屏幕的手机效果不佳。计时器栏在我的xml中设置为vert和match_parent。 我在下面包括我的方法你能看一下并告诉我如何改进它吗?

//used to animate timer bar
private Handler mHandler = new Handler();

//timer event code
private Runnable mUpdateTimeTask = new Runnable() {
       public void run() {
           final long start = startTime; 
// startTime will come from a spinner it will be 0,1,2,3,4 or 5 (users choice) 
// ** the bar is GREEN until the if condition is meet
           long currentLength = System.currentTimeMillis() - start;
           long remainingTime = gameTime - currentLength;
           float x = (float)remainingTime / (float)gameTime;

           if(remainingTime > 0)
           {
               //still have time remaining - update UI
               LinearLayout timerUI = (LinearLayout) findViewById(R.id.timerUI);
               timerUI.getLayoutParams().height =  (int) (x * 400);

               //update color
               if(remainingTime < 15000)
                  timerUI.setBackgroundColor(Color.RED);
               else if(remainingTime < 30000)
                   timerUI.setBackgroundColor(Color.YELLOW);

               timerUI.requestLayout();
               timerUI.invalidate();
               mHandler.postDelayed(this, 500);
           }
           else
           {
               if(monitorThread != null)
                   monitorThread.interrupt();

                //NEED to push user to final screen
                Intent resultsIntent = new Intent(SingleGameActivity.this, 
                    ResultsActivity.class);
                startActivity(resultsIntent);
           }
       }
    };

0 个答案:

没有答案