单独的长文本转化为viewpager页面 [英] Separate long text into pages for viewpager

查看:319
本文介绍了单独的长文本转化为viewpager页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实施messureText方法这个问题在viewpager渲染之前,长文本与指定大小的页面分开。我在做与字符数增量while循环来获得所需的文本块,但它似乎不是最好的解决办法。有没有改善这种计算性能的任何建议?
P / S:我指的是Wattpad应用程序看到它做到这一点非常快,但不知道它是如何

I'm implementing messureText method in this question to separate long text into pages with specified size before rendering in viewpager. I'm doing a while loop with incremental number of characters to get desired text blocks but it seems not to be the best solution. Is there any suggestion to improve the performance for this calculation?. p/s: I refer to the Wattpad app saw it do this very fast but did not know it how?

推荐答案

StaticLayout DynamicLayout 能做到这一点。
Android的使用(镗|静态|动态)布局类来衡量和换行,这些类的构造函数采取的CharSequence作为输入参数等等样式的文本(包含跨度,甚至ImageSpan)是可以接受的。
可以计算出页宽和pageHeight根据您的视图或画面,以及TextPaint和两个lineSpacing参数必须等于你的目标TextView的,这里是我的code:

StaticLayout or DynamicLayout could do this. Android use (Boring|Static|Dynamic)Layout classes to measure and wrap text, these classes constructor take CharSequence as input param so styled text(contains spans, even ImageSpan) is acceptable. You can calculate the pageWidth and pageHeight according to your View or Screen, and the TextPaint and two lineSpacing param must equals to your target TextView, here is my code:

import android.text.Layout;
import android.text.SpannableStringBuilder;
import android.text.StaticLayout;
import android.text.TextPaint;

import java.util.ArrayList;
import java.util.List;

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 = new SpannableStringBuilder();

    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) {
        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;
    }
}

这篇关于单独的长文本转化为viewpager页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆