android 画布锁,android - 当锁定的画布不为空时,Android SurfaceView显示空白 - 堆栈内存溢出...

我正在使用扩展SurfaceView的视图并使用线程在SurfaceView上绘制图像来创建一个简单的游戏。 游戏将具有自己的线程(游戏引擎)来绘制和更新可绘制对象。

我有3个类来实现此目的,分别是BattleActivity,BattleView和BattleThread。 从另一个活动中调用BattleActivity。 BattleThread将调用BattleView.update()和BattleView.render()来完成这项工作。 但我看不到有什么用。 我知道所有这些都可以通过日志记录和调试(我已经尝试过)来实现,但是我仍然无法弄清楚哪个是不正确的。 有人愿意帮助我吗?

public class BattleActivity extends Activity {

@Override

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

// Making it full screen

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

//... shortened

// Create the battle view

battleView = new BattleView(this);

battleView.setBackground(battleBackground);

setContentView(battleView);

}}

BattleView:

public class BattleView extends SurfaceView implements SurfaceHolder.Callback{

//... shortened

public BattleView(Context context)

{

super(context);

getHolder().addCallback(this);

// Surface has been created

battleThread = new BattleThread(getHolder(), this);

setFocusable(true);

}

public synchronized void render(Canvas canvas)

{

// this function is called when I do debugging, but no image is shown

// canvas and background is not null

canvas.drawBitmap(background, 0, 0, null);

}

public synchronized void update()

{

monsterState = leftMonster.update();

//... shortened

rightMonster.update();

}

@Override

public void surfaceCreated(SurfaceHolder holder)

{

// Start the thread

battleThread.setRunning(true);

battleThread.start();

}}

战线:

public class BattleThread extends Thread {

public BattleThread(SurfaceHolder surfaceHolder, BattleView battleView)

{

super();

this.surfaceHolder = surfaceHolder;

this.battleView = battleView;

}

@Override

public void run()

{

Canvas canvas;

//... shortened

while (running)

{

canvas = null;

// try locking the canvas for exclusive pixel editing

// in the surface

try

{

canvas = this.surfaceHolder.lockCanvas();

synchronized (surfaceHolder)

{

beginTime = System.currentTimeMillis();

framesSkipped = 0; // resetting the frames skipped

// update game state

this.battleView.update();

beginTime = System.currentTimeMillis();

// render it

this.battleView.render(canvas);

//... shortened

}

}

finally

{

if (canvas != null)

{

surfaceHolder.unlockCanvasAndPost(canvas);

}

} // end finally

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值