Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
409 views
in Technique[技术] by (71.8m points)

android - Find exact coordinates of a single Character inside a TextView

Currently I'm using paintObject.measureText(textCopy.substring(0,i)) while iterating through a copy of the TextView's text. For example, measureText("abc".substring(0,1)) will give me the relative x coordinates of 'b'. The y coordinate I get from layout.getLineTop(). This is working but not accurate for x coordinates for non-monospaced fonts. I can calibrate a little, but on each device it works differently.

The best solution I can think of is to overwrite the class that is responsible for drawing the TextView on the screen and, hopefully, get the coordinates of each character drawn to screen.

Does anyone know what class I need to overwrite to accomplish this? Or maybe some other creative solution?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Well it seems sort of a bug of Paint.measureText() or other deeper class. But I have FINALLY found a way around it:

layout.getPrimaryHorizontal(int offset)

This is very easy. You just iterate through the layout using the length of the text it uses.

It will return the the x of the Character REGARDLESS of line position. So lines I'm still getting from the layout.getLineTop(). By the way, if you are using the layout.getLineTop(), note that there is some strange behaviour, possibly a bug. I have submitted a bug report here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...