0

I have continents image and i need when user clicks on image detect which continent he is clicked.

I have tried collecting x y coordinates for every continent and when user clicks on image I check if user finger x y exists in my Paths like this:

val path = Path()
path.moveTo(409f, 1986f)
path.lineTo(414f, 1986f)
path.lineTo(418f, 1986f)
...
path.close()

val rectF = RectF()
path.computeBounds(rectF, true)
val r = Region()
r.setPath(path, Region(rectF.left.toInt(), rectF.top.toInt(), rectF.right.toInt(), rectF.bottom.toInt()))

ivMainMap?.setOnTouchListener { v, event ->
    val point = Point()
    point.x = event.x.toInt()
    point.y = event.y.toInt()
    if (r.contains(point.x, point.y)) {
        Toast.makeText(this, "South America", Toast.LENGTH_LONG).show()
    }
    return@setOnTouchListener true
}

but i faced issue with multiple screen sizes, after that i have tried collecting x y coordinates on 560 dpi screen and convert x y to new x y with current screen size density, like this:

private fun getExactX(x: Float): Float {
    val screenAdjust = Resources.getSystem().displayMetrics.densityDpi.toFloat() / 560f
    return ((x) * screenAdjust)
}

private fun getExactY(y: Float): Float {
    val screenAdjust = Resources.getSystem().displayMetrics.densityDpi.toFloat() / 560f
    return ((y) * screenAdjust)
}

but the issue still exist

continents image

Mohamed Hamdan
  • 147
  • 2
  • 16
  • Possible duplicate of [Android: How to check if a path contains touched point?](https://stackoverflow.com/questions/9588003/android-how-to-check-if-a-path-contains-touched-point) – Martin Zeitler Oct 26 '18 at 23:09
  • hint: `RectF` is a rectangle, which does not meet the minimum requirements, for the shape of a continent ...while you'd obviously need five paths, instead of one. while you're drawing rectangles, this probably never will work as expected. – Martin Zeitler Oct 26 '18 at 23:11
  • @MartinZeitler i don't want to check if path contains touched point i want to draw path in image with fixed xs and ys and support all screen sizes – Mohamed Hamdan Oct 26 '18 at 23:26
  • @MartinZeitler I know i need 7 paths not 5 but i added one path here just for example – Mohamed Hamdan Oct 26 '18 at 23:28
  • no matter if 5, 6, or 7... draw a thin line along the paths, to see where they are. one could even make these paths click-able: https://stackoverflow.com/questions/18275032/technique-to-make-a-canvas-drawline-clickable (and even remove the background image). – Martin Zeitler Oct 26 '18 at 23:36
  • Thank you Martin but i know how i can make path clickable but what i don't know how i can draw same path with same position on multiple screen sizes – Mohamed Hamdan Oct 26 '18 at 23:45

1 Answers1

0

Create a ratio multiplier:

DisplayMetrics displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    screen_width = displayMetrics.widthPixels;
    ratio_multiplier = screen_width/720; // Or whatever your base screen size is.

Then that ratio_multiplier can be used for anything that needs resizing.
I use this in all my programs for resizing button, images, etc.
RAPTORp
  • 90
  • 6
  • You should be able to use this for coordinate adjustments as well. – RAPTORp Oct 26 '18 at 22:57
  • I have used this code like this ((x) * (Resources.getSystem().displayMetrics.widthPixels.toFloat() / 1440f)) and it's worked fine with two different screen sizes but i don't know if it's will work with other screen sizes. – Mohamed Hamdan Oct 26 '18 at 23:31
  • It should work for all of them. I've emulated various sizes like watch-wear, tv, etc., also tried on various devices, and even flipped to landscape from portrait during an apps operation; so far so good. But I'm always open to any unforeseen possibilities. – RAPTORp Oct 26 '18 at 23:52
  • It's works fine if android navigation bar visible if i hide it the issue will present Samsung galaxy a8 2018 – Mohamed Hamdan Oct 27 '18 at 02:02