Android:如何制作一个可点击的地图图像,每个国家都会产生不同的动作? [英] Android: how to make a clickable map image with each country producing a different action?

查看:23
本文介绍了Android:如何制作一个可点击的地图图像,每个国家都会产生不同的动作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要显示一张漂亮的欧洲地图图像,并且我希望我的应用能够,例如调出不同的活动,当用户单击每个国家/地区时 - 地图上的每个国家/地区都需要有不同的 onClickListener(或等效项).

I need to display a pretty image of a map of Europe, and I want my app to, e.g. bring up a different activity, when the user clicks each country - each country on the map needs to have a different onClickListener (or equivalent).

本质上,当用户在像这样的图像中点击法国而不是西班牙时,我需要能够调用不同的函数:http://commons.wikimedia.org/wiki/File:Blank_map_of_Europe_cropped.svg

Essentially, I need to be able to call a different function when the user taps on France rather than Spain in an image such as this: http://commons.wikimedia.org/wiki/File:Blank_map_of_Europe_cropped.svg

我在 Android 上如何最好地解决这个问题?

How would I best go about this on Android?

我有一些想法,但我可能忽略了一些简单的方法.

I've got ideas, but there may be some simple way that I'm overlooking.

非常感谢!

干杯,r3mo

推荐答案

以下是我解决类似问题的方法.

Here is how I solved a similar problem.

首先复制要用作图像映射的图像并为每个部分着色.不用说,每个部分都有不同的颜色:D.然后在您的布局中创建两个 ImageView.将第一个的背景设置为要显示在屏幕上的图像,将第二个的背景设置为一个颜色.

First duplicate the image that you want to use as an image map and colour each section. Needless to say, a different colour for each section :D. Then create two ImageViews in your layout. Set the background of the first one as the image that you want displayed to screen and the background of the second as the coloured in one.

然后将第二个ImageView的可见性设置为不可见.如果此时运行程序,您应该会看到想要显示的图像.然后使用 OnTouch 侦听器并获取您触摸的像素的颜色.颜色将对应于彩色图像的颜色.

Then set the visibility of the second ImageView to invisible. If you run the program at this point you should see the image that you want displayed. Then use an OnTouch listener and get the colour of the pixel where you touched. The colour will correspond to that of the coloured image.

以下 getColour 方法需要传递触摸事件的 x 和 y 坐标.R.id.img2 是不可见图像.

The following getColour method would need to be passed the x and y coordinates of the touch event. R.id.img2 is the invisible image.

private int getColour( int x, int y)
{
    ImageView img = (ImageView) findViewById(R.id.img2);
    img.setDrawingCacheEnabled(true); 
    Bitmap hotspots=Bitmap.createBitmap(img.getDrawingCache()); 
    img.setDrawingCacheEnabled(false);
    return hotspots.getPixel(x, y);
}

希望对你有帮助:)

这篇关于Android:如何制作一个可点击的地图图像,每个国家都会产生不同的动作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆