1
我一直在嘗試開始一個新項目,當我去啓動時,我無法獲得任何工作。我已經查看了過去幾個小時的代碼並使用了它,但我不記得最後一次發生這種情況的原因。Canvas not drawing Android
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;
}
}
我在裏面撒了一些破發點,並且這是從來沒有在CollisionTest調用surfaceCreated。我知道這是我在這裏失蹤的一些愚蠢的東西,所以如果你看到它,讓我知道。謝謝!
威廉
您的同步塊不可能幫助。你已經在調用lockCanvas() - 爲你做同步。順便說一句,surfaceview是雙緩衝的,所以你需要重畫每一幀的狀態以避免閃爍。另外,如果該線程被中斷,你應該跳出while循環。不要只是拋出該異常。休息一下;聲明。 –
好吧......我將不得不在它上面工作...這些都是我從其他工程中複製而來的。我只是試圖讓它再次繪製顏色,並且我知道線程在正確使用時能正常工作,但我無法弄清楚它爲什麼沒有被調用,我猜測我忘記了在surfaceview中有這麼一些東西 –