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
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.