アプリケーションの実行中に奇妙な問題に直面しています。
段落を行に分割するコードがあります。コードの各行の下にログを出力せずに実行すると、コードが完全に間違って実行されます。行の幅にまだ達していない場合は改行し、次のような段落を作成します。
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;
}