每隔X秒内线程无效

时间:2015-12-13 19:44:59

标签: java android multithreading

所以我有一个在视图中实例化的线程。线程应该调用postInvalidate()每16毫秒(60fps)重绘一次屏幕。不过,屏幕永远不会重绘。它只是空白。我究竟做错了什么?代码如下:

 public class DrawingView extends View {
    private Paint paint;
    private GraphicsLoop gThread;

    public DrawingView(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
        gThread = new GraphicsLoop();
        gThread.start();
    }

    @Override
    protected void onDraw(Canvas canvas) {
         // Draw Stuff
    }
}

主题:

public class GraphicsLoop extends Thread {
        private long frameTime;

        GraphicsLoop(){
        }

        @Override
        public void run() {
            while(true) {
                long frameTimeDelta = System.currentTimeMillis() - frameTime;
                if (frameTimeDelta > 16) {
                    frameTime = System.currentTimeMillis();
                    postInvalidate();
                }
            }
        }

        @Override
        public void start ()
        {
            frameTime = System.currentTimeMillis();
        }
    }

1 个答案:

答案 0 :(得分:1)

您在super.start()中的覆盖start()方法中错过了对GraphicsLoop的调用,因此该线程永远不会启动。