0

运行我的应用程序时,我遇到了一个奇怪的问题。
我有一个将段落分成几行的代码。当运行它而不在每行代码下方打印日志时,我的代码运行完全错误。它在尚未达到行宽时换行,创建如下段落:

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;
}
4

0 回答 0