在Canvas-多线程

时间:2015-05-29 18:39:30

标签: android eclipse multithreading

这是我的代码,当应用程序启动时,我只看到一个黑屏。 我希望应用程序添加时间计数器,但我认为我的多线程有一些问题。

        timeCounter.startcount();
        while(pressed!=true){
            if (!holder.getSurface().isValid()) 
                continue;
            Canvas c= holder.lockCanvas();
            c.drawBitmap(galaxy, 30, 0, null);
            c.drawBitmap(player, x-(player.getWidth()/2), y-(player.getHeight()/2), null);
            c.drawText(timeCounter.toString(), 30, 60, timerpaint);
            holder.unlockCanvasAndPost(c);
        }
    }

计时器类(我不确定这个线程是否正常):

public class TimeCounter
{
    private int smallestseconds=0;
    private int seconds=0;
    private int minute=0;
    public TimeCounter()
    {
        smallestseconds=0;
        seconds=0;
        minute=0;
    }
    public void startcount()
    {
        Thread t=new Thread();
        while(pressed!=true)
        {
            try{
                t.sleep(10);
            }catch(InterruptedException e){
                smallestseconds++;
                if(smallestseconds==100)
                {
                    smallestseconds=0;
                    seconds++;
                    if(seconds==60)
                    {
                    seconds=0;  
                    minute++;
                    }
                }
            }
        }
    }
    public String toString()
    {
        return(minute+":"+seconds+":"+smallestseconds);
    }
}

0 个答案:

没有答案