获取TextView 字符所在位置参考

    public static void getRect(Rect parentTextViewRect, TextView parentTextView, int start, int end){
        // Initialize values for the computing of clickedText position
        SpannableString completeText = (SpannableString)(parentTextView).getText();
        Layout textViewLayout = parentTextView.getLayout();

        double startOffsetOfClickedText = start;
        double endOffsetOfClickedText = end;
       
        //获取startOffsetOfClickedText对应下标对应的字符x坐标
        double startXCoordinatesOfClickedText = textViewLayout.getPrimaryHorizontal((int)startOffsetOfClickedText);
      //获取endOffsetOfClickedText对应下标对应的字符x坐标
        double endXCoordinatesOfClickedText = textViewLayout.getPrimaryHorizontal((int)endOffsetOfClickedText);
        //获取startOffsetOfClickedText对应下标对应的字符属于第几行
        int currentLineStartOffset = textViewLayout.getLineForOffset((int)startOffsetOfClickedText);
        //获取startOffsetOfClickedText对应下标对应的字符属于第几行
        int currentLineEndOffset = textViewLayout.getLineForOffset((int)endOffsetOfClickedText);
        boolean keywordIsInMultiLine = currentLineStartOffset != currentLineEndOffset;
        //获取字符所在的那一行的矩形框的信息 存储在parentTextViewRect
        textViewLayout.getLineBounds(currentLineStartOffset, parentTextViewRect);


        int[] parentTextViewLocation = {0,0};
        parentTextView.getLocationOnScreen(parentTextViewLocation);

        double parentTextViewTopAndBottomOffset = (
                parentTextViewLocation[1] -
                        parentTextView.getScrollY() +
                        parentTextView.getCompoundPaddingTop()
        );
        parentTextViewRect.top += parentTextViewTopAndBottomOffset;
        parentTextViewRect.bottom += parentTextViewTopAndBottomOffset;

        // 这里区分开始字符跟结束字符是否是在同一行,并技术对应字符的坐标
        if (keywordIsInMultiLine){

            int screenHeight = ClientN.height();
            int dyTop = parentTextViewRect.top;
            int dyBottom = screenHeight - parentTextViewRect.bottom;
            boolean onTop = dyTop > dyBottom;

            if (onTop){
                endXCoordinatesOfClickedText = textViewLayout.getLineRight(currentLineStartOffset);
            }
            else{
                parentTextViewRect = new Rect();
                textViewLayout.getLineBounds(currentLineEndOffset, parentTextViewRect);
                parentTextViewRect.top += parentTextViewTopAndBottomOffset;
                parentTextViewRect.bottom += parentTextViewTopAndBottomOffset;
                startXCoordinatesOfClickedText = textViewLayout.getLineLeft(currentLineEndOffset);
            }

        }

        parentTextViewRect.left += (
                parentTextViewLocation[0] +
                        startXCoordinatesOfClickedText +
                        parentTextView.getCompoundPaddingLeft() -
                        parentTextView.getScrollX()
        );
        parentTextViewRect.right = (int) (
                parentTextViewRect.left +
                        endXCoordinatesOfClickedText -
                        startXCoordinatesOfClickedText
        );
    }

你可能感兴趣的:(获取TextView 字符所在位置参考)