打印日志和不打印日志之间发生了奇怪的事情

时间:2012-08-27 03:32:38

标签: android eclipse

运行我的应用程序时,我遇到了一个奇怪的问题 我有一个代码将段落分成几行。在没有在每行代码下面打印日志的情况下运行它时,我的代码运行完全错误。它还没有达到行的宽度时断行,创建如下的段落:

Lorem ipsum dolor 
sit amet, consectetur adipisicing elit, 
sed do eiusmod tempor incididunt ut labore
et dolore magna aliqua. 
Ut enim ad minim veniam, quis nostrud.

但是当我将日志插入每行代码的打印结果时,我的代码几乎完全像:

Lorem ipsum dolor sit amet, consectetur 
adipisicing elit, sed do eiusmod tempor 
incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud.

我的日志仅用于在每个变量中打印值,它们不计算任何内容。

我的问题是:你们有没有这样的事情,在运行应用程序时打印日志与不打印日志之间的结果不同?为什么会这样?怎么解决?

谢谢。

更新

下面是我将代码拆分成行的代码。使用boolean log = false时,会返回错误的结果。

public static ArrayList<Row> getRows(int displayWidth, String text, TextStyle style) {
    // Split to words
    ArrayList<String> punctuations = new ArrayList<String>();
    ArrayList<Row> rows = new ArrayList<Row>();

    boolean log = true;
    text = text.replace("\\r\\n", "");

    ArrayList<String> words = new ArrayList<String>();

    Pattern p = Pattern.compile(REGEX);
    Matcher m = p.matcher(text);

    int lastCutIndex = 0;
    while (m.find()) {
        words.add(text.substring(lastCutIndex, m.start()));

        if (m.end() <= text.length()) {
            punctuations.add(text.substring(m.start(), m.end()));
        }

        lastCutIndex = m.end();
    }

    if (lastCutIndex < text.length()) words.add(text.substring(lastCutIndex, text.length()));

    // Fill words into row
    int full = getSize("a b", style)[0];
    int less = getSize("ab", style)[0];
    int spaceCharWidth = full - less;

    int currentRow = 0;

    rows.add(new Row(style.getTypeface(), style.getTextSize()));

    for (int i = 0; i < words.size(); i++) {
        int[] rowSize = getSize(rows.get(currentRow).getText(), style); 
        int rowWidth = rowSize[0];
        int rowHeight = rowSize[1];

        rows.get(currentRow).lineHeight = 
                FastMath.round(rowHeight * style.line_mult + style.line_add);

        String word = words.get(i);
        String punc = "";
        if (i < punctuations.size()) punc = punctuations.get(i);

        if (log) {
            Logger.e(TAG, "word:" + word + ",punc:" + punc + "|");
        }

        int puncWidth = 0;
        if (punc.equals(" ")) puncWidth = spaceCharWidth;
        else puncWidth = getSize(punc, style)[0];

        int nextWordWidth = getSize(word, style)[0];

        if (log) {
            Logger.e(TAG, "nexWordWidth:" + nextWordWidth);
        }

        int predictWidth = FastMath.round(rowWidth + nextWordWidth 
                + (rows.get(currentRow).elementCount - 1) * WIDTH_ADD_RATIO);

        if (log) {
            Logger.e(TAG, "predictWidth:" + predictWidth);
        }

        if (punc.equals(" ")) {
            if (log) {
                Logger.e(TAG, "predict is < displayWidth?" + ((predictWidth < displayWidth) ? true : false));
            }

            if (predictWidth < displayWidth) {
                rows.get(currentRow).append(word);

                if (predictWidth + puncWidth < displayWidth) {
                    rows.get(currentRow).append(punc);
                }

                if (log) {
                    Logger.e(TAG, "rows[" + currentRow + "]:" + rows.get(currentRow).getText());
                }

            } else {
                currentRow++;

                if (log) {
                    Logger.e(TAG, "new row");
                }

                Row row = new Row(style.getTypeface(), style.getTextSize());
                row.append(word);

                if (word.equals("")) {
                    if (!punc.equals(" ")) {
                        row.append(punc);
                    }
                } else {
                    row.append(punc);
                }

                rows.add(row);

                if (log) {
                    Logger.e(TAG, "rows[" + currentRow + "]:" + rows.get(currentRow).getText());
                }
            }
        } else {
            if (log) {
                Logger.e(TAG, "predict + puncWidth is < displayWidth?" + ((predictWidth < displayWidth) ? true : false));
            }

            if (predictWidth + puncWidth < displayWidth) {
                rows.get(currentRow).append(word);
                rows.get(currentRow).append(punc);

                if (log) {
                    Logger.e(TAG, "rows[" + currentRow + "]:" + rows.get(currentRow).getText());
                }

            } else {
                currentRow++;
                if (log) {
                    Logger.e(TAG, "new row");
                }

                Row row = new Row(style.getTypeface(), style.getTextSize());
                row.append(word);

                if (word.equals("")) {
                    if (!punc.equals(" ")) {
                        row.append(punc);
                    }
                } else {
                    row.append(punc);
                }

                rows.add(row);
                if (log) {
                    Logger.e(TAG, "rows[" + currentRow + "]:" + rows.get(currentRow).getText());
                }
            }
        }
    }

    return rows;
}

public static int[] getSize(String text, TextStyle style) {
    if (text == null) return new int[2];

    Paint paint = new Paint();
    paint.setTextSize(style.getTextSize());
    paint.setTypeface(style.getTypeface());
    Rect rect = new Rect();
    paint.getTextBounds(text.toCharArray(), 0, text.length(), rect);

    int[] result = new int[2];
    result[0] = rect.width();
    result[1] = rect.height();
    return result;
}

0 个答案:

没有答案