4

I am developing a game.I have drawn images on a surface view which is residing in a relative layout.I want that my game's look and feel should be the same in all resolutions. So now i want to measure the surfaceview/canva's width and height so that I can launch the images in proper proportion rather than hard coded values. But I am unable to retrieve the height and width so please help me.

  I tried canvas.getWidth() and all but it returns 0.
tonylo
  • 3,311
  • 3
  • 28
  • 27
Pramod
  • 1,123
  • 2
  • 12
  • 33

2 Answers2

5

Maybe it is because you try to get the width too soon.

Try this:

canvas.post(new Runnable()
    {
      public void run()
      {
        int w = canvas.getWidth();
      }
    }
);
GôTô
  • 7,974
  • 3
  • 32
  • 43
  • This is more than likely correct, I've had the same problem in the past. – user432209 Oct 22 '10 at 16:15
  • 1
    I saved surface view's width and height in Surface view's overridden method OnSurfaceViewChanged(); and it worked fine for me. – Pramod May 16 '11 at 10:05
  • Sorry to ask (maybe stupid) questions but I can't find the OnSurfaceViewChanged Method: http://developer.android.com/reference/android/view/SurfaceView.html – Tobias Reich Oct 18 '14 at 14:04
0

You probably need to do something like this:

Determining the size of an Android view at runtime

Community
  • 1
  • 1
gregm
  • 12,019
  • 7
  • 56
  • 78