问题
I use StaticLayout to Paginate my Html Text. But when I use large text in StaticLayout Constructing process is very heavy and taking a lot of time. It's Just Happen in androids below android 5 How can I resolve this problem? this is my code
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;
}
}
And I used it here
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);
}
I debug my code and I know this part of code is my problem :
StaticLayout staticLayout = new StaticLayout(
mSpannableStringBuilder,
textPaint,
pageWidth,
Layout.Alignment.ALIGN_NORMAL,
lineSpacingMultiplier,
lineSpacingExtra,
false
);
来源:https://stackoverflow.com/questions/44017361/staticlayout-too-much-time-taking