我正在使用一个引擎(AndEngine)中的方法:
public final void setText(String pString){...}
我的应用程序每1秒钟从静态整型变量更新得分。
mScoreText.setText(""+PlayerSystem.mScore);
问题在于每秒钟都会创建一个新的字符串对象,一分钟后我需要收集59个字符串对象以及额外的AbstractStringBuilders和init...。我在andengine论坛上找到了部分解决方案,如下所示:
private static StringBuilder mScoreValue = new StringBuilder("000000");
private static final char[] DIGITS = {'0','1','2','3','4','5','6','7','8','9'};
mScoreValue.setCharAt(0, DIGITS[(PlayerSystem.mScore% 1000000) / 100000]);
mScoreValue.setCharAt(1, DIGITS[(PlayerSystem.mScore% 100000) / 10000]);
mScoreValue.setCharAt(2, DIGITS[(PlayerSystem.mScore% 10000) / 1000]);
mScoreValue.setCharAt(3, DIGITS[(PlayerSystem.mScore% 1000) / 100]);
mScoreValue.setCharAt(4, DIGITS[(PlayerSystem.mScore% 100) / 10]);
mScoreValue.setCharAt(5, DIGITS[(PlayerSystem.mScore% 10)]);
mScoreText.setText(mScoreValue.toString());
但主要问题仍然存在,每次调用.toString()都会返回一个新对象。有没有办法解决这个问题?