开发者

Obtaining SurfaceView dimensions

开发者 https://www.devze.com 2023-01-06 03:01 出处:网络
I think I might be about to ask a dummy question, but I\'m still new with Android programming, and I couldn\'t (despite all my efforts) find my answer on Google.

I think I might be about to ask a dummy question, but I'm still new with Android programming, and I couldn't (despite all my efforts) find my answer on Google.

The thing is, I'm trying to develop a little game, with 2D Graphics. I want my "gaming board" to be at a specific position on my screen, so that I can display in-game informations above and below the box. But since there is beginning to be a lot of Android phones out there, I was thinking about get开发者_开发技巧ting "dynamic" values so that I can adapt my font size to every device.

My game is not in full screen (but it could be, it's no big deal), but in a window with no title bar.

I'm using an extension of the default SurfaceView class, implementing SurfaceHolder.Callback. I tried writing the following method :

public void getViewSize()
    {
        VIEW_WIDTH = this.getWidth();
        VIEW_HEIGHT = this.getHeight();
    }

but the values returned are zeroes.

Anyone got an idea (even if it means changing display strategy) ? Thanks a lot.


You can do this:

public void onDraw(Canvas canvas) {
  VIEW_WIDTH = canvas.getWidth();
  VIEW_HEIGHT = canvas.getHeight();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号