xml路径数据如何转换为屏幕坐标?我试图抓住这个,以便我可以检查触摸事件是否发生在国家多边形内。
我一直在使用从.svg转换为vectordrawable .xml的地图。为了降低复杂性(希望找到解决方案),我的世界地图包括安哥拉,土耳其和澳大利亚,画在画布上。我觉得画布会给我提供比imageview更多的灵活性。我认为mapviews仅对Google地图有用。 testmap
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="492dp"
android:height="330dp"
android:viewportWidth="492.0"
android:viewportHeight="330.0">
<path
android:name="Angola"
android:pathData="M18.41,120.7L18.95,122.34L19.58,123.67L20.08,124.39L20.93,125.54L22.38,125.37L23.1,125.05L24.32,125.37L24.65,124.81L25.2,123.52L26.56,123.43L26.68,123.05L27.8,123.04L27.61,123.84L30.27,123.82L30.31,125.22L30.76,126.07L30.43,127.41L30.6,128.78L31.33,129.61L31.21,132.26L31.75,132.06L32.71,132.11L34.07,131.78L35.07,131.91L35.3,132.61L35.05,133.69L35.44,134.75L35.11,135.59L35.3,136.37L30.72,136.34L30.62,143.56L32.11,145.43L33.54,146.87L29.5,147.81L24.18,147.48L22.66,146.38L13.76,146.48L13.42,146.64L12.12,145.6L10.69,145.53L9.38,145.92L8.32,146.36L8.11,144.92L8.42,142.9L9.17,140.82L9.29,139.84L10,137.8L10.53,136.88L11.79,135.4L12.49,134.4L12.72,132.74L12.61,131.47L11.95,130.67L11.37,129.32L10.83,127.98L10.94,127.52L11.62,126.64L10.95,124.5L10.5,123.02L9.4,121.62L9.61,121.19L10.52,120.89L11.15,120.93L11.92,120.67ZM9.86,120.27L9.3,120.5L8.72,118.84L9.6,117.89L10.27,117.52L11.09,118.28L10.29,118.74L9.93,119.31Z"
android:strokeColor="#ffffff"
android:fillColor="#4682b4"
android:strokeWidth="1"/>
<path
android:name="Australia"
android:pathData="M301.94,206.07L304.06,207.07L305.26,206.67L306.97,206.11L308.29,206.31L308.44,209.79L307.69,210.81L307.46,213.22L306.7,212.4L305.17,214.5L304.72,214.34L303.37,214.24L302.01,211.67L301.71,209.7L300.44,207.15L300.5,205.81ZM297.9,138.29L298.7,140.07L300.1,139.21L300.83,140.17L301.89,141.06L301.66,142.07L302.13,144.03L302.46,145.17L303.02,145.45L303.61,147.42L303.4,148.62L304.11,150.19L306.49,151.4L308.04,152.51L309.52,153.53L309.23,154.1L310.49,155.57L311.34,158.13L312.22,157.61L313.11,158.64L313.64,158.28L314.02,160.8L315.58,162.28L316.6,163.2L318.32,165.16L318.94,167.12L318.99,168.51L318.84,170.04L319.89,172.15L319.76,174.36L319.38,175.53L318.79,177.79L318.83,179.25L318.4,181.09L317.43,183.45L315.8,184.74L315,186.78L314.27,188.09L313.62,190.39L312.77,191.74L312.21,193.77L311.93,195.66L312.04,196.53L310.78,197.5L308.32,197.6L306.3,198.74L305.29,199.83L303.96,201.04L302.14,199.79L300.79,199.3L301.13,197.84L299.93,198.37L298.01,200.4L296.11,199.63L294.86,199.19L293.61,198.99L291.48,198.18L290.06,196.47L289.65,194.38L289.14,193L288.06,191.9L285.95,191.58L286.67,190.27L286.14,188.28L285.07,190.13L283.11,190.63L284.26,189.14L284.6,187.61L285.44,186.31L285.27,184.37L283.48,186.61L282.11,187.51L281.27,189.63L279.55,188.53L279.62,187.13L278.25,185.21L277.09,184.23L277.5,183.63L274.68,182.05L273.14,181.98L271.03,180.72L267.09,180.97L264.25,181.89L261.75,182.76L259.65,182.58L257.32,183.92L255.42,184.52L255,185.9L254.19,186.97L252.32,187.04L250.94,187.27L249,186.79L247.42,187.08L245.91,187.2L244.61,188.62L243.97,188.5L242.86,189.25L241.81,190.1L240.2,190L238.73,190L236.4,188.29L235.22,187.78L235.27,186.27L236.36,185.91L236.73,185.31L236.65,184.37L236.92,182.56L236.68,181.02L235.52,178.43L235.16,176.98L235.25,175.53L234.38,173.9L234.32,173.16L233.35,172.17L233.07,170.22L231.82,168.27L231.51,167.23L232.48,168.29L231.74,166.02L232.83,166.73L233.48,167.67L233.44,166.42L232.35,164.51L232.14,163.74L231.63,163.02L231.87,161.63L232.32,161.04L232.62,159.84L232.39,158.45L233.29,156.74L233.46,158.55L234.39,156.92L236.17,156.13L237.24,155.12L238.92,154.26L239.92,154.08L240.53,154.37L242.26,153.49L243.59,153.23L243.92,152.72L244.5,152.51L245.72,152.56L248.03,151.88L249.22,150.84L249.78,149.6L251.07,148.43L251.17,147.5L251.23,146.25L252.76,144.3L253.69,146.28L254.62,145.82L253.84,144.74L254.53,143.63L255.5,144.12L255.77,142.39L256.97,141.27L257.5,140.37L258.6,139.98L258.64,139.35L259.6,139.61L259.64,139.04L260.61,138.72L261.67,138.41L263.29,139.45L264.51,140.8L265.88,140.81L267.28,141.03L266.82,139.78L267.87,137.96L268.86,137.37L268.52,136.81L269.47,135.52L270.8,134.73L271.92,135L273.77,134.57L273.73,133.43L272.12,132.69L273.29,132.36L274.75,132.92L275.91,133.84L277.76,134.41L278.39,134.18L279.75,134.88L281.04,134.23L281.86,134.43L282.37,134L283.38,135.11L282.8,136.31L281.96,137.22L281.21,137.3L281.46,138.2L280.82,139.33L280.04,140.45L280.2,141.09L281.94,142.35L283.63,143.08L284.76,143.87L286.35,145.22L286.97,145.22L288.12,145.81L288.46,146.52L290.55,147.3L292,146.51L292.43,145.28L292.88,144.26L293.15,143.01L293.82,141.2L293.51,140.1L293.67,139.44L293.42,138.14L293.71,136.44L294.13,135.98L293.79,135.23L294.32,134.04L294.73,132.81L294.79,132.17L295.61,131.33L296.22,132.43L296.38,133.83L296.92,134.1L297.02,135.05L297.82,136.19L297.98,137.47Z"
android:strokeColor="#ffffff"
android:fillColor="#4682b4"
android:strokeWidth="1"/>
<path
android:name="Turkey"
android:pathData="M63.63,7.87L66.78,9L69.34,8.55L71.23,8.81L73.82,7.29L76.16,7.15L78.28,8.58L78.65,9.61L78.44,11.01L80.08,11.73L80.94,12.56L79.44,13.37L80.12,16.61L79.69,17.47L80.89,19.69L80.89,19.69L79.84,20.16L79.07,19.46L76.52,19.1L75.57,19.53L73.07,19.96L71.89,19.91L69.36,20.94L67.55,20.95L66.39,20.44L63.97,21.2L63.25,20.66L63.13,22.19L62.54,22.79L61.95,23.38L61.14,22.15L61.98,21.12L60.64,21.36L58.8,20.73L57.29,22.3L53.96,22.6L52.18,21.14L49.81,21.05L49.3,22.18L47.78,22.5L45.66,21.05L43.26,21.1L41.96,18.36L40.36,16.81L41.42,14.63L40.03,13.27L42.47,10.53L45.85,10.42L46.77,8.21L50.96,8.6L53.6,6.7L56.16,5.86L59.8,5.8ZM42.28,9.75L40.44,11.3L39.75,9.96L39.78,9.36L40.3,9.04L40.98,7.21L39.91,6.43L42.15,5.5L44.04,5.9L44.3,7.03L46.22,7.98L45.82,8.69L43.21,8.85Z"
android:strokeColor="#ffffff"
android:fillColor="#4682b4"
android:strokeWidth="1"/>
我已将边界设置为固有图像宽度和高度,确保宽度和高度等同于.xml文件中的viewportwidth和viewportheight。
public class MyView extends View{
VectorDrawableCompat mymap;
private RectF rectbuffer;
public MyView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mymap = VectorDrawableCompat.create(getContext().getResources(), R.drawable.ic_ata, null);
int mapwidth = mymap.getIntrinsicWidth();
int mapheight = mymap.getIntrinsicHeight();
mymap.setBounds(0, 0, mapwidth, mapheight);
mymap.draw(canvas);
}
例如,澳大利亚的道路从X = 301.94开始,Y = 206.07,这是塔斯马尼亚(澳大利亚下面的岛屿)的一个点。如果我点击Tasmania,我的屏幕位置会显示在日志中,大约X = 453 Y = 346。
@Override
public boolean onTouchEvent(MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
}
float newx= event.getRawX() - myview.getLeft();
float newy= event.getRawY() - myview.getTop();
Log.e("Xvalue", String.valueOf(newx));
Log.e("Yvalue", String.valueOf(newy));
return true;
}
`必须有一种简单的方法来使用xml路径数据计算屏幕坐标?
答案 0 :(得分:0)
您的触摸坐标计算已关闭:event.getRawX()为您提供屏幕坐标,您需要通过View的屏幕坐标来偏移它以获得此视图的相对位置:
@Override
public boolean onTouchEvent(MotionEvent event) {
int[] loc = new int[2];
myview.getLocationOnScreen(loc);
float newx = event.getRawX() - loc[0];
float newy = event.getRawY() - loc[1];
// Log the values:
...
return true;
}