Android TextViewは、ある文字の座標を取得します.

882 ワード

TextViewでは、Layoutによって文字が管理される位置
/**
     *   TextView        
     *
     * @parms tv
     * @parms index     
     * @return         
     */
    private int getTextViewSelectionBottomY(TextView tv,int index) {
        Layout layout = tv.getLayout();
        Rect bound = new Rect();
        int line = layout.getLineForOffset(index);
        layout.getLineBounds(line, bound);
        int yAxisBottom = bound.bottom;//    y  
//        int yAxisTop = bound.top;//    y  
//        float xAxisLeft = layout.getPrimaryHorizontal(index);//    x  
//        float xAxisRight = layout.getSecondaryHorizontal(index);//    x  

        return yAxisBottom;

    }


なお、取得されたこの座標はTextViewの相対座標である.TextView.getScrollY()によってtextviewに示されている内容の一番上の行の座標を取得することができ、そうすると現在の文字列のスクリーン上の座標位置を算出することができます.