Android SurfaceView为空

时间:2012-12-05 22:36:30

标签: android surfaceview

我正在尝试制作一款简单的2D Android游戏。在这个游戏中,我试图制作一些按钮来控制角色,而角色则在SurfaceView中显示。目前,SurfaceView仅渲染黑屏。

这是我的MainActivity,它扩展了Activity

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    GameView gm = (GameView)findViewById(R.id.snake_surface);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.layout.main, menu);
    return true;
}

GameView类,扩展了SurfaceView

   private HeroSprite hero;
   public GameView(Context context, AttributeSet attributeSet) {

         super(context, attributeSet);

         gameLoopThread = new GameLoopThread(this);

         getHolder().addCallback(new SurfaceHolder.Callback() {

                public void surfaceDestroyed(SurfaceHolder holder) {
                       boolean retry = true;
                       gameLoopThread.setRunning(false);
                       while (retry) {
                              try {
                                    gameLoopThread.join();
                                    retry = false;
                              } catch (InterruptedException e) {}
                       }
                }

                public void surfaceCreated(SurfaceHolder holder) {
                       createSprites();
                       createHero();
                       gameLoopThread.setRunning(true);
                       gameLoopThread.start();
                }


                public void surfaceChanged(SurfaceHolder holder, int format,int width, int height) {
                }
         });
         bmpBlood = BitmapFactory.decodeResource(context.getResources(), R.drawable.blood1);
   }


      private void createHero(){
       hero=(createHeroSprite(R.drawable.aslan));

   }
      private HeroSprite createHeroSprite(int resouce) {
          Bitmap bmp = BitmapFactory.decodeResource(getResources(), resouce);
          return new HeroSprite(this, bmp);
    }
   private void createSprites() {
         sprites.add(createSprite(R.drawable.tavsan));
         sprites.add(createSprite(R.drawable.fare));
         sprites.add(createSprite(R.drawable.sansar));
         sprites.add(createSprite(R.drawable.tavsan));
         sprites.add(createSprite(R.drawable.fare));
         sprites.add(createSprite(R.drawable.sansar));
         sprites.add(createSprite(R.drawable.tavsan));
         sprites.add(createSprite(R.drawable.fare));
         sprites.add(createSprite(R.drawable.sansar));
         sprites.add(createSprite(R.drawable.tavsan));
         sprites.add(createSprite(R.drawable.tavsan));

   }

   private Sprite createSprite(int resouce) {
         Bitmap bmp = BitmapFactory.decodeResource(getResources(), resouce);
         return new Sprite(this, bmp);
   }

   @Override
   protected void onDraw(Canvas canvas) {
         canvas.drawColor(Color.GREEN);
         for (int i = temps.size() - 1; i >= 0; i--) {
                temps.get(i).onDraw(canvas);
         }
         for (Sprite sprite : sprites) {
                sprite.onDraw(canvas);
         }
         hero.onDraw(canvas);
   }

   @Override
   public boolean onTouchEvent(MotionEvent event) {
         if (System.currentTimeMillis() - lastClick > 300) {
                lastClick = System.currentTimeMillis();
                float x = event.getX();
                float y = event.getY();
                synchronized (getHolder()) {
                       for (int i = sprites.size() - 1; i >= 0; i--) {
                              Sprite sprite = sprites.get(i);
                              if ((sprite).isCollition(x, y)) {
                                    sprites.remove(sprite);
                                    temps.add(new TempSprite(temps, this, x, y, bmpBlood));
                                    break;
                              }
                       }
                }
         }
         return true;
   }

我的GameLoopThread课程,其中包含Thread

static final long FPS = 10;
private GameView view;
private boolean running = false;

public GameLoopThread(GameView view) {
    this.view = view;
}

public void setRunning(boolean run) {
    running = run;
}

@Override
public void run() {
    long ticksPS = 1000 / FPS;
    long startTime;
    long sleepTime;
    while (running) {
        Canvas c = null;
        startTime = System.currentTimeMillis();
        try {
            c = view.getHolder().lockCanvas();
            synchronized (view.getHolder()) {
                view.onDraw(c);
            }
        } finally {
            if (c != null) {
                view.getHolder().unlockCanvasAndPost(c);
            }
        }
        sleepTime = ticksPS - (System.currentTimeMillis() - startTime);
        try {
            if (sleepTime > 0)
                sleep(sleepTime);
            else
                sleep(10);
        } catch (Exception e) {
        }
    }
}

这是我的布局:

<LinearLayout  
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="30dp"
        android:gravity="center"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/score"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|center_horizontal"
            android:text="Score:"
            android:textSize="20dp" />

        <Button
            android:id="@+id/btnThread"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|center_horizontal" />

        <TextView
            android:id="@+id/max"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|center_horizontal"
            android:layout_marginLeft="100dp"
            android:text="Max:"
            android:textSize="20dp" />
    </LinearLayout>

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.3" >

            <SurfaceView
                android:id="@+id/snake_surface"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="0.3" />

        </FrameLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:weightSum="1" >

            <Button
                android:id="@+id/butUp"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal|center"
                android:text="UP" />

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

                <Button
                    android:id="@+id/butLeft"
                    android:layout_width="100dp"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentTop="true"
                    android:text="LEFT" />

                <Button
                    android:id="@+id/butRight"
                    android:layout_width="100dp"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentTop="true"
                    android:text="RIGHT" />
            </RelativeLayout>

            <Button
                android:id="@+id/butDown"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal|center"
                android:text="DOWN" />
        </LinearLayout>

     </LinearLayout>

</LinearLayout>

1 个答案:

答案 0 :(得分:1)

您在布局中的视图是SurfaceView,而不是GameView。你需要改变它:

        <your.package.name.GameView
            android:id="@+id/snake_surface"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="0.3" />

your.package.name替换为包含GameView的包的实际名称。

此外,第GameView gm = ...行必须在 setContentView

最后,当您修改了将要绘制的项目时,不要忘记致电gm.invalidate()