Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

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.

like image 234
Shanij P.S Avatar asked Jan 12 '23 04:01

Shanij P.S


2 Answers

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);
like image 198
Steven Trigg Avatar answered Jan 17 '23 18:01

Steven Trigg


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

like image 45
Pratapi Hemant Patel Avatar answered Jan 17 '23 17:01

Pratapi Hemant Patel