how can I create a custom clickable shape on an image in android?

醉酒当歌 提交于 2019-12-08 03:43:37

问题


I want to create a clickable image, my image has some different clickable parts in it, like this one:

I want to draw a custom shape like : A,B,C,D,E,F and make sure when user click on of this something happen. the problem is I don't have any kind of idea to, how create shapes like the shapes in the image make sure it just fix on the image and in different screen size don't see a massed up thing.

回答1:


Will there be more than many of such images?

If no I suggest you to create mask image for each region where black part of image represents the region and white part excludes rest.

To draw image:

  • create custom View
  • in constructor don't forget to use setWillNotDraw to true so you can do custom drawing
  • override View.onDraw method where you can draw main image and all others with some filters via setColorFilter.

To handle click events:

  • override onTouchEvent method
  • get touch position
  • compare touch position with point color in mask image

To optimise:

  • create mask image downscaled by some scale factor
  • during comparison divide touch position by scale factor

This is not ideal, but solution with vectors is non trivial I think




回答2:


Take it as image and setOnclickListner for that image



来源:https://stackoverflow.com/questions/28499282/how-can-i-create-a-custom-clickable-shape-on-an-image-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!