开发者

Android SurfaceView width/height

I've made a couple of apps for android, but none used graphics that much. I'm now trying to make an actual game. I have some bitmaps that are on my canvas. My problem is I'm not sure where to initialize the开发者_如何学Pythonir positions. and in OnTouch events I want to make sure the bitmaps don't stray outside the screen. For example, initializing one bitmap:

canvas.drawBitmap(glider.getGraphic(), 30, 20, null);

20 and 30 were arbitrary. But I want to be able to write something like screen.getWidth() so I know exactly where the bitmap is relative to the border of the screen. I can't find this in the developers reference.


If you're just looking to get the screen dimensions, take a look at the DisplayMetrics class. You can get an instance and the measurements like so:

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int screenHeight = metrics.heightPixels;
int screenWidth = metrics.widthPixels;

Keep in mind this is the absolute screen size. If you're doing a fullscreen game, this will work fine, but if not, you have to keep in mind the size of the notification bar, and possibly the title bar. It might be better in that instance to just get the height of the SurfaceView itself and use it for your calculations.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜