[英]Invalidate in Thread Every X Seconds
所以我有一个在视图内部实例化的线程。 该线程应调用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();
}
}
您在GraphicsLoop
的重写start()
方法中缺少对super.start()
的调用,因此该线程永远不会启动。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.