Android dynamic text drawing / touch detection

I want to display text (relatively large) and detect which character the user is touching. I would also like to embellish the text by drawing a circle (for example) over certain characters. How do you know which characters are in which place? Forcing a monospaced font would be nice if useful programmatically.

Is it possible to also make this line of text scroll left / right (using touch-> slide) and still have touch work to identify the correct character?

+1


source to share


1 answer


This is a very broad question and you have a lot of work ahead of you, but here's a diagram of one possible solution:

  • Create your own view. ImageView is often a good base class to inherit from.

    Override the onDraw () method of the view.

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

    Use paint.getTextBounds to turn off text. See the discussion below for a complete understanding. Since you've drawn text on the screen and you've measured it, you know exactly where the character is.

    Override the onTouch () method in your custom view and track the events you want. There is a lot of QA here on SO on how to do this.

    To scroll the text use canvas.translate (), using the movement you calculate in onTouch (). Since you know how much you scrolled (translated) you know how far your characters have come and can compensate for your touch detection to detect characteristic touches.

    Finally, since you are now in control of the entire drawing, and the Symbol positions are abstract, make sense when compared to the touch position, you can decorate them in any way you want.

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



Phew!

Good luck.

+1


source







All Articles