Displaying simple bitmap on Android

2019-07-16 06:45发布

beginner in graphics here! I'm trying to display a simple image on my device, but nothing appears, and the log doesn't show any bug. The program runs but nothing happens.

Here is my code

protected void onDraw(Canvas canvas) {
    canvas.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.droid_1), 10, 10, null);
}

and the cal of this method

public void run() {
        Canvas canvas;
        long tickCount = 0;
        Log.d(TAG, "starting game loop");
        while (running) {
            canvas = null;
            //try locking the canvas for exclusive pixel editing on the surface
            try
            {
                canvas = this.surfaceHolder.lockCanvas();
                synchronized (surfaceHolder)
                {
                    //update the game state
                    //draws the canvas on the panel
                    this.gamePanel.draw(canvas);                    
                }

            }
            finally
            {
                //in case of an exception the surface is not left in
                //an inconsistent state
                if (canvas != null)
                {
                    surfaceHolder.unlockCanvasAndPost(canvas);
                }
            }

The picture does exist in the /res/drawablemdpi directory.

Thanks for your help!

EDIT:

Thanks for the advice. I had already visit a lot of links before posting. I've finanlly found the answer on my own.

The method onDraw called there

this.gamePanel.draw(canvas);    

which I intentionnally and by mistake corrected by "draw" because it raised an error, should have stood onDraw. The compiler was actually not calling my overriden method but the original one.

The soltuion was to disable the mark on this error (right click) forcing it to call my own method.

Hoping I'm clear. Thanks anyway.

1条回答
登录 后发表回答