Android image with clickable areas

后端 未结 4 1313
自闭症患者
自闭症患者 2021-01-03 13:52

I need an advice how to achieve the following functionality under Android:

  • I need an image that represents something like a graph (from discrete math), with ve
4条回答
  •  时光说笑
    2021-01-03 14:53

    I can imagine how to do this with SurfaceView:

    • create a Vertex class, which among other things, has an x,y coordinate representing where to draw the vertex. If your vertex was a png image of a circle, then the top-left x,y coordinates of the image are stored in the Vertex class.
    • Have all your verticies in a List, and iterate through and draw each vertex.
    • the edges are more complicated since they might criss-cross or curve around.
      • assuming they are straight lines, then you can have a Edge class that contains the starting x,y and ending x,y coordinates.
      • you can iterate through a List of Edges and draw the lines accordingly

    In order to detect when a user clicks on them, you should override the onTouch method and check the event.rawX() and event.rawY() values to see if they match up to a Vertex or Edge class.

    • for a Vertex class, you can check if x <= event.rawX <= x + image_width and y <= event.rawY <= y + image_height

    • for an Edge, you can check if the event.rawX, event.rawY coordinates are found in the line formed by the two sets of coordinates you stored in the Edge class.

    I've used a similar method to draw a set of nodes in a game. I'm not so sure how to do the edges though - the method I outline would only work if they were straight and do not criss-cross.

    I am sure there is a better way to do this using openGL, but I have not used openGL before.

    Hopefully you can get some ideas out of this.

提交回复
热议问题