我可以在SurfaceView中制作一个按钮吗?

时间:2011-06-21 08:44:51

标签: android surfaceview

我创建了一个这样的类:

public class nir extends Activity implements OnClickListener{

    /** Called when the activity is first created. */
    ImageButton btnNew, btnPlay, btnSignUp;
    TextView txtTitle;
    Typeface font;
    //sprite
    SurfaceView pet;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        font = Typeface.createFromAsset(getAssets(), "fonts/aaaiight.ttf");
        btnNew = (ImageButton)findViewById(R.id.btnNew);
        btnNew.setOnClickListener(this);

        btnPlay = (ImageButton)findViewById(R.id.btnPlay);
        btnPlay.setOnClickListener(this);

        txtTitle = (TextView)findViewById(R.id.lblTama);
        txtTitle.setTypeface(font);

        //sprite

        //setContentView(new MainGamePanel(this));

    }

}

现在我想让SurfaceView显示任何精灵。我试过了

<view class="Tamagotchi.nir.MainGamePanel...">

在我的XML中,但它给了我一个“强制关闭”对话框。

我也有这个课程:

public class MainGamePanel extends SurfaceView implements
        SurfaceHolder.Callback {

    private static final String TAG = MainGamePanel.class.getSimpleName();

    private MainThread thread;
    private Sprite elaine;

    // the fps to be displayed
    private String avgFps;
    public void setAvgFps(String avgFps) {
        this.avgFps = avgFps;
    }

    public MainGamePanel(Context context) {
        super(context);
        // adding the callback (this) to the surface holder to intercept events
        getHolder().addCallback(this);

        // create Elaine and load bitmap
        elaine = new Sprite(
            BitmapFactory.decodeResource(getResources(),
                            R.drawable.walk_elaine),
                            10, 50, // initial position
            30, 47, // width and height of sprite
            5, 5);  // FPS and number of frames in the animation

        // create the game loop thread
        thread = new MainThread(getHolder(), this);

        // make the GamePanel focusable so it can handle events
        setFocusable(true);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // at this point the surface is created and
        // we can safely start the game loop
        thread.setRunning(true);
        thread.start();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        Log.d(TAG, "Surface is being destroyed");
        // tell the thread to shut down and wait for it to finish
        // this is a clean shutdown
        boolean retry = true;
        while (retry) {
            try {
                thread.join();
                retry = false;
            } catch (InterruptedException e) {
                // try again shutting down the thread
            }
        }
        Log.d(TAG, "Thread was shut down cleanly");
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            // handle touch
        }
        return true;
    }

    public void render(Canvas canvas) {
        canvas.drawColor(Color.BLACK);
        elaine.draw(canvas);
        // display fps
        displayFps(canvas, avgFps);
    }

    /**
     * This is the game update method. It iterates through all the objects
     * and calls their update method if they have one or calls specific
     * engine's update method.
     */
    public void update() {
        elaine.update(System.currentTimeMillis());
    }

    private void displayFps(Canvas canvas, String fps) {
        if (canvas != null && fps != null) {
            Paint paint = new Paint();
            paint.setARGB(255, 255, 255, 255);
            canvas.drawText(fps, this.getWidth() - 50, 20, paint);

        }
    }
}

坠机的原因是什么?

1 个答案:

答案 0 :(得分:0)

取出view class = 在您的包名称前面,您只需要包名称.YourSurfaceViewClass

这是你的代码看起来的样子

<Tamagotchi.nir.MainGamePanel.YourSurfaceViewClassName
android:id="@+id/myView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>