每1秒画一次

时间:2015-06-11 13:53:23

标签: java android

我正在尝试创建Android应用程序,时钟包含秒针,手每秒都在移动,我写的代码在几秒钟后崩溃,并且不起作用..任何想法?

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.os.Handler;
import android.view.View;
import android.widget.Toast;

class Circles extends View
{

public Circles(Context context)
  {
      super(context);    
  }

  protected void onDraw(final Canvas canvas) 
  {
      super.onDraw(canvas);
      //2 Circels
      Paint p1 = new Paint();
      p1.setColor(Color.BLUE);
      p1.setStyle(Style.STROKE);
       Paint p2 = new Paint();
      p2.setColor(Color.RED);
      p2.setStyle(Style.FILL);
      canvas.drawCircle(500, 250, 250, p1);
      canvas.drawCircle(500, 250, 20, p2);
      invalidate();

      // Seconds
      final Paint p3 = new Paint();
      p3.setColor(Color.RED);
      p3.setStyle(Style.FILL);

  Runnable seconds = new Runnable() {
        public void run() {

            canvas.drawLine(500, 250 , 500 , 400, p3 );
        }
    }; 
    ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
    executor.scheduleAtFixedRate(seconds, 0, 1, TimeUnit.SECONDS); 
  }


}

4 个答案:

答案 0 :(得分:3)

如果您想每秒重绘一次,请不要编写执行绘图代码的计时器。

显式调用onDraw是一个坏主意

而是编写一个每秒使画布无效的计时器。如果您使画布无效,它将自动重新绘制您已有的代码。使用您当前使用的Runnable方法可能是:

Runnable seconds = new Runnable() {
    public void run() {
        myView.postInvalidate();
    }
};

然而,处理程序可能是安排定期任务的更好方法。

确保您的绘图代码找到正确的绘图时间。

您也不想从onDraw中调用canvas.invalidate()。这将导致视图在完成绘制后再次重绘。这是导致崩溃的原因。即使你想要连续绘制,postInvalidate(0是一个更好的方法来调用。

答案 1 :(得分:2)

我想你可能想要使用Java的timer class

Timer timer = new Timer();

int interval = 1000; // One second

timer.scheduleAtFixedRate(new TimerTask() {
  @Override
  public void run() {
       place your code here that you'd like to run every second
  }
}, timeinterval);

答案 2 :(得分:1)

我认为应用程序崩溃因为你的方法canvas.drawLine调用递归onDraw。只是尝试从onDraw移动执行程序。

答案 3 :(得分:0)

你应该扩展Android的Thread类:

public class ClockThread extends Thread {
    private boolean running = false;
    private Circles circles;
    private long lastUpdate;

   public ClockThread (Circles circles) {
        this.circles = circles;
    }

    public void setRunning(boolean run) {
        running = run;
    }

    @Override
    public void run() {
        while (running) {

            // Compare current time to last time it was drawn
            if (System.currentTimeMillis() - lastUpdate > 1000) {

                // Create draw method in Circles to update properly
                circles.draw();
                lastUpdate = System.currentTimeMillis();
            }
        }
    }
}

然后,您可以在创建Circles对象后创建新的线程:

ClockThread clockThread = new ClockThread(circles);

当你想要开始时:

clockThread.setRunning(true);
clockThread.start();