XML 到 SurfaceView

XML to SurfaceView

我想在我的主菜单上制作一个按钮来开始我的游戏。主菜单在XML,游戏在Su​​rfaceView。我知道我不能直接启动 SurfaceView,所以我将它嵌入到 Activity 中。这是主要内容:

package gametest.gametest;

import android.content.Intent;
import android.os.Bundle;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.app.Activity;
import android.widget.Button;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        init();
    }


    public void init(){
        Button PLAY = (Button) findViewById(R.id.bPlay);
        PLAY.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(v.getContext(), gameViewActivity.class);
                startActivityForResult(i, 0);
            }
        });
    }

}

游戏视图:

package gametest.gametest;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;

import java.util.ArrayList;
import java.util.List;


public class gameView extends SurfaceView{
    private GameLoopThread gameLoopThread;
    private List<Sprite> sprites = new ArrayList<Sprite>();
    private List<TempSprite> temps = new ArrayList<TempSprite>();
    private long lastClick;
    private Bitmap bmpBlood;



    public gameView(Context context) {
        super(context);
        gameLoopThread = new GameLoopThread(this);
        getHolder().addCallback(new SurfaceHolder.Callback() {

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

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

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

    private void createSprites() {
        sprites.add(createSprite(R.drawable.zombie));
        sprites.add(createSprite(R.drawable.zombie));
        sprites.add(createSprite(R.drawable.zombie));
        sprites.add(createSprite(R.drawable.zombie));
        sprites.add(createSprite(R.drawable.zombie));
        sprites.add(createSprite(R.drawable.zombie));
        sprites.add(createSprite(R.drawable.zombie));
        sprites.add(createSprite(R.drawable.zombie));
        sprites.add(createSprite(R.drawable.zombie));
        sprites.add(createSprite(R.drawable.zombie));
        sprites.add(createSprite(R.drawable.zombie));

    }

    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.DKGRAY);
        for (int i = temps.size() - 1; i >= 0; i--) {
            temps.get(i).onDraw(canvas);
        }
        for (Sprite sprite : sprites) {
            sprite.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.isCollision(x, y)) {
                        sprites.remove(sprite);
                        temps.add(new TempSprite(temps, this, x, y, bmpBlood));
                        break;
                    }
                }
            }
        }
        return true;
    }
}

嵌入式游戏视图:

package gametest.gametest;

import android.app.Activity;
import android.os.Bundle;

public class gameViewActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstance){
        super.onCreate(savedInstance);
        setContentView(new gameView(this));
    }
}

主要XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="PLAY"
        android:id="@+id/bPlay"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Headshot!"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="77dp" />

</RelativeLayout>

它运行但当我点击 PLAY 按钮时崩溃。我认为问题出在为我的 onClickListener 创建 Intent 的地方。任何帮助都会很棒!

根据您的错误判断,您似乎忘记了在清单中为新 Activity 添加条目。在您的 AndroidManifest.xml 中,您可能会看到 MainActivity 的条目,看起来像这样:

<activity
    android:name="gametest.gametest.MainActivity"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

您需要在下面为您的 gameViewActivity 创建一个条目,否则您的应用程序将无法识别它。您可能想要添加如下内容:

<activity>
    android:name="gametest.gametest.gameViewActivity"
</activity>