driftking9987
driftking9987

Reputation: 1713

Get position on a custom imageview across devices

I have a app which uses co-ordinates on the image to pin a marker.

final GestureDetector gestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onSingleTapConfirmed(MotionEvent e) {
                if (imageView.isReady()) {
                    sCoord = imageView.viewToSourceCoord(e.getX(), e.getY());

                    imageView.setPin(new PointF(sCoord.x,sCoord.y));
                    Toast.makeText(getApplicationContext(), "Single tap: " + ((int) convertPixelsToDp(sCoord.x)) + ", " + ((int) convertPixelsToDp(sCoord.y)), Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getApplicationContext(), "Something is not right!", Toast.LENGTH_SHORT).show();
                }
                return true;
            }
        });

Now am doing some calculations on the point and points and changing the position of the marker. It's working fine on the device which am using but when I use another device, it's shows random location(which is obvious).

I tried this to change the co-ordinates(pixels) to dp unit and then pinning the image to that position, still it doesn't work.

/**
 * This method converts dp unit to equivalent pixels, depending on device density. 
 * 
 * @param dp A value in dp (density independent pixels) unit. Which we need to convert into pixels
 * @param context Context to get resources and device specific display metrics
 * @return A float value to represent px equivalent to dp depending on device density
 */
public static float convertDpToPixel(float dp, Context context){
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    float px = dp * (metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
    return px;
}

/**
 * This method converts device specific pixels to density independent pixels.
 * 
 * @param px A value in px (pixels) unit. Which we need to convert into db
 * @param context Context to get resources and device specific display metrics
 * @return A float value to represent dp equivalent to px value
 */
public static float convertPixelsToDp(float px, Context context){
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    float dp = px / (metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
    return dp;
}

Let's say I have an floorplan of a store, I want a pin a marker near the door. So I take the co-ordinate and convert it to dp using above mentioned function,then I pass that dp location to another device and there am converting that dp to pixels and using that location to pin the marker, but it's not working. Showing random locations.

//trying
float x=convertDpToPixel(sCoord.x);
float y=convertDpToPixel(sCoord.y);
imageView.setPin(new PointF(x,y));

Any suggestion is welcomed. Thanks.

Upvotes: 0

Views: 202

Answers (1)

Enzokie
Enzokie

Reputation: 7415

The approach to solve this problem is just to use a Percentage approach, to elaborate the given answer I am going to show some simple Math.

The process of this computation requires the following Values:

  • imageWidth = the width of the image from the left to right.
  • imageHeight = the height if the image from top to bottom.
  • xOccupation = the occupied with of the x-coordinate from left to right.
  • yOccupation = the occupied height of the y-coordinate from top to bottom.

To compute the xPercentage and yPercentage we are going to use this formula:

  • xPercentage = xOccupation/imageWidth
  • yPercentage = yOccupation/imageHeight

After getting those 2 values, you are now ready to send it to your server and access the same value in all kinds of device regardless of the size density.

The client device requires to recompute the x and y coordinates by doing the reverse formula.

  • new-x-coordinate = newImageWidth*xPercentage.
  • new-y-coordinate = newImageHeight*yPercentage.

Upvotes: 2

Related Questions