查找TextView中单个字符的确切坐标 [英] Find exact coordinates of a single Character inside a TextView

查看:316
本文介绍了查找TextView中单个字符的确切坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我在遍历TextView文本的副本时使用 paintObject.measureText(textCopy.substring(0,i))。例如, measureText( abc .substring(0,1))会给我'b'的相对x坐标。我从 layout.getLineTop()获得的 y 坐标。这是可行的,但对于非等宽字体的 x 坐标,不准确。我可以进行一些校准,但是在每个设备上它的工作方式都不同。

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.

我能想到的最佳解决方案是覆盖负责在TextView上绘制TextView的类。屏幕,并希望获得绘制到屏幕上的每个字符的坐标。

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?

推荐答案

好吧,这似乎是 Paint.measureText( )或其他更高级的类。但是我最终找到了解决方法:

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.

它将返回行位置的Character REGARDLESS的x。因此,我仍然可以从 layout.getLineTop()获得行。顺便说一句,如果您使用的是 layout.getLineTop(),请注意其中有些奇怪的行为,可能是一个错误。我已经在此处提交了错误报告。

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.

这篇关于查找TextView中单个字符的确切坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆