Question

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 their 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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top