大家好,我是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(){
}
}
}
你错过了几个回调
查看下一个示例
带有SurfaceHolder的SurfaceView示例
你的类应该实现SurfaceHolder。回调和调用函数holder.addCallback(这)