我有一个绘图程序的骨架,使用一个线程:
@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 的另一个对象?