Android 如何获得给定字符串的高度';谁的后裔?
如何获得给定字符串的下降器的高度 比如说,Android 如何获得给定字符串的高度';谁的后裔?,android,Android,如何获得给定字符串的下降器的高度 比如说, abc应返回0 abcl应返回0 abcp应返回从下降线到基线的距离 abclp应返回从下降线到基线的距离 到目前为止,我能说的最好的是 private int getDecender(String string, Paint paint) { // Append "l", to ensure there is Ascender string = string + "l"; final String stringWitho
应返回0李>abc
应返回0李>abcl
应返回从下降线到基线的距离李>abcp
应返回从下降线到基线的距离abclp
private int getDecender(String string, Paint paint) {
// Append "l", to ensure there is Ascender
string = string + "l";
final String stringWithoutDecender = "l";
final Rect bounds = new Rect();
final Rect boundsForStringWithoutDecender = new Rect();
paint.getTextBounds(string, 0, string.length(), bounds);
paint.getTextBounds(stringWithoutDecender, 0, stringWithoutDecender.length(), boundsForStringWithoutDecender);
return bounds.height() - boundsForStringWithoutDecender.height();
}
然而,我的代码气味是它们不够好。有更好更快的方法吗?你应该看看。下降成员将为您提供“单间隔文本低于基线的建议距离”。实际上,我正在寻找相同的功能。事实证明有更简单的方法, 你甚至不需要单独的函数 如果只对给定字符串调用getTextBounds(),则返回的边界框将已经包含该信息 例如:
paint.getTextBounds(exampleString1 , 0, exampleString1.length(), bounds);
if (bounds.bottom > 0) Log.i("Test", "String HAS descender");
else Log.i("Test", "String DOES NOT HAVE descender");
简单地说bounds.top告诉你字符串的上升(它有负值,因为Y轴0点在字符串的基线上)和bounds.bottom告诉你字符串的下降(下降的字符串可以是0或正值)。如何找到,给定字符串中是否有非零下降的字符。对不起,我读得太快了。您的代码中可能有最好的解决方案。这里发生了一些有趣的事情。当我用它来测试字符串“abcd”时,得到的
bounds.bottom
rect是1。当我用“abcp”测试时,底部是3。所以总是有一个后裔?除等待外,如果我使用“A”或“AB”进行测试,则下降值为0。“ABC”是1。“a”,“ab”是1,“h”是0。似乎很多角色都有一个非零的后代。。。。