【发布时间】:2013-08-17 21:16:16
【问题描述】:
这里是图形初学者! 我试图在我的设备上显示一个简单的图像,但什么也没有出现,并且日志 没有显示任何错误。程序运行但没有任何反应。
这是我的代码
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.droid_1), 10, 10, null);
}
以及这个方法的调用
public void run() {
Canvas canvas;
long tickCount = 0;
Log.d(TAG, "starting game loop");
while (running) {
canvas = null;
//try locking the canvas for exclusive pixel editing on the surface
try
{
canvas = this.surfaceHolder.lockCanvas();
synchronized (surfaceHolder)
{
//update the game state
//draws the canvas on the panel
this.gamePanel.draw(canvas);
}
}
finally
{
//in case of an exception the surface is not left in
//an inconsistent state
if (canvas != null)
{
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
图片确实存在于/res/drawablemdpi目录中。
感谢您的帮助!
编辑:
感谢您的建议。在发布之前,我已经访问了很多链接。 我终于自己找到了答案。
那里调用的方法 onDraw
this.gamePanel.draw(canvas);
我故意和错误地通过“draw”更正,因为它引发了错误,应该站在 onDraw 上。 编译器实际上并没有调用我重写的方法,而是调用了原来的方法。
解决方案是禁用此错误上的标记(右键单击),强制它调用我自己的方法。
希望我很清楚。 还是谢谢。
【问题讨论】: