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.
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);
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With