簡體   English   中英

每隔X秒使線程無效

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM