我使用StaticLayout来分隔我的Html文本。 但是当我在StaticLayout中使用大文本构造过程非常繁重并且花费了大量时间。 它只是在android 5下面的机器人中发生的 我该如何解决这个问题? 这是我的代码
public class PageSplitter {
private final int pageWidth;
private final int pageHeight;
private final float lineSpacingMultiplier;
private final float lineSpacingExtra;
private final List<CharSequence> pages = new ArrayList<CharSequence>();
private SpannableStringBuilder mSpannableStringBuilder;
public PageSplitter(int pageWidth, int pageHeight, float lineSpacingMultiplier, float lineSpacingExtra) {
this.pageWidth = pageWidth;
this.pageHeight = pageHeight;
this.lineSpacingMultiplier = lineSpacingMultiplier;
this.lineSpacingExtra = lineSpacingExtra;
}
public void append(CharSequence charSequence) {
this.mSpannableStringBuilder.append(charSequence);
}
public void split(TextPaint textPaint) {
StaticLayout staticLayout = new StaticLayout(
mSpannableStringBuilder,
textPaint,
pageWidth,
Layout.Alignment.ALIGN_NORMAL,
lineSpacingMultiplier,
lineSpacingExtra,
false
);
int startLine = 0;
while(startLine < staticLayout.getLineCount()) {
int startLineTop = staticLayout.getLineTop(startLine);
int endLine = staticLayout.getLineForVertical(startLineTop + pageHeight);
int endLineBottom = staticLayout.getLineBottom(endLine);
int lastFullyVisibleLine;
if(endLineBottom > startLineTop + pageHeight)
lastFullyVisibleLine = endLine - 1;
else
lastFullyVisibleLine = endLine;
int startOffset = staticLayout.getLineStart(startLine);
int endOffset = staticLayout.getLineEnd(lastFullyVisibleLine);
pages.add(mSpannableStringBuilder.subSequence(startOffset, endOffset));
startLine = lastFullyVisibleLine + 1;
}
}
public List<CharSequence> getPages() {
return pages;
}
}
我在这里用它
private void initialViewPager(float textSize, float lineSpace){
TextPaint textPaint = new TextPaint();
float textSizeInPx = UnitConverter.spToPixels(this, textSize);
textPaint.setTextSize(textSizeInPx);
ru.appheads.pagesplitterapp.garbage.PageSplitter splitter = new ru.appheads.pagesplitterapp.garbage.PageSplitter(pageWidth,pageHeight,1,0);
for (String bookTopicText : bookTopicTexts) {
splitter.append( bookTopicText);
}
splitter.split(textPaint);
TextPagerAdapter textPagerAdapter = new TextPagerAdapter(getSupportFragmentManager(), splitter.getPages(), textSize, lineSpace, pageHeight);
pageSeekbar.setMax(textPagerAdapter.getCount()-1);
pagesView.setAdapter(textPagerAdapter);
}
我调试了我的代码,我知道这部分代码是我的问题:
StaticLayout staticLayout = new StaticLayout(
mSpannableStringBuilder,
textPaint,
pageWidth,
Layout.Alignment.ALIGN_NORMAL,
lineSpacingMultiplier,
lineSpacingExtra,
false
);