Can't call the onDraw of a surface view

Problems with Canvas, OpenGL, etc...

Can't call the onDraw of a surface view

Postby EightBitSpade » Fri Oct 15, 2010 3:00 am

Hello everyone! I'm having an odd problem that I hope you can help me with. In my program I have a variable named myImage of type Bitmap, which i initially make refer to a drawable image using the line:

myImage = BitmapFactory.decodeResource(getResources(), R.drawable.dirtmound);

Displaying this image later using canvas.drawBitmap works fine. But later I want to change the image that myImage refers to, so I use a line like this:

myImage = BitmapFactory.decodeResource(getResources(), R.drawable.clump);

After looking at things in the debug, it seems that myImage is indeed referring to the second image, but it isn't drawing. In my surfaceview's onDraw method I call the method canvas.drawBitmap using the variable myImage as the bitmap argument. The ondraw method is being called over and over in a while loop that i have going on in a thread called CanvasThread, which looks like this:

Code: Select all
Canvas c;
      while(_run)
      {
         counterUpdate();
         c = null;
         try
         {
            c = _surfaceHolder.lockCanvas(null);
            synchronized (_surfaceHolder)
            {   
               _panel.onDraw(c);
            }
         }
         finally
         {
            //do this in a finally so that if an exception is thrown
            //during the code above, we don't leave the surface in an
            //inconsistent state.
            if (c != null)
            {
               _surfaceHolder.unlockCanvasAndPost(c);
            }
         }


EDIT:
However, upon going through the debugger, I have found that my problem is that the onDraw method in my Panel calss (which extends SurfaceView) is never getting called. Does anyone know why this would be?
EightBitSpade
Junior Developer
Junior Developer
 
Posts: 15
Joined: Sun Sep 26, 2010 2:07 am

Top

Re: Can't call the onDraw of a surface view

Postby Tage Nielsen » Wed Oct 20, 2010 9:22 pm

Apparently this is a common problem.

You need to call invalidate() to cause your view to call its method onDraw().
Tage Nielsen
Junior Developer
Junior Developer
 
Posts: 17
Joined: Wed Oct 13, 2010 8:02 pm

Top

Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: No registered users and 4 guests