开发者

Different size screens for gaming - android

开发者 https://www.devze.com 2023-04-01 16:19 出处:网络
I have asked this before however I am still having issues with this problem! Basically, I am hardcoding pixel values when using a canvas to draw images for a game.

I have asked this before however I am still having issues with this problem!

Basically, I am hardcoding pixel values when using a canvas to draw images for a game.

How would I go about the process of making this game suitable for any screen size as obviously the pixel v开发者_JAVA百科alues for one screen wont run properly on a different screen!

Thanks in advance

Ben


Why do you need pixel values? Use dp not px values!!

final float scale = getContext().getResources().getDisplayMetrics().density;
int px = (int) (dp * scale + 0.5f);

dp Density-independent Pixels - an abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi (dots per inch) screen, so 160dp is always one inch regardless of the screen density. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. You should use these units when specifying view dimensions in your layout, so the UI properly scales to render at the same actual size on different screens. (The compiler accepts both "dip" and "dp", though "dp" is more consistent with "sp".)


you should be able to use the following in order to get the screen dimensions:

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();
int height = display.getHeight();

Then decide what to do as you see fit - stretch your original resolution to fit, draw directly to the larger res, etc.

If your canvas is only part of the screen, keep track of its relative position to the top left and scale the positions/sizes accordingly.

0

精彩评论

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

关注公众号