Android在支持多个屏幕的ImageView上绘制固定路径

时间:2018-10-26 22:29:34

标签: android kotlin android-imageview

我有大洲的图像,当用户单击图像时我需要检测他被单击的大洲。

我尝试收集每个大洲的x y坐标,当用户单击图像时,我检查用户的手指x y是否存在于我的“路径”中,如下所示:

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
}

但是我遇到了多个屏幕尺寸的问题,之后我尝试在560 dpi屏幕上收集x y坐标并将x y转换为当前屏幕尺寸密度的新x y,如下所示:

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)
}

但问题仍然存在

continents image

1 个答案:

答案 0 :(得分:0)

创建比率乘数:

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.