Displaying simple bitmap on Android

2019-07-16 07:36发布

问题:

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:

not sure how to do it myself but there are a ton of resources that will show you how its done.

How to Show Image in Android using Java Code

How can I display image in Android Application

How to show a picture?

Show picture in android app

http://www.mkyong.com/android/android-imageview-example/

http://www.javacodegeeks.com/2011/07/android-game-development-displaying.html

and these are just a few from a guick google