canvas return 空 onResume 方法
canvas return null onResume method
我正在尝试开发游戏 app.I 实现 onResume 和 onPause 方法。 onPause 方法工作正常,但 onResume 方法崩溃。
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
resume();
}
public void resume()
{
addAnime();
myThread.setRunning(true);
myThread.start();
}
如果我没有在 onResume 方法中调用更新,myApp 工作正常,但如果我暂停游戏并再次恢复,模拟器说应用程序没有响应。
如果我在 onResume 方法中调用 resume,它会说 canvas is null at first runnig
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
view.resume();
}
我不明白为什么当我在 onResume.Someone 中调用 resume 时,应用程序开头的 canvas 为空,请解释一下?
编辑
暂停():
public void pause()
{
boolean repaet=true;
while(repaet)
{
try {
myThread.join();
myThread.setRunning(false);
repaet=false;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
myThread=null;
}
EDIT2
我的线程
public void run() {
long ticksPS = 1000 / FPS;
long startTime;
long sleepTime;
while (running) {
Canvas c = null;
startTime = System.currentTimeMillis();
try {
c = view.getHolder().lockCanvas();
synchronized (view.getHolder()) {
view.onDraw(c);
}
} finally {
if (c != null) {
view.getHolder().unlockCanvasAndPost(c);
}
}
sleepTime = ticksPS - (System.currentTimeMillis() - startTime);
try {
if (sleepTime > 0)
sleep(sleepTime);
else
sleep(10);
} catch (Exception e) {}
}
EDIT3
这是我设置的运行函数
public void setRunning(boolean run) {
running = run;
}
暂停方法:
public void pause()
{
boolean repeat = myThread != null;
while(repeat)
{
try {
myThread.setRunning(false);
myThread.join();
repeat=false;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
myThread=null;
}
我的线程:
public class gameThread extends Thread {
private SurfaceView view;
private boolean isRunning;
private static final int FPS; // set FPS with your value
private void sleep(long time) throws InterruptedException
{
Thread.sleep(time);
}
public void setRunning(boolean running)
{
synchronized(this)
{
this.isRunning = running;
}
}
public boolean isRunning()
{
synchronized(this)
{
return this.isRunning;
}
}
public void run()
{
long ticksPS = 1000 / FPS;
long startTime;
long sleepTime;
while (isRunning()) {
Canvas c = null;
startTime = System.currentTimeMillis();
try {
c = view.getHolder().lockCanvas();
synchronized (view.getHolder()) {
view.onDraw(c);
}
} finally {
if (c != null) {
view.getHolder().unlockCanvasAndPost(c);
}
}
sleepTime = ticksPS - (System.currentTimeMillis() - startTime);
try {
if (sleepTime > 0)
sleep(sleepTime);
else
sleep(10);
} catch (Exception e) {}
}
}
恢复方法:
public void resume()
{
addAnime();
// you must create a new thread. (You cannot use this same thread if it end)
myThread = new gameThread(surfaceView);
myThread.setRunning(true);
myThread.start();
}
您好,您可以在文档中找到更多信息。
SurfaceHolder.lockCanvas
Start editing the pixels in the surface. The returned Canvas can be used to draw into the surface's bitmap. A null is returned if the surface has not been created or otherwise cannot be edited. You will usually need to implement Callback.surfaceCreated to find out when the Surface is available for use.
The content of the Surface is never preserved between unlockCanvas() and lockCanvas(), for this reason, every pixel within the Surface area must be written. The only exception to this rule is when a dirty rectangle is specified, in which case, non-dirty pixels will be preserved.
If you call this repeatedly when the Surface is not ready (before Callback.surfaceCreated or after Callback.surfaceDestroyed), your calls will be throttled to a slow rate in order to avoid consuming CPU.
关于Threads或者Runneables的使用,可以查看这个。
https://guides.codepath.com/android/Repeating-Periodic-Tasks
http://developer.android.com/guide/components/processes-and-threads.html
我正在尝试开发游戏 app.I 实现 onResume 和 onPause 方法。 onPause 方法工作正常,但 onResume 方法崩溃。
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
resume();
}
public void resume()
{
addAnime();
myThread.setRunning(true);
myThread.start();
}
如果我没有在 onResume 方法中调用更新,myApp 工作正常,但如果我暂停游戏并再次恢复,模拟器说应用程序没有响应。
如果我在 onResume 方法中调用 resume,它会说 canvas is null at first runnig
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
view.resume();
}
我不明白为什么当我在 onResume.Someone 中调用 resume 时,应用程序开头的 canvas 为空,请解释一下?
编辑 暂停():
public void pause()
{
boolean repaet=true;
while(repaet)
{
try {
myThread.join();
myThread.setRunning(false);
repaet=false;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
myThread=null;
}
EDIT2 我的线程
public void run() {
long ticksPS = 1000 / FPS;
long startTime;
long sleepTime;
while (running) {
Canvas c = null;
startTime = System.currentTimeMillis();
try {
c = view.getHolder().lockCanvas();
synchronized (view.getHolder()) {
view.onDraw(c);
}
} finally {
if (c != null) {
view.getHolder().unlockCanvasAndPost(c);
}
}
sleepTime = ticksPS - (System.currentTimeMillis() - startTime);
try {
if (sleepTime > 0)
sleep(sleepTime);
else
sleep(10);
} catch (Exception e) {}
}
EDIT3 这是我设置的运行函数
public void setRunning(boolean run) {
running = run;
}
暂停方法:
public void pause()
{
boolean repeat = myThread != null;
while(repeat)
{
try {
myThread.setRunning(false);
myThread.join();
repeat=false;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
myThread=null;
}
我的线程:
public class gameThread extends Thread {
private SurfaceView view;
private boolean isRunning;
private static final int FPS; // set FPS with your value
private void sleep(long time) throws InterruptedException
{
Thread.sleep(time);
}
public void setRunning(boolean running)
{
synchronized(this)
{
this.isRunning = running;
}
}
public boolean isRunning()
{
synchronized(this)
{
return this.isRunning;
}
}
public void run()
{
long ticksPS = 1000 / FPS;
long startTime;
long sleepTime;
while (isRunning()) {
Canvas c = null;
startTime = System.currentTimeMillis();
try {
c = view.getHolder().lockCanvas();
synchronized (view.getHolder()) {
view.onDraw(c);
}
} finally {
if (c != null) {
view.getHolder().unlockCanvasAndPost(c);
}
}
sleepTime = ticksPS - (System.currentTimeMillis() - startTime);
try {
if (sleepTime > 0)
sleep(sleepTime);
else
sleep(10);
} catch (Exception e) {}
}
}
恢复方法:
public void resume()
{
addAnime();
// you must create a new thread. (You cannot use this same thread if it end)
myThread = new gameThread(surfaceView);
myThread.setRunning(true);
myThread.start();
}
您好,您可以在文档中找到更多信息。 SurfaceHolder.lockCanvas
Start editing the pixels in the surface. The returned Canvas can be used to draw into the surface's bitmap. A null is returned if the surface has not been created or otherwise cannot be edited. You will usually need to implement Callback.surfaceCreated to find out when the Surface is available for use. The content of the Surface is never preserved between unlockCanvas() and lockCanvas(), for this reason, every pixel within the Surface area must be written. The only exception to this rule is when a dirty rectangle is specified, in which case, non-dirty pixels will be preserved. If you call this repeatedly when the Surface is not ready (before Callback.surfaceCreated or after Callback.surfaceDestroyed), your calls will be throttled to a slow rate in order to avoid consuming CPU.
关于Threads或者Runneables的使用,可以查看这个。 https://guides.codepath.com/android/Repeating-Periodic-Tasks http://developer.android.com/guide/components/processes-and-threads.html