我正在制作非常简单的应用程序。它需要显示一个包含广义世界地图的 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/