我正在做一个非常简单的应用程序。它需要显示一个包含广义世界地图的 SVG。通过单击城市名称(svg 矩形),我需要显示与该城市对应的另一个 SVG。此外,所有 SVG 都必须支持缩放和平移。
我设法让 SVG 支持缩放和平移,效果非常完美。为此,我正在使用svg-android https://code.google.com/archive/p/svg-android/用于渲染 SVG 和自定义的库触摸图像视图 https://github.com/MikeOrtiz/TouchImageView用于平移、缩放以及捕获比例和触摸事件。但是,在 Android 中似乎不可能在 SVG 上创建可点击区域。
我尝试使用蛮力,在每次用户交互(触摸或按比例)以及每次单击 SVG 时重新计算我感兴趣的区域的位置,以检查我的区域(矩形列表)是否包含该点,并执行相应的操作。
这不起作用,因为我无法理解计算规则MotionEvent#getX/Y
和类似的方法。也许对于固定 SVG 我可以实现这一点,但对于可缩放,我真的不能。
有什么提示吗?这在 Android 中可能吗?
首先,非常抱歉这么晚才回复。希望对每个人来说还不算太晚:)
我制造了一个带有代码示例的 GitHub 存储库 https://github.com/AlexMilutinovic/ClickZoomSVG(不适用于工作应用程序),所以请使用它! :)
使用 repo 的自述文件了解一些详细信息。
核心解决方案:
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);
感谢投票! :)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)