1

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.

swati
  • 1,263
  • 2
  • 17
  • 27

2 Answers2

2

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.

Braj
  • 2,164
  • 2
  • 26
  • 44
Khay
  • 981
  • 1
  • 10
  • 22
  • I was looking for this. – swati Dec 11 '13 at 07:08
  • Happy to help... But like I said before Share what is your real intention behind and you can get better solutions than this.. – Khay Dec 11 '13 at 07:55
  • My actual task is to craete some links on imageview.I am getting links from webend.They have created virtual device on the website User will put some links on different position of the device and we have to show that link on our device whose resolution is higher than what they have taken. – swati Dec 11 '13 at 09:43
  • Kwel, sounds like this solution would work well for you in this case!! – Khay Dec 12 '13 at 08:58
0

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;
}
Techfist
  • 4,314
  • 6
  • 22
  • 32
  • Suppose I have a coordinate (5,5) for a 600*400 device resolution, so this coordinate will 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. – swati Dec 11 '13 at 06:26
  • Just give the layout_weight for your view as Left|Bottom and set margins as 5 you will get yor answer, this is what i can figure out at most with your above comment. – Techfist Dec 11 '13 at 06:31
  • btw, (5,5) means x and y position in screen, which by no means represent bottom, left of screen instead it means Top, Left of screen 5 peixel from top and left. – Techfist Dec 11 '13 at 06:32
  • the coordinates are not fixed,its dynamic.So,how can we give weight – swati Dec 11 '13 at 06:39