Android的SurfaceView显示空白的画布锁定不为空 [英] Android SurfaceView displays blank when locked canvas is not null

查看:669
本文介绍了Android的SurfaceView显示空白的画布锁定不为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用该扩展了SurfaceView视图,并使用一个线程来借鉴SurfaceView图像创建一个简单的游戏。本场比赛将有它自己的线程(游戏引擎)来绘制和更新可绘制。

I'm creating a simple game using a view which extends a SurfaceView and using a thread to draw images on the SurfaceView. The game will have its own thread (game engine) to draw and update drawables.

我有3个班,以实现这一目标,即BattleActivity,BattleView和BattleThread。该BattleActivity从另一个活动调用。 BattleThread将调用BattleView.update()和BattleView.render()来完成这项工作。但我没有看到任何工作。我知道这一切是通过记录和调试(我都试过)到达,但我仍然无法弄清楚这是不正确。任何人都关心帮助我吗?

I have 3 classes to achieve this, namely BattleActivity, BattleView, and BattleThread. The BattleActivity is called from another activity. BattleThread will call BattleView.update() and BattleView.render() to do the job. But I don't see anything working. I know it all is reachable through logging and debugging (which I have tried), but I still can't figure out which is incorrect. Anyone care to help me?

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:

The 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();
}}

该BattleThread:

The BattleThread:

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
    }
}

}

推荐答案

我知道这是一个老的文章,但为了以防万一它可以帮助人在那里。

I know this is an old post but just in case it helps someone out there.

我有这个同样的问题,几乎是相​​同的code到你的,发现所有我需要做的是调用 postInvalidate()从我重写的onDraw(帆布油画)在我的SurfaceView方法。

I had this same problem, almost identical code to yours, and found that all I needed to do was call postInvalidate() from my overridden onDraw(Canvas canvas) method in my SurfaceView.

这是我的code:

@Override
protected void onDraw(Canvas canvas)
{
   mPaperPlaneImage.draw(canvas);
   postInvalidate();
}

这篇关于Android的SurfaceView显示空白的画布锁定不为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆