if(ourHolder.getSurface()。isValid()似乎返回false

时间:2015-11-07 22:54:43

标签: java android android-studio

我正在通过阅读书籍来学习android编程,并且出于某种原因,当代码到达“if(ourHolder.getSurface()。isValid()”时,它会转到else语句。代码尚未完成但是根据当我运行代码时,我应该在屏幕上看到蛇头。基于之前的游戏,也可以从本书“if(ourHolder.getSurface()。isValid()”工作正常并绘制Canvas。我也是在“public SnakeAnimView(Context context)”中手动调用Draw();方法。为什么“if(ourHolder.getSurface()。isValid()”转到else语句?

感谢您的帮助**

代码:

package com.packetpub.snake;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

//public class MainActivity extends AppCompatActivity {

public class MainActivity extends Activity
{
    Canvas canvas;
    SnakeAnimView snakeAnimView;
    //The snake head sprite sheet
    Bitmap headAnimBitmap;
    //The portion of the bitmap to be drawn in the current frame
    Rect rectToBeDrawn;
    //The dimensions of a single frame
    int frameHeight = 64;
    int frameWidth = 64;
    int numFrames = 6;
    int frameNumber;
    int screenWidth;
    int screenHeight;
    //stats
    long lastFrameTime;
    int fps;
    int hi;
    //To start the game from onTouchEvent
    Intent i;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {

        Log.i("Step 1 onCreate: ", " start");

        super.onCreate(savedInstanceState);
        //find out the width and height of the screen
        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        screenWidth = size.x;
        screenHeight = size.y;
        headAnimBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.head_sprite_sheet);
        snakeAnimView = new SnakeAnimView(this);
        setContentView(snakeAnimView);
        //i = new Intent(this, GameActivity.class);

        Log.i("Step 1 onCreate: ", " display : " + display);
        Log.i("Step 1 onCreate: ", " screenWidth : " + screenWidth);
        Log.i("Step 1 onCreate: ", " screenHeight : " + screenHeight);
        Log.i("Step 1 onCreate: ", " headAnimBitmap : " + headAnimBitmap);
        Log.i("Step 1 onCreate: ", " snakeAnimView : " + snakeAnimView);
    }



    class SnakeAnimView extends SurfaceView implements Runnable
    {


        Thread ourThread = null;
        SurfaceHolder ourHolder;
        volatile boolean playingSnake = true ;
        Paint paint;



        ////Here is the constructor that gets the frameWidth value by dividing the
        //  bitmap width by the number of frames, and the frameHeight value using
        //  the getHeight method. Type this code after the code from the previous step:
        public SnakeAnimView(Context context)
        {

            super(context);
            ourHolder = getHolder();
            paint = new Paint();
            frameWidth = headAnimBitmap.getWidth() / numFrames;
            frameHeight = headAnimBitmap.getHeight();

            Log.i("Step 2 SnakeAnimView: ", "(Context context)");
            Log.i("Step 2 SnakeAnimView: ", "headAnimBitmap:" + headAnimBitmap);
            Log.i("Step 2 SnakeAnimView: ", "numFrames:" + numFrames);
            Log.i("Step 2 SnakeAnimView: ", "frameWidth:" + frameWidth);
            Log.i("Step 2 SnakeAnimView: ", "frameHeight:" + frameHeight);
            Log.i("Step 2 SnakeAnimView: ", "playingSnake:" + playingSnake);

            draw();
        }




        @Override
        public void run()
        {

            Log.i("Step 3 Run: ", "while (playingSnake): " + playingSnake );
            while (playingSnake)

            {

                Log.i("Step 3 Run: ", "start");

                //update();
                draw();
                //controlFPS();

                Log.i("Step 3 Run: ", "end");
            }
        }


        public void update()
        {

            Log.i("Step 4 update: ", "public void update()");
            //which frame should we draw
            rectToBeDrawn = new Rect((frameNumber * frameWidth) - 1,
                    0, (frameNumber * frameWidth + frameWidth) - 1,
                    frameHeight);
            //now the next frame
            frameNumber++;
            //don't try and draw frames that don't exist
            if (frameNumber == numFrames)
            {
                frameNumber = 0;//back to the first frame
            }
        }


        public void draw()
        {

            Log.i("Step 5 draw: ", "public void draw()");
            Log.i("Step 5 draw: ", "ourHolder : " + ourHolder);
            Log.i("Step 5 draw: ", "ourHolder.getSurface() = " + ourHolder.getSurface());
            boolean test = ourHolder.getSurface().isValid();
            Log.i("Step 5 draw: ", "ourHolder.getSurface().isValid() : " + test);

           if (ourHolder.getSurface().isValid())
            {
                canvas = ourHolder.lockCanvas();
                //Paint paint = new Paint();
                canvas.drawColor(Color.BLACK);//the background
                paint.setColor(Color.argb(255, 255, 255, 255));
                paint.setTextSize(150);
                canvas.drawText("Snake", 10, 150, paint);
                paint.setTextSize(25);
                canvas.drawText(" Hi Score:" + hi, 10, screenHeight - 50, paint);
                //Draw the snake head
                //make this Rect whatever size and location you like
                //(startX, startY, endX, endY)
                Rect destRect = new Rect(screenWidth/2-100,
                        screenHeight/2-100, screenWidth/2+100,
                        screenHeight/2+100);
                canvas.drawBitmap(headAnimBitmap,
                        rectToBeDrawn, destRect, paint);
                ourHolder.unlockCanvasAndPost(canvas);

                Log.i("Step 5 draw: ", "ourHolder : " + ourHolder);
                Log.i("Step 5 draw: ", "canvas : " + canvas);
                Log.i("Step 5 draw: ", "paint : " + paint);
                Log.i("Step 5 draw: ", "screenWidth : " + screenWidth);
                Log.i("Step 5 draw: ", "screenHeight : " + screenHeight);
                Log.i("Step 5 draw: ", "headAnimBitmap : " + headAnimBitmap);


            }
            else
           {
               Log.i("Step 5 draw: ", "Went to Else");

           }

        }


        public void controlFPS()
        {

            Log.i("Step 6 controlFPS: ", "public void controlFPS()");
            long timeThisFrame =
                    (System.currentTimeMillis() -
                            lastFrameTime);
            long timeToSleep = 500 - timeThisFrame;
            if (timeThisFrame > 0) {
                fps = (int) (1000 / timeThisFrame);
            }
            if (timeToSleep > 0)
            {
                try
                {
                    ourThread.sleep(timeToSleep);
                }
                catch (InterruptedException e)
                {

                }
            }
            lastFrameTime = System.currentTimeMillis();
        }


        public void pause()
        {

            Log.i("Step 7 pause: ", "public void pause())");
            playingSnake = false;
            try
            {
                ourThread.join();
            }
            catch (InterruptedException e)
            {

            }

        }

        @Override
        public boolean onTouchEvent(MotionEvent motionEvent)
        {

            Log.i("Step 8 onTouchEvent: ", "public boolean onTouchEvent)");
            startActivity(i);
            return true;
        }

    }




    @Override
    protected void onStop()
    {

        Log.i("Step 9 onStop: ", "onStop");
        super.onStop();
        while (true) {
            snakeAnimView.pause();
            break;
        }
        finish();
    }


//    @Override
//    protected void onResume() {
//        super.onResume();
//        snakeAnimView.resume();
//    }

    @Override
    protected void onPause()
    {
        Log.i("Step 10 onPause: ", "onPause");
        super.onPause();
        snakeAnimView.pause();
    }


    public boolean onKeyDown(int keyCode, KeyEvent event)
    {

        Log.i("Step 11 onKeyDown: ", "onKeyDown");
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            snakeAnimView.pause();
            finish();
            return true;
        }
        return false;
    }`enter code here`

}

1 个答案:

答案 0 :(得分:0)

表面在创建之前无效,请查看创建/销毁曲面时的个别回调。请注意,当它被销毁时,它是无效的。