Shanij P.S
Shanij P.S

Reputation: 164

convert x and y coordinate from one android device to another independent of resolution

I have a canvas for painting in my application and saving all the coordinate while user drawing on it. the saved coordinates are then transformed to another device and trying to plot the pixels.

like this; (20,30), (50,40) .. .. ..

Because of the different screen size and resolution my drawing is incomplete and positions and plotted incorrectly

how can i map the coordinate to other device which should be in exact location as that of the device where i draw the actual image.

Upvotes: 4

Views: 4245

Answers (2)

Hemant Patel
Hemant Patel

Reputation: 3260

Try to implement density independent pixel(dp)

The density-independent pixel is equivalent to one physical pixel on a 160 dpi screen.

The conversion of dp units to screen pixels is simple:   px = dp * (dpi / 160).

So first determine your device dpi(dot per pixel).

So to move to (20, 30).
determine x = 20 * (dpi/160);
                    y = 30 * (dpi/160);

move to (x, y).

you can get dpi = getResources().getDisplayMetrics().density

Upvotes: 1

Strigger
Strigger

Reputation: 1903

When you save the coordinates you need to get the device independent pixels from the drawing by dividing the coordinates by the screen density and when you draw it on a device you need to multiply your coordinates by the device density. For example:

float density = getContext().getResources().getDisplayMetrics().density;
canvas.drawText(text, 
        xPos * density,
        yPos * density,
        mPaint);

Upvotes: 8

Related Questions