- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我做了一个扩展 SurfaceView
和 Thread
的例子,以便在 Android 的 Canvas 上绘制一些对象,但我面临两个问题。
当按下后退按钮时,应用程序关闭,然后显示“不幸的是,您的示例已停止”。
当将方向从“Portrait”更改为“Landscape”或 VS 时,应用程序无法调整新屏幕并且不更新绘图然后它崩溃并显示相同的消息“不幸的是,你的例子有停止了”。
请查看代码:
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO implement this method
holder.getSurface().setSize(width, height);
holder.setFormat(format);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// at this point the surface is created and
// we can safely start the game loop
thread.setRunning(true);
thread.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// tell the thread to shut down and wait for it to finish
// this is a clean shutdown
boolean retry = true;
while (retry) {
try {
thread.join();
retry = false;
} catch (InterruptedException e) {
// try again shutting down the thread
}
}
}
当我检查 logcat 时,我发现以下行是这部分的问题
@Override
public void run() {
Canvas canvas;
// initialise timing elements for stat gathering
initTimingElements();
long beginTime; // the time when the cycle begun
long timeDiff; // the time it took for the cycle to execute
int sleepTime; // ms to sleep (<0 if we're behind)
int framesSkipped; // number of frames being skipped
sleepTime = 0;
while (running) {
canvas = null;
// try locking the canvas for exclusive pixel editing
// in the surface
try {
canvas = this.surfaceHolder.lockCanvas();
LoggerHandler.log("canvas : " + canvas);
synchronized (surfaceHolder) {
beginTime = System.currentTimeMillis();
framesSkipped = 0; // resetting the frames skipped
// update game state
this.gamePanel.update();
// render state to the screen
// draws the canvas on the panel
this.gamePanel.render(canvas);
// calculate how long did the cycle take
timeDiff = System.currentTimeMillis() - beginTime;
// calculate sleep time
sleepTime = (int) (FRAME_PERIOD - timeDiff);
if (sleepTime > 0) {
// if sleepTime > 0 we're OK
try {
// send the thread to sleep for a short period
// very useful for battery saving
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
}
}
while (sleepTime < 0 && framesSkipped < MAX_FRAME_SKIPS) {
// we need to catch up
this.gamePanel.update(); // update without rendering
sleepTime += FRAME_PERIOD; // add frame period to check
// if in next frame
framesSkipped++;
}
// for statistics
framesSkippedPerStatCycle += framesSkipped;
// calling the routine to store the gathered statistics
storeStats();
}
} finally {
// in case of an exception the surface is not left in
// an inconsistent state
if (canvas != null) {
surfaceHolder.unlockCanvasAndPost(canvas);
}
} // end finally
}
}
这一行的问题:
canvas = this.surfaceHolder.lockCanvas();
改变方向返回null
。
最佳答案
我遇到了同样的问题。解决方案:只需检查 Canvas 是否为非空。在我的测试中:
try {
canvas = surfaceHolder.lockCanvas(null);
if(canvas != null){
synchronized (surfaceHolder) {
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(picture, 0, 0 , null);
}
}
}
finally {
if (canvas != null) {
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
关于android - SurfaceView 在 surfaceChange Android 上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14212165/
来自 the developer tutorial ,它写道:在对表面进行任何结构更改(格式或大小)后立即调用此方法。此时您应该更新表面中的图像。此方法总是至少调用一次,在 我不知道什么情况指的是结构
我有一个绘制在表面上的位图,每次旋转设备时都需要调整其大小。由于软按钮,我使用此代码获取当前尺寸(它似乎工作正常): public void getCanvasXY(){ int curren
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback { .......... ......
从方法 surfaceChanged() 调用 Camera.setParameters() 时出现 RuntimeException 错误。我看到其他帖子也有同样的问题,但没有找到明确的解决方案:
几天来我一直在尝试让我的项目运行但没有成功,这个网站上有类似的问题和答案,但没有帮助我解决我的问题,这就是为什么我用下面的完整代码问这个问题。 我正在使用 Android 相机 API 创建一个相机项
我做了一个扩展 SurfaceView 和 Thread 的例子,以便在 Android 的 Canvas 上绘制一些对象,但我面临两个问题。 当按下后退按钮时,应用程序关闭,然后显示“不幸的是,您的
我是一名优秀的程序员,十分优秀!