canvas重启或调试后为黑色,而运行
canvas is black after restart or debugg while running
当我重新启动我的应用程序时,我的意思是当我按下 Homebutton
并从任务 window 启动它时,或者当我在它仍在运行时调试它时整个屏幕都变黑了!
这是我的 MainView:
package net.kex.toll;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
public class MainView extends SurfaceView implements SurfaceHolder.Callback{
public GameLoop gameLoop;
public Player player;
private int playerX = 50;
private int playerY = 50;
public MainView(final Context context) {
super(context);
getHolder().addCallback(this);
player = new Player();
this.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
playerX = playerX + 20;
playerY = playerY + 20;
player.play();
return false;
}
}
);
setFocusable(true);
}
public void surfaceChanged(SurfaceHolder surfaceHolder, int foramt, int width, int height){
}
public void surfaceDestroyed(SurfaceHolder surfaceHolder){
boolean retry = true;
while (true) {
try {
gameLoop.setRunning(false);
gameLoop.join();
} catch (Exception e) { e.printStackTrace();}
retry = false;
}
}
public void surfaceCreated(SurfaceHolder surfaceHolder){
gameLoop = new GameLoop(getHolder(), this);
gameLoop.setRunning(true);
gameLoop.start();
}
public void update(){
player.update();
}
public void draw(Canvas canvas){
if(canvas == null) {
return;
}
canvas.drawColor(Color.WHITE);
player.draw(canvas);
}
}
这是我的 GameLoop:
package net.kex.toll;
import android.graphics.Canvas;
import android.util.Log;
import android.view.SurfaceHolder;
public class GameLoop extends Thread {
public static final int MAX_FPS = 30;
private double averageFPS;
private MainView mainView;
private SurfaceHolder surfaceHolder;
private boolean running;
public static Canvas canvas;
public void setRunning(boolean running) {
this.running = running;
}
public GameLoop(SurfaceHolder surfaceHolder, MainView mainView) {
super();
this.surfaceHolder = surfaceHolder;
this.mainView = mainView;
}
@Override
public void run(){
long startTime;
long timeMillis = 1000/MAX_FPS;
long waitTime;
long 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.mainView.update();
this.mainView.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;
Log.i("GameLoop", "averageFPS: " + Double.toString(averageFPS));
}
}
}
}
我不知道该怎么办!如果你想要其他的class请告诉我,我会发给你!
请帮助我坐在这里并试图解决这个问题几个小时!
重新绘制对象,它会再次渲染它。例如在 onResume 添加 Repaint()
当我重新启动我的应用程序时,我的意思是当我按下 Homebutton
并从任务 window 启动它时,或者当我在它仍在运行时调试它时整个屏幕都变黑了!
这是我的 MainView:
package net.kex.toll;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
public class MainView extends SurfaceView implements SurfaceHolder.Callback{
public GameLoop gameLoop;
public Player player;
private int playerX = 50;
private int playerY = 50;
public MainView(final Context context) {
super(context);
getHolder().addCallback(this);
player = new Player();
this.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
playerX = playerX + 20;
playerY = playerY + 20;
player.play();
return false;
}
}
);
setFocusable(true);
}
public void surfaceChanged(SurfaceHolder surfaceHolder, int foramt, int width, int height){
}
public void surfaceDestroyed(SurfaceHolder surfaceHolder){
boolean retry = true;
while (true) {
try {
gameLoop.setRunning(false);
gameLoop.join();
} catch (Exception e) { e.printStackTrace();}
retry = false;
}
}
public void surfaceCreated(SurfaceHolder surfaceHolder){
gameLoop = new GameLoop(getHolder(), this);
gameLoop.setRunning(true);
gameLoop.start();
}
public void update(){
player.update();
}
public void draw(Canvas canvas){
if(canvas == null) {
return;
}
canvas.drawColor(Color.WHITE);
player.draw(canvas);
}
}
这是我的 GameLoop:
package net.kex.toll;
import android.graphics.Canvas;
import android.util.Log;
import android.view.SurfaceHolder;
public class GameLoop extends Thread {
public static final int MAX_FPS = 30;
private double averageFPS;
private MainView mainView;
private SurfaceHolder surfaceHolder;
private boolean running;
public static Canvas canvas;
public void setRunning(boolean running) {
this.running = running;
}
public GameLoop(SurfaceHolder surfaceHolder, MainView mainView) {
super();
this.surfaceHolder = surfaceHolder;
this.mainView = mainView;
}
@Override
public void run(){
long startTime;
long timeMillis = 1000/MAX_FPS;
long waitTime;
long 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.mainView.update();
this.mainView.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;
Log.i("GameLoop", "averageFPS: " + Double.toString(averageFPS));
}
}
}
}
我不知道该怎么办!如果你想要其他的class请告诉我,我会发给你!
请帮助我坐在这里并试图解决这个问题几个小时!
重新绘制对象,它会再次渲染它。例如在 onResume 添加 Repaint()