当字符串太大时,有没有办法避免Android文本视图混乱?

当字符串太大时,有没有办法避免Android文本视图混乱?,android,textview,Android,Textview,我一直在开发一款电子阅读器,它使用安卓TextView在屏幕上标出epub的章节。当章节少于2300个字符(不常见)时,这非常有效,但当章节较长时,我的文本开始混乱。结果如下所示:。在这里,您可以看到我的字符串的开头与字符串的中间重叠 import android.content.Context; import android.graphics.Rect; import android.text.TextPaint; import android.util.AttributeSet; impor

我一直在开发一款电子阅读器,它使用安卓
TextView
在屏幕上标出epub的章节。当章节少于2300个字符(不常见)时,这非常有效,但当章节较长时,我的文本开始混乱。结果如下所示:。在这里,您可以看到我的字符串的开头与字符串的中间重叠

import android.content.Context;
import android.graphics.Rect;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.animation.LinearInterpolator;
import android.widget.Scroller;
import android.widget.TextView;

public class ScrollTextView extends TextView {

    // scrolling feature
    private Scroller mSlr;

    // milliseconds for a round of scrolling
    private int mRndDuration = 250;

    // the X offset when paused
    private int mXPaused = 0;

    // whether it's being paused
    private boolean mPaused = true;

    /*
     * constructor
     */
    public ScrollTextView(Context context) {
        this(context, null);
    }

    /*
     * constructor
     */
    public ScrollTextView(Context context, AttributeSet attrs) {
        this(context, attrs, android.R.attr.textViewStyle);
    }

    /*
     * constructor
     */
    public ScrollTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // customize the TextView
        setSingleLine();
        setEllipsize(null);
        setVisibility(INVISIBLE);
    }

    /**
     * begin to scroll the text from the original position
     */
    public void startScroll() {
        // begin from the very right side
        mXPaused = -1 * getWidth();
        // assume it's paused
        mPaused = true;
        resumeScroll();
    }

    /**
     * resume the scroll from the pausing point
     */
    public void resumeScroll() {

        if (!mPaused) {
            return;
        }

        // Do not know why it would not scroll sometimes
        // if setHorizontallyScrolling is called in constructor.
        setHorizontallyScrolling(true);

        // use LinearInterpolator for steady scrolling
        mSlr = new Scroller(this.getContext(), new LinearInterpolator());
        setScroller(mSlr);

        int scrollingLen = calculateScrollingLen();
        int distance = scrollingLen - (getWidth() + mXPaused);
        int duration = (new Double(mRndDuration * distance * 1.00000
                / scrollingLen)).intValue();

        setVisibility(VISIBLE);
        mSlr.startScroll(mXPaused, 0, distance, 0, duration);
        mPaused = false;
    }

    /**
     * calculate the scrolling length of the text in pixel
     *
     * @return the scrolling length in pixels
     */
    private int calculateScrollingLen() {
        TextPaint tp = getPaint();
        Rect rect = new Rect();
        String strTxt = getText().toString();
        tp.getTextBounds(strTxt, 0, strTxt.length(), rect);
        int scrollingLen = rect.width() + getWidth();
        rect = null;
        return scrollingLen;
    }

    /**
     * pause scrolling the text
     */
    public void pauseScroll() {
        if (null == mSlr) {
            return;
        }

        if (mPaused) {
            return;
        }

        mPaused = true;

        // abortAnimation sets the current X to be the final X,
        // and sets isFinished to be true
        // so current position shall be saved
        mXPaused = mSlr.getCurrX();

        mSlr.abortAnimation();
    }

    /*
     * override the computeScroll to restart scrolling when finished so as that
     * the text is scrolled forever
     */
    @Override
    public void computeScroll() {
        super.computeScroll();

        if (null == mSlr) {
            return;
        }

        if (mSlr.isFinished() && (!mPaused)) {
            this.startScroll();
        }
    }

    public int getRndDuration() {
        return mRndDuration;
    }

    public void setRndDuration(int duration) {
        this.mRndDuration = duration;
    }

    public boolean isPaused() {
        return mPaused;
    }
}
如有任何帮助或见解,将不胜感激。我很好奇这是否只是Android TextView的一个糟糕的使用,也许我应该用OpenGL来代替

更新1:增加文本大小会使加扰更快开始。减少它会产生相反的效果

Update2:当字体为32pt时,字符串在滚动约2300个字符后加扰


更新3:为了澄清为什么不在
滚动视图中使用
TextView
-

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Your long text goes here..." />
</ScrollView>

为什么不在
滚动视图中使用
TextView
-

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Your long text goes here..." />
</ScrollView>

你可以增加文本视图的宽度你可以将文本视图放在滚动视图中,不是吗?@jiteshmohite你是指布局宽度参数还是将maxwidth设置为更大的值?你可以增加文本视图的宽度你可以将文本视图放在滚动视图中,这不管用吗?@jiteshmohite您是指布局宽度参数还是将maxwidth设置为更大的值?我刚刚实现了您的建议,但不幸的是,这种行为仍然存在。谢谢你的主意。@Dustin它对我有用。你可以尝试完全替换你的代码。如果您需要任何特殊的财产,请提出来。如果您想水平滚动此文本,可以在我的解决方案中尝试“水平滚动视图”而不是“滚动视图”。谢谢您的跟进。我刚刚按照您的建议尝试了HorizontalScrollView,但问题仍然存在。当我开始自动滚动时,文本看起来很棒。正是在书的后面一章,正文开始变得杂乱无章。我刚刚在原始帖子的编辑中添加了一段行为视频。它是真实的设备还是模拟器?如果是在emulator中,我建议您在真实设备中测试它。还可以尝试在不同的API级别了解它是否是特定于设备的问题。我一直在使用真实的设备进行开发。今晚我将研究不同的API级别,并向您报告。谢谢你的建议。我刚刚实施了你的建议,不幸的是,这种行为仍然存在。谢谢你的主意。@Dustin它对我有用。你可以尝试完全替换你的代码。如果您需要任何特殊的财产,请提出来。如果您想水平滚动此文本,可以在我的解决方案中尝试“水平滚动视图”而不是“滚动视图”。谢谢您的跟进。我刚刚按照您的建议尝试了HorizontalScrollView,但问题仍然存在。当我开始自动滚动时,文本看起来很棒。正是在书的后面一章,正文开始变得杂乱无章。我刚刚在原始帖子的编辑中添加了一段行为视频。它是真实的设备还是模拟器?如果是在emulator中,我建议您在真实设备中测试它。还可以尝试在不同的API级别了解它是否是特定于设备的问题。我一直在使用真实的设备进行开发。今晚我将研究不同的API级别,并向您报告。谢谢你的建议。