Java 画布不绘制Android
我一直在尝试启动一个新项目,但当我开始启动时,我什么都做不到。在过去的几个小时里,我一直在看代码并使用它,但我不记得上次它发生时我是如何修复的Java 画布不绘制Android,java,android,android-canvas,Java,Android,Android Canvas,我一直在尝试启动一个新项目,但当我开始启动时,我什么都做不到。在过去的几个小时里,我一直在看代码并使用它,但我不记得上次它发生时我是如何修复的 package org.waldev.canvascollisiontest; import android.app.Activity; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import androi
package org.waldev.canvascollisiontest;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.Menu;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class CollisionTest extends Activity {
private Panel game;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
game = new Panel(this);
setContentView(game);
}
public class Panel extends SurfaceView implements SurfaceHolder.Callback{
Threads thread;
public Panel(Context context) {
super(context);
thread = new Threads(this.getHolder(), this);
setFocusable(true);
}
public void onDraw(Canvas c)
{
c.drawColor(Color.BLUE);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
//when the game starts, run the thread\
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
holder.setFormat(PixelFormat.RGB_565);
thread.setRunning(true);
thread.start();
}
//if its destroyed, then destroy the thread
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
boolean retry = true;
thread.setRunning(false);
while (retry) {
try {
thread.join();
// bitmaps.recycleAll();
retry = false;
} catch (InterruptedException e) {
// we will try it again and again...
}
}
}
}
}
而线程:
package org.waldev.canvascollisiontest;
import org.waldev.canvascollisiontest.CollisionTest.Panel;
import android.graphics.Canvas;
import android.view.SurfaceHolder;
public class Threads extends Thread{
SurfaceHolder surfaceHolder;
private Panel game;
private Canvas c;
private boolean isRunning = true;
// desired fps
private final static int MAX_FPS = 24;
// maximum number of frames to be skipped
private final static int MAX_FRAME_SKIPS = 0;
// the frame period
private final static int FRAME_PERIOD = 1000 / MAX_FPS;
public Threads(SurfaceHolder surfaceHolder, Panel panel){
this.surfaceHolder = surfaceHolder;
game = panel;
}
public void setRunning(boolean run){
isRunning = run;
}
public boolean getRunning(){
return isRunning();
}
public SurfaceHolder getSurface(){
return surfaceHolder;
}
@Override
public void run() {
Canvas canvas;
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
boolean updated = false;
sleepTime = 0;
while (isRunning) {
canvas = null;
try {
canvas = this.surfaceHolder.lockCanvas();
synchronized (surfaceHolder) {
beginTime = System.currentTimeMillis();
framesSkipped = 0;
if(canvas != null)
{
game.onDraw(canvas);
}
timeDiff = System.currentTimeMillis() - beginTime;
sleepTime = (int)(FRAME_PERIOD - timeDiff);
if (sleepTime > 0) {
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {}
}
else
{
updated = false;
}
}
} finally {
// in case of an exception the surface is not left in
// an inconsistent state
if (canvas != null) {
surfaceHolder.unlockCanvasAndPost(canvas);
}
} // end finally
}
}
public boolean isRunning() {
return isRunning;
}
}
package org.waldev.canvascollisiontest;
导入org.waldev.canvasclisiontest.CollisionTest.Panel;
导入android.graphics.Canvas;
导入android.view.SurfaceHolder;
公共类线程扩展线程{
浮雕浮雕;
私人小组游戏;
私人帆布c;
私有布尔值isRunning=true;
//期望fps
专用最终静态int MAX_FPS=24;
//要跳过的最大帧数
私有最终静态int MAX_FRAME_SKIPS=0;
//帧周期
专用最终静态整数帧周期=1000/最大帧/秒;
公共线程(表面文件夹表面文件夹面板){
this.surfaceHolder=surfaceHolder;
游戏=面板;
}
公共void setRunning(布尔运行){
isRunning=run;
}
公共布尔getRunning(){
返回isRunning();
}
公共SurfaceHolder getSurface(){
返回表面层;
}
@凌驾
公开募捐{
帆布;
long beginTime;//循环开始的时间
long-timeDiff;//循环执行所用的时间
int sleepTime;//ms to sleep(我让它工作了,忘了添加:
getHolder().addCallback(this);
到面板的构造函数,这是导致它无法工作的原因。谢谢!您的同步块可能没有帮助。您已经在调用lockCanvas()--这为您完成同步。顺便说一句,surfaceview是双缓冲的,所以您需要在每一帧上重新绘制状态以避免闪烁。此外,如果该线程被中断,您应该中断while循环。不要只是滚动该异常。使用break;语句。好的……我必须处理它……我复制了这两个从另一个有效的项目。我只是想让它再次绘制颜色,我知道线程在正确使用时可以工作,我只是不明白为什么它没有被调用,我猜我忘了在SurfaceViewSST中这样做。将你的答案标记为答案。