我尝试运行一个项目,到目前为止,它也没有显示我想要的项目
在我的MainActivity类中
package com.tmd.game;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new GamePanel(this));
}
}
在我的GamePanel类中
package com.tmd.game;
import android.content.Context;
import android.graphics.Canvas;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class GamePanel extends SurfaceView implements SurfaceHolder.Callback {
private MainThread thread;
public GamePanel(Context context){
super(context);
thread = new MainThread(getHolder(), this);
setFocusable(true);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int
height){
}
@Override
public void surfaceCreated(SurfaceHolder holder){
thread = new MainThread(getHolder(), this);
thread.setRunning(true);
thread.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder){
boolean retry = true;
while(true){
try{
thread.setRunning(false);
thread.join();
}catch(Exception e) {e.printStackTrace();}
retry = false;
}
}
@Override
public boolean onTouchEvent(MotionEvent event){
return super.onTouchEvent(event);
}
public void update(){
}
@Override
public void draw(Canvas canvas){
super.draw(canvas);
}
}
在我的MainThread类中
package com.tmd.game;
import android.graphics.Canvas;
import android.view.SurfaceHolder;
public class MainThread extends Thread {
public static final int MAX_FPS = 30;
private double averageFPS;
private SurfaceHolder surfaceHolder;
private GamePanel gamePanel;
private boolean running;
public static Canvas canvas;
public void setRunning(boolean running){
this.running = running;
}
public MainThread(SurfaceHolder surfaceHolder, GamePanel gamePanel) {
super();
this.surfaceHolder = surfaceHolder;
this.gamePanel = gamePanel;
}
@Override
public void run() {
long startTime;
long timeMillis = 1000 / MAX_FPS;
long waitTime;
int frameCount = 0;
long totalTime = 0;
long targetTime = 1000 / MAX_FPS;
while (running) {
startTime = System.nanoTime();
canvas = null;
try {
canvas = this.surfaceHolder.lockCanvas();
synchronized (surfaceHolder) {
this.gamePanel.update();
this.gamePanel.draw(canvas);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (canvas != null) {
try {
surfaceHolder.unlockCanvasAndPost(canvas);
} catch (Exception e) { e.printStackTrace(); }
}
}
timeMillis = (System.nanoTime() - startTime) / 1000000;
waitTime = targetTime - timeMillis;
try{
if(waitTime > 0)
this.sleep(waitTime);
}catch(Exception e) {e.printStackTrace();}
totalTime += System.nanoTime() - startTime;
frameCount++;
if(frameCount == MAX_FPS){
averageFPS = 1000/((totalTime/frameCount)/1000000);
frameCount = 0;
totalTime = 0;
System.out.println(averageFPS);
}
}
}
}
因此,当我运行此代码时,没有收到任何错误消息,但奇怪的是,我似乎无法让System.out.println(averageFPS)显示平均值。
至少对我来说这很奇怪,但是如果有人可以帮助我,我真的很合适
(对不起,如果这个问题不是“符合标准”,我是刚开始在Stack Overflow上发布问题的人)