Reputation: 251
I'm doing a 2D game on android, and everything works just fine. The problem is the character moves really fast on my phone (Xperia) but on a phone with a much more wider screen he runs really slow. I tried getting the screen width and divide by 100 and set it as the step so the character will have a step depending on the width- didn't work. Is there some way to get some sort of scaled pixel or something. Appreciate the help :)
here's who i do it:
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
height = displaymetrics.heightPixels;
width = displaymetrics.widthPixels;
ideas?
Upvotes: 1
Views: 436
Reputation: 510
This works for me.
int height = Resources.getSystem().getDisplayMetrics().heightPixels;
int width = Resources.getSystem().getDisplayMetrics().widthPixels;
Upvotes: 1
Reputation: 4430
You want the density scale according to the default of 160, it is a multiplier and when you obtain the height and width in pixels you can use this multiplier in your code to correctly scale.
See the density section of the developer page linked below.
http://developer.android.com/reference/android/util/DisplayMetrics.html#density
Upvotes: 0
Reputation: 7947
Multiply your step width with displaymetrics.density
. That effectively turns a "px" value into a "dp" value, i.e. it should be the same physical width on all devices.
Upvotes: 0