所以我有一个在视图内部实例化的线程。该线程应每16毫秒(60fps)调用postInvalidate()
重新绘制屏幕。屏幕永远不会重新绘制自己。它只是空白。我究竟做错了什么?代码如下:
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();
}
}
最佳答案
您缺少在super.start()
中被覆盖的start()
方法中对GraphicsLoop
的调用,因此该线程永远不会启动。