我正在做一个游戏。我想创建一个计数器。它从200开始,然后1s减少大约0。我的代码:
public class GameView extends View{
private int count = 200;
private TimeCountThread timeCountThread;
public GameView(Context context, AttributeSet attrs) {
super(context, attrs);
timeCountThread = new TimeCountThread();
timeCountThread.start();
}
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
paint.setColor(Color.WHITE);
canvas.drawText("Time :"+count, 10, 35, paint);
}
public class TimeCountThread extends Thread{
public void run(){
while(count > 0){
try {
sleep(1000);
count--;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public boolean onTouchEvent(MotionEvent event){
if(event.getAction()==MotionEvent.ACTION_DOWN){
int x1=(int) event.getX();
int y1=(int) event.getY();
Toast.makeText(getContext(), "x1 = "+x1+", y1 ="+y1,1).show();
}
return true;
}
}
为什么柜台不活动。请帮帮我? 感谢
答案 0 :(得分:1)
我想创建一个计数器。它从200开始,然后1s减少大约0
您可以通过CountDownTimer
来实现答案 1 :(得分:0)
尝试使用其中包含runnable的处理程序。如:
Handler handler=new Handler();
Runnable r=new Runnable() {
public void run() {
count--;
if(count > 0) {
handler.postDelayed(this, 1000);
}
}
};
if(count > 0) {
handler.postDelayed(r, 1000);
}
你的
timeCountThread = new TimeCountThread();
timeCountThread.start();
是。另一个问题可能是你的计数是私人的,但我认为这不会产生那么大的差别。