基于时间的小问题>我想在活动中获得计时器我已经尝试过这段代码,但它运行得如此之快我得到“0”我想编程运行而没有睡眠。但计数从0到60秒
package com.okok;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Handler;
import android.os.SystemClock;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.SurfaceHolder.Callback;
import android.widget.Chronometer;
class GameView extends SurfaceView {
//private Bitmap bmp;
// Chronometer mChronometer;
private SurfaceHolder holder;
private GameLoopThread gameLoopThread;
private List<Sprite> sprites = new ArrayList<Sprite>();
private long lastClick;
private Bitmap bmpBlood;
Handler m_handler;
Runnable m_handlerTask ;
private List<TempSprite> temps = new ArrayList<TempSprite>();
public GameView(Context context) {
super(context);
gameLoopThread = new GameLoopThread(this);
holder = getHolder();
holder.addCallback(new Callback() {
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
createSprites();
gameLoopThread.setRunning(true);
gameLoopThread.start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
}
});
bmpBlood = BitmapFactory.decodeResource(getResources(), R.drawable.blast2);
}
private void createSprites() {
sprites.add(createSprite(R.drawable.greenenact));
// sprites.add(createSprite(R.drawable.greenenact));
// sprites.add(createSprite(R.drawable.greenenact));
}
private Sprite createSprite(int resouce) {
Bitmap bmp = BitmapFactory.decodeResource(getResources(), resouce);
return new Sprite(this, bmp);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.rgb(21, 181, 195));
for (int i = temps.size() - 1; i >= 0; i--) {
temps.get(i).onDraw(canvas);
}
final int count=0;
m_handler = new Handler();
m_handlerTask = new Runnable()
{
@Override
public void run() {
// do something
if(count=60)
{
m_handler.removeCallbacks(m_handlerTask);
}
count++;
m_handler.postDelayed(m_handlerTask, 5000);
for (Sprite sprite : sprites) {
sprite.onDraw(canvas);
}
}
};
m_handlerTask.run();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (System.currentTimeMillis() - lastClick > 500) {
lastClick = System.currentTimeMillis();
synchronized (getHolder()) {
float x = event.getX();
float y =event.getY();
for (int i = sprites.size() - 1; i >= 0; i--) {
Sprite sprite = sprites.get(i);
if (sprite.isCollition(x, y)) {
sprites.remove(sprite);
temps.add(new TempSprite(temps, this, x, y, bmpBlood));
break;
}
}
}
}
return true;
}
}
这是我的程序我只有一个错误 不能将count从int更改为布尔错误
我不想使用sleep方法,因为我的应用程序有睡眠方法影响我的动画
或者给我一个计数为0到60秒的程序,并显示哪些程序没有睡眠方法
答案 0 :(得分:0)
使用计时器代替。 Here is example, Example 2
答案 1 :(得分:0)
使用处理程序
Handler m_handler;
Runnable m_handlerTask ;
int count=0;
m_handler = new Handler();
m_handlerTask = new Runnable()
{
@Override
public void run() {
// do something
if(count=60)
{
m_handler.removeCallbacks(m_handlerTask);
}
count++:
m_handler.postDelayed(m_handlerTask, 1000);
// repeat some task every 1 second
}
};
m_handlerTask.run();