0

我有一个绘图程序的骨架,使用一个线程:

    @Override
public void surfaceCreated( SurfaceHolder holder) {
    pajeuThread = new PageThread( holder);
    pajeuThread.setRunning(true);
    Log.v(TAG, "surfaceCreated"  );
    pajeuThread.start();
} // end method surfaceCreated

然后我在页面中心附近写下“Hello World”,我还在页面左侧写了一个增加 30 的数字,如下所示:

public void dessineArbre( Canvas canvasse ) {
    compteur += 30;
    if ( compteur > 500 ) compteur = 30;
    screenWidth = getWidth();
    screenHeight = getHeight();
    Log.v(TAG, "dessineArbre"  );
    pagePaint.setColor( Color.WHITE);
    canvasse.drawRect(0, 0, screenWidth, screenHeight, pagePaint);
    pagePaint.setColor( Color.GREEN);
    canvasse.drawText( "Hello World", 200, 200, pagePaint);
    canvasse.drawText(Integer.toString( compteur ), 10, compteur, pagePaint);
} // end method dessineArbre

我想要使​​用第二个线程绘制的数字。

线程由内部类以这种方式处理:

        private class PageThread extends Thread {
    private SurfaceHolder surfaceHoldere;
    private boolean threadIsRunning = true;

    public PageThread( SurfaceHolder holdere){
        surfaceHoldere = holdere;
        setName( "page thread");
    } // end constructor

    public void setRunning( boolean runninge)   {
        threadIsRunning = runninge;
    } // end setRunning

    @Override
    public void run() {
        Canvas canvasvas = null;

        while( threadIsRunning ) {
            try {
                canvasvas = surfaceHoldere.lockCanvas(null);

                synchronized( surfaceHoldere) {
                    Log.v(TAG, "PageThread try synchronized"  );
                    dessineArbre( canvasvas);
                }// end synchronized
            } // end try
            finally {
                if( canvasvas != null)
                    Log.v(TAG, "Pagethread finally"  );
                    surfaceHoldere.unlockCanvasAndPost(canvasvas);
            } // end finally
        } // end while
    } // end method run
} // end class Pagethread

如何创建此类 Pagethread 的另一个对象?

4

1 回答 1

0

我找到了一种方法来创建第二个线程并让它休眠 1000 毫秒

    private PageThread pajeuThread;
private PageThread pajeuThread2;
    .
    .
@Override
public void surfaceCreated( SurfaceHolder holder) {
    pajeuThread = new PageThread( holder);
    pajeuThread.setRunning(true);
    pajeuThread2 = new PageThread( holder);
    pajeuThread2.setRunning(true);
    Log.v(TAG, "surfaceCreated"  );
    pajeuThread.start();
} // end method surfaceCreated

(我没有 start() pajeuThread2 但它仍然有效。)

public void dessineArbre( Canvas canvasse ) {
    screenWidth = getWidth();
    screenHeight = getHeight();
    Log.v(TAG, "dessineArbre"  );
    pagePaint.setColor( Color.WHITE);
    canvasse.drawRect(0, 0, screenWidth, screenHeight, pagePaint);
    pagePaint.setColor( Color.GREEN);
    canvasse.drawText( "Hello World", 200, 200, pagePaint);
} // end method dessineArbre

public void drawCounter( Canvas canvassed ) {
    compteur += 30;
    if ( compteur > 500 ) compteur = 30;
    Log.v(TAG, "drawCounter"  );
    pagePaint.setColor( Color.GREEN);
    canvassed.drawText(Integer.toString( compteur ), 10, compteur, pagePaint);
    try{
       pajeuThread2.sleep( 1000);
    }
    catch( InterruptedException e) {
        Log.v(TAG, "drawCounter exception"  );
    }

} // end method drawCounter 

我添加了对 drawCounter 方法的调用:

    private class PageThread extends Thread {
    private SurfaceHolder surfaceHoldere;
    private boolean threadIsRunning = true;

    public PageThread( SurfaceHolder holdere){
        surfaceHoldere = holdere;
        setName( "page thread");
    } // end constructor

    public void setRunning( boolean runninge)   {
        threadIsRunning = runninge;
    } // end setRunning

    @Override
    public void run() {
        Canvas canvasvas = null;

        while( threadIsRunning ) {
            try {
                canvasvas = surfaceHoldere.lockCanvas(null);

                synchronized( surfaceHoldere) {
                    Log.v(TAG, "PageThread try synchronized"  );
                    dessineArbre( canvasvas);
                    drawCounter( canvasvas );
                }// end synchronized
            } // end try
            finally {
                if( canvasvas != null)
                    Log.v(TAG, "Pagethread finally"  );
                    surfaceHoldere.unlockCanvasAndPost(canvasvas);
            } // end finally
        } // end while
    } // end method run
} // end class Pagethread

这工作正常,但我不确定这是否是管理线程的正确方法。

非常感谢您的帮助

于 2012-07-17T08:06:58.440 回答