android - 具有可点击区域的可缩放 SVG - Android

标签 android svg motionevent touchimageview svg-android

我正在制作非常简单的应用程序。它需要显示一个包含广义世界地图的 SVG。通过单击城市名称(svg 矩形),我需要显示与该城市相对应的另一个 SVG。此外,所有 SVG 都必须支持缩放和平移。

我设法使 SVG 支持缩放和平移,而且效果很好。为此,我使用 svg-android用于呈现 SVG 和自定义的库 TouchImageView用于平移、缩放和捕获缩放和触摸事件。但是,在 Android 的 SVG 上制作可点击区域似乎是不可能的。

我尝试了蛮力,在每次用户交互(触摸或缩放)和每次点击 SVG 时重新计算我感兴趣的区域的位置,以检查我的区域(矩形列表)是否包含该点,并执行相应的操作行动。

那行不通,因为我无法理解计算 MotionEvent#getX/Y 和类似方法的规则。也许我可以为固定的 SVG 实现这一点,但对于可缩放的 SVG,我真的做不到。

有什么提示吗?这在 Android 中甚至可能吗?

最佳答案

首先,对于迟到的回答真的很抱歉。希望对大家来说还不算太晚:)

我做了一个GitHub repo with code sample (不适用于工作应用程序),所以请使用它! :)

使用存储库的自述文件了解一些细节。

核心解决方案:

SVG svg = SVGParser.getSVGFromResource(getResources(), R.raw.world_map);

// Get image view (this is our SVG map that is going to be clickable and zoomable):
ImageView touchImageView = (ImageView) view.findViewById(R.id.touchImageView);

// Create drawable (will convert svg to drawable so we can set it to image view as a bitmap):
PictureDrawable image = svg.createPictureDrawable();
Bitmap b = drawableToBitmap(image); // refer to WorldMapFragment for source code of the converting method 
touchImageView.setImageBitmap(b);

// Create image with clickable areas from previous image view and set listener to id (listener of type OnClickableAreaClickedListener created in onAttach callback):
ClickableAreasImage clickableAreasImage = new ClickableAreasImage(new PhotoViewAttacher(touchImageView), listener);

// Define your clickable areas
// parameter values (pixels): (x coordinate, y coordinate, width, h eight) and assign an object to it
List<ClickableArea> clickableAreas = new ArrayList<>();
Map<String, FintemCity> cities = svg.getCities();
for (String key : cities.keySet()){
  clickableAreas.add(cities.get(key).toClickableArea(getMetrics()));
}

// Set your clickable areas to the image
clickableAreasImage.setClickableAreas(clickableAreas);

感谢投票! :)

关于android - 具有可点击区域的可缩放 SVG - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41211780/

相关文章:

svg - 更改 svg :g id to inkscape:label

java - 如何根据图像在屏幕上的放置位置显示消息

android - 当用户在 View 上移动手指时,我们如何在 Android 的 onTouchListener 中获取被触摸的 View 的 id?

ios - swift:motionEnded 不触发或真实设备

android - 向不同的收款人汇款 Paypal

android - 管理您不想丢失某些 fragment 数据的 fragment 事务

android - 将 C++ OpenCV 链接到 Android Studio 中的应用程序

javascript - xeponline.jqPlugin.js(或 css2pdf)SVG <use> 引用另一个 SVG 不会在 PDF 中显示

javascript - 将组元素重新排序为特定顺序

android - 返回 fragment 和嵌套 fragment