我有一个绘图程序的骨架,使用一个线程:
@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的另一个对象?
我找到了一种方法来创建第二个线程,并使其睡眠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
(我没有启动()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
这很好,但我不确定这是否是管理线程的正确方式。
非常感谢您的帮助