Android dynamic text drawing / touch detection

狂风中的少年 提交于 2019-12-11 10:38:47

问题


I want to display text (relatively large) and determine which character the user touches. I'd also like to embellish the text by drawing a circle (for example) over certain characters. How do I know which characters are at what location? Enforcing monospaced font would be fine if helpful programmatically.

Would it also be able to make this one line of text scroll left/right (using touch->slide) and still have touch work to determine the correct character?


回答1:


A very broad question, and you've got a lot of work ahead, but here's the outline of one possible solution:

  • Create a custom view. ImageView is often a good base class to inherit.

    Override the onDraw() method of the view.

    Use canvas.drawText() to place your text on screen.

    Use paint.getTextBounds to meausure your text. See the discussion linked below for a thorough understanding. Since you drew the text on screen, and you measured it, you know exactly where each character is.

    Override the onTouch() method in your custom view and track the events you need. There are many QAs here in SO on how to do this.

    For scrolling the text, use canvas.translate() using the movement you calculate in the onTouch(). Since you know how much you scrolled (translated), you know how far your characters have moved and can offset your touch detection to detect character touches.

    Finally, since you now control all of the drawing and, your character positions are abstract, only having meaning when compared to a touch position, you can embellish them in any way choose.

Android Paint: .measureText() vs .getTextBounds()

Phew!

Good luck.



来源:https://stackoverflow.com/questions/12549647/android-dynamic-text-drawing-touch-detection

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