Android: How to Make a Clickable Map Image with Each Country Producing a Different Action

Android: how to make a clickable map image with each country producing a different action?

Here is how I solved a similar problem.

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.

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.

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);
}

Hope this was of some help to you :).

How to use image mapping in Android?

Finally got a perfect solution I find ClickableAreasImages library. I just have to define x,y co-ordination and it allows me to click on that area with zooming and scrolling functionality.

How to make image visible area clickable in android?

Identifying image area clicked in Android? check this question

OR if green will be transparent,
I think the easiest way to detect whether 'visible' content of the image was clicked, is to hook up an OnTouchListener, get the touch coordinates and subsequently get the color for those coordinates using Bitmap.getPixel(int x, int y). Since this will return an ARBG color, you should have little problems with images using an alpha channel. Anything that is 'transparent' (if green will be transparent?) will be invalid, everything else will mean the actual content was tapped.

something like this as a start up:

int color = Bitmap.getPixel(x,y); // x and y are the location of the touch event in Bitmap space
int alpha = Color.getAlpha(color);
boolean isTransparent = (alpha==0);


Related Topics



Leave a reply



Submit