swati
swati

Reputation: 1283

How to get relative coordinate on a large screen if we have a coordinate for small screen in android

I have coordinates corresponding to screen resolution 600x400. Now I want to get the relative position of this coordinate for the screen resolution 1280x800. After getting the coordinates, I have to create a link on that coordinate.
.For example

Suppose I have a coordinate (5,5) for a 600*400 device resolution, so this coordinate will be at the left-bottom of the screen.Now i want to know what will be the relative coordinate of this on a 1280*800 screen resolution device so that it looks at the same position i.e bottom left of screen. Thanks in advance.

Upvotes: 1

Views: 2119

Answers (2)

Khay
Khay

Reputation: 993

Well sticking to what you asked ,you can get your new pixels as per follow

suppose the coordinates are (6,4) on 600*400 screen size, now calculate the % of x,y as per screen resolution ,as follow

(6 * 100 )/600 = 1%

and

(4* 100)/400 = 1%

now calculate the coordinates as per the new screen size as follow ,

(1 * 1280) /100 = 12.8 

and

(1* 800) /100 = 8

so the coordinates in the new screen size are : (12.8, 8) which were previously (6,4) .

But there are better ways to go through in requirements like these , if you could be more specific with what you are actually doing.

Upvotes: 2

Techfist
Techfist

Reputation: 4344

Here is formula for converting dp to px, and px to dp based on screen density, so either way you can convert the coordinates appropriated to relative screen density.

public int doToPixel(int dp) {
    DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
    int px = Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));       
    return px;
}


public int pixelToDP(int px) {
    DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
    int dp = Math.round(px / (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
    return dp;
}

Upvotes: 0

Related Questions