表面视图不显示任何内容

时间:2014-07-06 21:41:12

标签: android graphics surfaceview

嘿大家我是android图形的菜鸟,并且会对我的游戏有所帮助。出于某种原因,我的表面视图正在加载,但没有任何东西被渲染(这里不多,所以它应该很简单)只有黑色背景。感谢任何帮助= D。

公共类StartActivity扩展了Activity {

GameView gameView;

Bitmap player;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_start);

    //init our resources
  //  InitializeResources();


    gameView = new GameView(this);

    player = BitmapFactory.decodeResource(getResources(),R.raw.inspector);

    setContentView(gameView);

}



public void InitializeResources(){



  // sprite = new SpriteClass(gameView,player);


}




@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.start, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}


public void stop(){


}


public void pause(){


}





public class GameView extends SurfaceView implements Runnable {


    Thread gamethread = null;

    SurfaceHolder surfaceholder;

    boolean canvasready = false;




    public GameView(Context context) {
        super(context);

        surfaceholder = getHolder();

    }


    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);

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

    }

    @Override
    public void run() {
        // if the canvas is ready draw to it
        while(canvasready ==  true)
        {
            if(!surfaceholder.getSurface().isValid())
            {
                continue;
            }

            Canvas c = surfaceholder.lockCanvas();

            gameView.setBackgroundColor(Color.WHITE);

            c.drawBitmap(player,0,0,null);



            surfaceholder.unlockCanvasAndPost(c);

        }


    }

    public void Pause(){
        canvasready =  false;

        while(true)
        {
            try{
                //tries to stop the thread
                gamethread.join();
            }catch(InterruptedException e){
                e.printStackTrace();

            }

            break;

        }


        gamethread = null;

    }

    public void Resume(){
        canvasready =  true;
        gamethread  = new Thread(this);

        gamethread.start();

    }

    public void stop(){


    }



}

}

1 个答案:

答案 0 :(得分:0)

你错过了几个回调

查看下一个例子

SurfaceView Example with SurfaceHolder

你的类应该实现SurfaceHolder.Callback并调用函数

holder.addCallback(本) 持有人是表面持有人