Nathan Tornquist
Nathan Tornquist

Reputation: 6609

Android Custom View - Adjust to Different Screens

I am using a class that extends the basic surface view. This class contains the main logic for my game. Within the class I have the main update functions as well as my drawing code, which is all canvas-based.

I am not currently using any XML in my code. I create a new instance of my SurfaceView class and set that as the view for the activity. My question is this, what is the best way to support multiple screen sizes?

Because I'm writing a game, I do not want any different layouts on new screen sizes. I just want to scale the game and maybe display a bit of whitespace if the screen ratio does not match my development device.

I have read the Android Developer's guide, but it ultimately doesn't give me answers to the actual implementation strategy when you are not using XML.

Upvotes: 2

Views: 715

Answers (1)

Carles
Carles

Reputation: 451

I have the same problem here, but i'll try to explain what i've discovered at the moment.

For sure, you'll need knowledge of the screen the game is running on, visit the devoloper's page to know the way to get them.

http://developer.android.com/reference/android/util/DisplayMetrics.html

You need to work on density independent pixels to be sure that your game's behaviour is the same in every different display. There's a post on google groups that maybe will help you to understand dp if you dont know it yet.

http://groups.google.com/a/googleproductforums.com/forum/#!category-topic/mobile/android-devices/jeflid4UsMQ

So the main problem now: Images.

There are only 4 kind of drawable folder, as you know for sure (low, medium, high and xhigh dpi). The problem is that a tablet of 10 inch and a SmarthPhone of 4 inch will probably have the same resolution or dpi (dots per inch). So, when printing an image from the same drawable folder, it will have the same size even if the device's screen is twice as big.

At this point, you have as many different solution as you want. Resizing the images, making the whole view bigger... but you'll probably need the DisplayMetrics class info. I'm still working on my decision but if you have any more specific question I'll try to answer it as good as possible.

Upvotes: 1

Related Questions