Java Code Examples for android.text.StaticLayout#getOffsetForHorizontal()

The following examples show how to use android.text.StaticLayout#getOffsetForHorizontal() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TextSelectionHelper.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
@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;
}
 
Example 2
Source File: TextSelectionHelper.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
@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;
}