class MyThread extends Thread{ private SurfaceHolder holder; public boolean isRun ; float radius = 10f; Paint p; public MyThread(SurfaceHolder holder) { this.holder =holder; isRun = true; p = new Paint(); } public void run(){ int count = 0; while(isRun) { Canvas c = null; try{ synchronized (holder){ c = holder.lockCanvas(); c.drawColor(Color.BLACK); if(count % 2 == 0) p.setColor(Color.RED); else p.setColor(Color.YELLOW); Rect r = new Rect(100, 50, 300, 250); c.drawRect(r, p); c.drawText("这是第"+(count++)+"秒", 100, 310, p); Thread.sleep(1000); } }catch(Exception e){ e.printStackTrace(); }finally{ if(c!= null) holder.unlockCanvasAndPost(c); } } } }
REFERENES:http://www.myexception.cn/j2se/1623.html
http://www.myexception.cn/j2se/java.lang.IllegalThreadStateException.html
http://blog.sina.com.cn/s/blog_4550f3ca0101a9kz.html
http://www.eoeandroid.com/forum.php?mod=viewthread&tid=175140
http://ultrablog.sinaapp.com/printthread.php?t=26
http://blog.csdn.net/liuhanhan512/article/details/7575386