0

So I have a thread that is instantiated inside a view. The thread should call postInvalidate() to redraw the screen every 16 milliseconds (60fps). The screen never redraws itself, though. It just remains blank. What am I doing wrong? Code is below:

 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
    }
}

Thread:

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();
        }
    }
mstagg
  • 507
  • 4
  • 16

1 Answers1

1

You're missing a call to super.start() in the overriden start() method in GraphicsLoop, thus the thread is never started.

Nicklas Jensen
  • 1,424
  • 12
  • 19
  • This was it. Thanks alot! – mstagg Dec 13 '15 at 20:04
  • This may have solved your problem but I presume you have many other issues and the top most is you are using a WHILE loop, it increases your CPU utilization like anything. Read about why you not use WHILE loop for recurring tasks. You should infact use Java's build in support for these kind of recurring tasks - read this http://stackoverflow.com/questions/12908412/print-hello-world-every-x-seconds, answer by @Tim Bender – hagrawal7777 Dec 13 '15 at 20:09