【问题标题】:make pause between drawing two draws在画两次画之间暂停
【发布时间】:2014-01-31 12:15:37
【问题描述】:

我想在两次抽签之间暂停一下。我已经尝试过Thread.sleep、处理程序、asyncTask 并得到了相同的结果 - 当活动启动时,我必须等待我设置的时间才能看到第一次抽奖,只有当我再次调用相同的方法(测试)时,我才看到第二次抽奖,而不是再次看到第一次抽奖。这是我的代码:

public void test(){
button.setClickable(false);
button.setBackgroundColor(Color.DKGRAY);
view.setFromAtoB(true);
view.invalidate();
AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
    @Override
    protected Void doInBackground(Void... params) {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        view.setMoveAB(true);
        view.postInvalidate();
        button.setBackgroundColor(Color.GRAY);
        button.setClickable(true);           
    }
};
task.execute((Void[])null);

问题出在哪里?为什么我看不到某种和谐,第一次画,暂停,第二次画? :) 也许我已经阻止了 UI 线程。对于绘图,我使用画布。在onDraw 方法中我做了一些计算并调用drawRodsAndDiscs 方法:

private void drawRodsAndDiscs(Canvas canvas){
    Paint paint = new Paint();
    drawRods(canvas);
    paint.setColor(Color.GREEN);
    paint.setStyle(Paint.Style.STROKE);
    for (Rect disc : discs) {
        canvas.drawRect(disc, paint);
    } 
}

【问题讨论】:

    标签: android multithreading drawing android-canvas draw


    【解决方案1】:

    尝试使用简单的CountDownTimer 而不是 Thread.sleep(int miliseconds);

    参考this

    【讨论】:

      【解决方案2】:

      我会使用此代码来解决您的问题。 当计时器完成时,它会自动重新启动。 试试这个:

      private boolean running = false;
      private Handler handler;
      
      public void onCreate(Bundle savedInstanceState) {
          handler = new Handler(this.getMainLooper()); //Run it in MainLooper
          this.handler.postDelayed(this.counterThread, 200); //Start timer in 200ms
      }
      
      private Thread counterThread = new Thread() {
      public void run() {
              if (isRunning()) {
                  return;
              }
              setRunning(true);
      
      // 10minute until finish, 200ms between ticks
              CountDownTimer ct = new CountDownTimer(10 * 60 * 1000, 200) {
      
                  public void onFinish() {
                      setRunning(false);
                  }
      
                  public void onTick(long time) {
                          //Do your shitznaz
                  }
              };
              ct.start();
          }
      };
      
      protected boolean isRunning() {
          return this.running;
      }
      protected void setRunning(boolean b) {
          this.running = b;
          if (!b) {
              // Reset timer
              this.handler.postDelayed(this.counterThread, 200); //Restarts the timer in 200ms
          }
      }
      

      【讨论】:

        【解决方案3】:

        对于简单的一次性延迟,您可以改用Handler

                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {                
                    @Override
                    public void run() {
                        view.setMoveAB(true);
                        view.invalidate();
                        button.setBackgroundColor(Color.GRAY);
                        button.setClickable(true); 
                    }
                }, 2000);
        

        【讨论】:

          猜你喜欢
          • 2012-07-26
          • 2011-10-22
          • 1970-01-01
          • 2018-12-22
          • 1970-01-01
          • 1970-01-01
          • 2016-03-28
          • 2013-11-25
          • 2012-08-21
          相关资源
          最近更新 更多