Reputation: 21
In an application, let suppose we have an image of a car onto which we have mark a damage point. We can mark this point anywhere of the image as shown below.
Suppose we have marked this point on a device of screen size “5 inches” and then we calculated the coordinates of this point and sent it to the server. Now the user is using another device of screen size “8 inches”. User wants to see those marked points in his new device. But he is not able to see the appropriate position of the points after fetching the actual value from the server.
Upvotes: 2
Views: 2197
Reputation: 2087
Get the height and width of the screen
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;
Then calculate the coordinates of the image, I assume you are currently doing this in the form of an [x,y] array
Then simply find the ratio across all screen sizes by doing:
float x_ratio = coordinatesArray[0]/width
float y_ratio = coordinatesArray[1]/height
This will result in something along the lines of 0.25
and 0.34
(or something like that depending on the screen size)
You then simply create an array including these two floats and pass this along to the server.
Upvotes: 0
Reputation: 1661
I think the coordinates depend on the device's screen dpi. Assume your server always store the coordinates in mdpi. Then you should get the correct coordinates at device at different dpi like this:
DisplayMetrics dm = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
float screenAdjust = (float) dm.densityDpi / 160f;
int x = (int) (x_value_from_server) * screenAdjust);
int y = (int) (y_value_from_server) * screenAdjust);
Upvotes: 3