Android - OnDraw()方法永远不会被调用

时间:2013-03-01 13:44:04

标签: android rendering game-engine

我是Android新手编程的新手,我无法理解为什么我的渲染器中的OnDraw()方法不会被调用,因为调用了invalidate()。然而,最终设置的后台资源会被调用。有两个类,Game.java和Renderer.java。似乎无效调用在游戏循环期间没有时间进行处理。

Game.Java

public class Game extends Activity 
{
    Level currentLevel;
    private static List<Entity> _renderList = new ArrayList<Entity>();


    public void StartLevel(View view)
    {   
        System.out.println("I HAVE STARTED THE LEVEL!");
        // Hide the play button
        Button playButton = (Button) findViewById(R.id.start_game);
        playButton.setVisibility(View.GONE);
        // Load the level
        currentLevel = new Level1(this);
        // Create the initial entities

        // Add the entities to the screen   
        List<Bitmap> bitmapList;
        bitmapList = new ArrayList<Bitmap>();
        Bitmap enemy = BitmapFactory.decodeResource(getResources(), R.drawable.enemy);
        bitmapList.add(enemy);
        System.out.println("CREATING RENDERER!");
        Renderer renderer = new Renderer(this, _renderList, bitmapList);
        renderer.setBackgroundResource(R.drawable.beachbackground);
        setContentView(renderer);
        System.out.println("STARTING TRAVEL!");
        while (currentLevel.GetDistTravelled() < currentLevel.GetDist())
        {
            Retrieve();
            currentLevel.AddDistance(1);
            System.out.println("DISTANCE +1!");
            renderer.RenderFrame(_renderList);
            System.out.println("RENDER LEVEL FRAME!");
            try 
            {
                Thread.currentThread().sleep(50);
            } 
            catch (InterruptedException e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        System.out.println("SET BACKGROUND!");
        renderer.setBackgroundResource(R.drawable.menubackground);
    }

    public List<Entity> GetRenderList()
    {
        return _renderList;
    }

    private void Retrieve()
    {
        // get the array
        Entity[] enemies;
        enemies = currentLevel.CheckSpawn();
        if(enemies != null)
        {
            // parse the array into entities
            int length = enemies.length;
            System.out.println(length);

            // remember to Get enemy strings
            for(int i = 0; i < length; i++)
            {
                // add them to our array
                _renderList.add(enemies[i]);
            }

        }
    }
}

Renderer.java

class Renderer extends SurfaceView implements SurfaceHolder.Callback 
{
    PanelThread _thread;
    List<Entity> _renderList = new ArrayList<Entity>();
    List<Bitmap> _bitmapList = new ArrayList<Bitmap>();

    public Renderer(Context context, List<Entity> renderList, List<Bitmap> bitmapList)
    {   
        super(context);
        getHolder().addCallback(this);
        _bitmapList = bitmapList;
    }

    public void RenderFrame(List<Entity> renderList)
    {
        for (int i = 0; i < renderList.size(); i++)
            {
                _renderList.add(renderList.get(i));
            }
        invalidate();
    }


    @Override
    public void onDraw(Canvas canvas)
    {
        Paint paint = null;

        // Draw enemies

        if(_renderList.size() != 0 && _renderList != null)
        {
            int size = _renderList.size();
            for(int i = 0; i < size; i++)
            {
                canvas.drawBitmap(_bitmapList.get(_renderList.get(i).GetBitmapID()), _renderList.get(i).GetX(), _renderList.get(i).GetY(), paint);
            }
        }
    }

        // position the train on the screen in the appropriate place    



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

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) 
    {
        setWillNotDraw(false); //Allows us to use invalidate() to call onDraw()

        _thread = new PanelThread(getHolder(), this); //Start the thread that
        _thread.setRunning(true);                     //will make calls to 
        _thread.start();                              //onDraw()
    }


    @Override
    public void surfaceDestroyed(SurfaceHolder holder) 
    {
        try 
        {
            _thread.setRunning(false);                //Tells thread to stop
            _thread.join();                           //Removes thread from mem.
        } 
        catch (InterruptedException e) {}
    }

    class PanelThread extends Thread
    {
        private SurfaceHolder _surfaceHolder;
        private Renderer _renderer;
        private boolean _run = false;

        public PanelThread(SurfaceHolder surfaceHolder, Renderer renderer) 
        {
            _surfaceHolder = surfaceHolder;
            _renderer = renderer;
        }


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


        @Override
        public void run() 
        {
            Canvas c;
            while (_run) 
            {     
                c = null;

                try 
                {
                    c = _surfaceHolder.lockCanvas(null);
                    synchronized (_surfaceHolder)
                    {
                    //Insert methods to modify positions of items in onDraw()
                    postInvalidate();

                    }
                } 
                finally {
                    if (c != null) {
                        _surfaceHolder.unlockCanvasAndPost(c);
                    }
                }
            }
        }  
    }
}

任何有关解决此问题的帮助都将非常感激。如果需要更多信息,请询问。

1 个答案:

答案 0 :(得分:1)

嗨我和你一样有同样的错误。但我发现了。但我对此并不清楚。

但你可以尝试

将属性 android:background 添加到您的布局中。