如何从kotlin中的imageview获取VectorDrawable路径点击监听器事件

How to get VectorDrawable path click listener event from imageview in kotlin

我正在展示一个显示 svg 地图的图像视图。我的要求是获得特定的 svg 路径点击事件。我不知道这个场景的过程或任何想法

这是我的xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="none">

        <ImageView
            android:id="@+id/richPathView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_map" />

    </HorizontalScrollView>

</RelativeLayout>

这是我的 svg 地图 请帮助我如何在点击 svg 路径时获得点击事件。

非常感谢任何帮助。

你不能。无法确定单击了 VectorDrawable 中的哪条路径。至少没有简单的方法。

我现在知道的唯一方法是将 SVG 加载到 WebView 中。然后从 Javascript.

调用 Android 应用中的方法

Call Java function from JavaScript over Android WebView

我发现 this (RichPath) 很棒的库,它允许我检测路径点击

class

 richPathView.setOnPathClickListener { richPath ->
            if (richPath.name != null) {
                when {
                    richPath.name.toLowerCase() == "m" -> {
                        // my task
                    }
                    richPath.name.toLowerCase() == "e" -> {
                        // my task
                    }
            }
      }

XML

<HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="none"
        android:overScrollMode="never"
        android:layout_centerVertical="true">
        <com.richpath.RichPathView
            android:id="@+id/richPathView"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            app:vector="@drawable/ic_map" />

</HorizontalScrollView>

不知道这是否是有效的正确解决方案,但它正在工作。