下面列出了android.text.StaticLayout#getOffsetForHorizontal ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected int getCharOffsetFromCord(int x, int y, int offsetX, int offsetY, ArticleSelectableView view, boolean maybe) {
if (view == null) {
return -1;
}
int line = -1;
x -= offsetX;
y -= offsetY;
arrayList.clear();
view.fillTextLayoutBlocks(arrayList);
int childIndex;
if (maybe) {
childIndex = maybeTextIndex;
} else {
childIndex = startPeek ? startViewChildPosition : endViewChildPosition;
}
StaticLayout layout = arrayList.get(childIndex).getLayout();
if (x < 0) {
x = 1;
}
if (y < 0) {
y = 1;
}
if (x > layout.getWidth()) {
x = layout.getWidth();
}
if (y > layout.getLineBottom(layout.getLineCount() - 1)) {
y = (int) (layout.getLineBottom(layout.getLineCount() - 1) - 1);
}
for (int i = 0; i < layout.getLineCount(); i++) {
if (y > layout.getLineTop(i) && y < layout.getLineBottom(i)) {
line = i;
break;
}
}
if (line >= 0) {
return layout.getOffsetForHorizontal(line, x);
}
return -1;
}
@Override
protected int getCharOffsetFromCord(int x, int y, int offsetX, int offsetY, ArticleSelectableView view, boolean maybe) {
if (view == null) {
return -1;
}
int line = -1;
x -= offsetX;
y -= offsetY;
arrayList.clear();
view.fillTextLayoutBlocks(arrayList);
int childIndex;
if (maybe) {
childIndex = maybeTextIndex;
} else {
childIndex = startPeek ? startViewChildPosition : endViewChildPosition;
}
StaticLayout layout = arrayList.get(childIndex).getLayout();
if (x < 0) {
x = 1;
}
if (y < 0) {
y = 1;
}
if (x > layout.getWidth()) {
x = layout.getWidth();
}
if (y > layout.getLineBottom(layout.getLineCount() - 1)) {
y = (int) (layout.getLineBottom(layout.getLineCount() - 1) - 1);
}
for (int i = 0; i < layout.getLineCount(); i++) {
if (y > layout.getLineTop(i) && y < layout.getLineBottom(i)) {
line = i;
break;
}
}
if (line >= 0) {
return layout.getOffsetForHorizontal(line, x);
}
return -1;
}