Mick
Mick

Reputation: 7927

Get canvas height without drawing

I wish to find out what dimensions my canvas is going to be long before I ever need to create one or draw on it.

The only canvas code I know (or have a flimsy knowledge of) is this:

  final SurfaceHolder holder = getHolder();

  try 
  {
    Canvas canvas = holder.lockCanvas();
    if(canvas != null)
    {
      onDraw(canvas);
      holder.unlockCanvasAndPost(canvas);
    }
  } 
  catch (Exception e) 
  {
    e.printStackTrace();
  }

But this looks like it is doing far too much for simply getting the height. Is there a function like WhatHeightWillMyCanvasBeWhenIMakeOne()?

EDIT: ...and if there isn't such a function, then what is a minimal piece of code for temporarily getting a canvas for long enough to ask its height and then get rid of it (if needed).

Upvotes: 2

Views: 3893

Answers (1)

WIllJBD
WIllJBD

Reputation: 6164

You cannot get a canvas sized to your drawable-screen-area until you override the onDraw and do some canvas locking and posting. (from what I understand anyways)

@Override
public void run() {

      while (running) {
             Canvas c = null;

             try {
                    c = view.getHolder().lockCanvas();
                    synchronized (view.getHolder()) {
                           view.onDraw(c);
                    }
             } finally {
                    if (c != null) {
                           view.getHolder().unlockCanvasAndPost(c);
                    }
             }

             try {

                           sleep(10);
             } catch (Exception e) {}
      }
}

Now when you are calling on draw in your loop, you are sending it a canvas. now just implement the onDraw method in your custom view.

@Override
protected void onDraw(Canvas canvas) {
    int h = canvas.getHeight();
    int w = canvas.getWidth();

}

This can also be done w/o a canvas. first of all declare a class with some static holders.

public class Window {
    public static int WINDOW_HEIGHT; // = to screen size
    public static int WINDOW_WIDTH; // = to screen size

}

Next call some special features in your main start up activity. we request these because i've never seen anyone want to draw to a canvas and not want a full screen with no bar. If you plan on not using a full screen ignore the options. So to get an accurate measure we need to hide them here as well.

public void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main); // <--- or here you can call your custom view

    Display display = getWindowManager().getDefaultDisplay(); 
    int width = display.getWidth();
    int height = display.getHeight();
    Window.WINDOW_HEIGHT = height;
    Window.WINDOW_WIDTH = width;
}

from inside your custom view you could make a call like this

private void CreateWindow(Context context) {
    Display display = ((WindowManager)     context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); 
    int width = display.getWidth(); 
    int height = display.getHeight();


    Window.WINDOW_HEIGHT = height; // = to screen size
    Window.WINDOW_WIDTH = width; // = to screen size

}

Upvotes: 1

Related Questions