For a generally rough position you'd multiply your x/y values by the DIsplayMetrics density parameter. I'd have to take another look at xdpi and ydpi, but you'd figure out a scaling value from xdpi and ydpi and be able to multiply your x/y positions by these scaling values to convert to pixel/screen space. A slightly more generic approach is to create or use a smaller helper conversion class that creates a coordinate space and a mapping to pixel space.

I'll be extra nice and post a reduced version of what is found in TyphonRT my middleware that is actually being released really soon. I've not played around with this with Android yet, but it should work as intended. Essentially you set width and height in pixel dimensions of your game board and xMin, xMax, yMin, yMax as the coordinate bounds (-100, 100, -100, 100) or whatever bounds you want. Say you do that and width & height is 480x320 (you can set this to anything you want and in your case whatever the pixel space of the board would be). Note that a non square width/height will introduce a skew of sorts. Also note posOffsetX & posOffesetY which can be set to border pixel offsets if the game board is not full screen and width/height is just part of the screen.

With those values the scaling values become

xScale = ((float) width) / (xMax - xMin);

yScale = ((float) height) / (yMax - yMin);

xScale = 480 / 200 = 2.4

yScale = 320 / 200 = 1.6

If you picked coordinate point (50, 50) and called coordsToPixels

public final void coordsToPixels(float x, float y, Tuple2f out)

{

out.x = (int) (xScale * (x - xMin) + s_OFFSET + posOffsetX);

out.y = (int) (yScale * (yMax - y) + s_OFFSET + posOffsetY);

}

out.x = 2.4 * (50 - (-100)) + 0.5 = 360

out.y = 1.6 * (50 - (-100)) + 0.5 = 240

So out.x = 360 and out.y = 240 which is precisely 75% of the width/height of the screen in pixel space and (50,50) in coordinate space is 75% / 75% of the coordinate space mapping (-100 to 100 in x/y).

Regarding the Tuple2f class that is a standard vector math class defined in the javax.vecmath library/API. You can find a free version here:

http://www.objectclub.jp/download/vecmath_eTyphonRT uses a modified version of the standard javax.vecmath library/API.

Note there are also methods for converting from pixel space into coordinate space with the methods pixelsToCoords

Using java Syntax Highlighting

public class CoordsConverter2D

{

public static final float s_OFFSET = 0.5f; // Used instead of Math.round()

public float xMin, xMax, deltaX, yMin, yMax, deltaY;

public int width, height;

public int posOffsetX, posOffsetY; // Internal offset of component; borders, etc.

float additionalSize = 50f;

float xScale, yScale;

public CoordsConverter2D(int width, int height)

{

this.width = width;

this.height = height;

setScale();

deltaX = deltaY = 1.0f;

}

public CoordsConverter2D(int width, int height, float xMin, float xMax, float yMin, float yMax)

{

this.width = width;

this.height = height;

this.xMin = xMin;

this.xMax = xMax;

this.yMin = yMin;

this.yMax = yMax;

setScale();

deltaX = deltaY = 1.0f;

}

private void setScale()

{

xScale = ((float) width) / (xMax - xMin);

yScale = ((float) height) / (yMax - yMin);

}

public void setPixels(int height, int width)

{

this.height = height;

this.width = width;

setScale();

}

public void setCoords(float xMin, float xMax, float yMin, float yMax)

{

this.xMin = xMin;

this.xMax = xMax;

this.yMin = yMin;

this.yMax = yMax;

setScale();

}

public void setDeltas(float deltaX, float deltaY)

{

this.deltaX = deltaX;

this.deltaY = deltaY;

}

// coordsToPixels 2D --------------------------------------

public final void coordsToPixels(float x, float y, Tuple2f out)

{

out.x = (int) (xScale * (x - xMin) + s_OFFSET + posOffsetX);

out.y = (int) (yScale * (yMax - y) + s_OFFSET + posOffsetY);

}

public final void coordsToPixels(Tuple2f v, Tuple2f out)

{

out.x = (int) (xScale * (v.x - xMin) + s_OFFSET + posOffsetX);

out.y = (int) (yScale * (yMax - v.y) + s_OFFSET + posOffsetY);

}

public final void coordsToPixels(Tuple2f v, int xVal[], int yVal[], int index)

{

xVal[index] = (int)(xScale * (v.x - xMin) + s_OFFSET + posOffsetX);

yVal[index] = (int)(yScale * (yMax - v.y) + s_OFFSET + posOffsetY);

}

// pixelsToCoords --------------------------------------

public final void pixelsToCoords(int x, int y, Tuple2f out)

{

out.x = (float) x / xScale + xMin;

out.y = yMax - (float) y / yScale;

}

public final void pixelsToCoords(Tuple2f p, Tuple2f out)

{

out.x = p.x / xScale + xMin;

out.y = yMax - p.y / yScale;

}

}

Parsed in 0.102 seconds, using

GeSHi 1.0.8.4