我正在尝试使用here来使用Rect
和getHitRect
在图像上设置点击事件,但是我无法获得一致的结果。
我创建了一个简单的activity layout
:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/name_POI"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
tools:text="Name POI" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<ImageView
android:id="@+id/get_route"
android:layout_width="32dp"
android:layout_height="32dp"
android:src="@drawable/ico_go" />
<ImageView
android:id="@+id/get_info_poi"
android:layout_width="32dp"
android:layout_height="32dp"
android:src="@drawable/ico_info" />
</LinearLayout>
</LinearLayout>
然后我只是尝试复制mapbox的示例,
private boolean handleClickCallout(PointF screenPoint)
{
List<Feature> features = m_mapboxMap.queryRenderedFeatures(screenPoint, CALLOUT_LAYER_ID);
if (!features.isEmpty())
{
if (features.size() == 1)
{
Feature feature = features.get(0);
PointF symbolScreenPoint = m_mapboxMap.getProjection().toScreenLocation(convertToLatLng(feature));
View view = m_hashViewMap.get(feature.getStringProperty(PROPERTY_NAME));
TextView namePOIContainer = view.findViewById(R.id.name_POI);
ImageView getRouteContainer = view.findViewById(R.id.get_route);
ImageView getInfoPOIContainer = view.findViewById(R.id.get_info_poi);
Rect hitRectNamePOI = new Rect();
namePOIContainer.getHitRect(hitRectNamePOI);
Rect hitRectGetRoute = new Rect();
getRouteContainer.getHitRect(hitRectGetRoute);
Rect hitRectGetInfoPOI = new Rect();
getInfoPOIContainer.getHitRect(hitRectGetInfoPOI);
hitRectNamePOI.offset((int) symbolScreenPoint.x, (int) symbolScreenPoint.y);
hitRectGetRoute.offset((int) symbolScreenPoint.x, (int) symbolScreenPoint.y);
hitRectGetInfoPOI.offset((int) symbolScreenPoint.x, (int) symbolScreenPoint.y);
hitRectNamePOI.offset(0, -view.getMeasuredHeight());
hitRectGetRoute.offset(0, -view.getMeasuredHeight());
hitRectGetInfoPOI.offset(0, -view.getMeasuredHeight());
if (hitRectNamePOI.contains((int) screenPoint.x, (int) screenPoint.y))
{
Toast.makeText(this, "Name", Toast.LENGTH_LONG).show();
}
if (hitRectGetRoute.contains((int) screenPoint.x, (int) screenPoint.y))
{
Toast.makeText(this, "Route", Toast.LENGTH_LONG).show();
}
if (hitRectGetInfoPOI.contains((int) screenPoint.x, (int) screenPoint.y))
{
Toast.makeText(this, "Info", Toast.LENGTH_LONG).show();
}
}
else
{
return false;
}
}
else
{
return false;
}
return true;
}
这样做无法获得与hitRectGetRoute
和hitRectGetInfoPOI
一致的结果,因为并非所有ImageView都处理单击事件(尺寸小于ImageView的区域)。 hitRectNamePOI
完全被遗漏,仅处理@+id/get_info_poi
右侧一小部分的点击。