计算文本的宽度

/**
     * 方法1:获取文本的宽度
     * @param paint
     * @param str
     * @return
     */
    public static int getTextWidth(Paint paint, String str) {
        int iRet = 0;
        if (str != null && str.length() > 0) {
            int len = str.length();
            float[] widths = new float[len];
            paint.getTextWidths(str, widths);
            for (int j = 0; j < len; j++) {
                iRet += (int) Math.ceil(widths[j]);
            }
        }
        return iRet;
    }

 

//方法2
int textWidth = (int) mTextPaint.measureText(mText, 0, mText.length());

 

posted @ 2017-06-15 14:59  ha_cjy  阅读(326)  评论(0编辑  收藏  举报