Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

阅读本地txt文件时,有可能出现乱码。 #36

Closed
brook19 opened this issue Oct 14, 2016 · 7 comments
Closed

阅读本地txt文件时,有可能出现乱码。 #36

brook19 opened this issue Oct 14, 2016 · 7 comments

Comments

@brook19
Copy link

brook19 commented Oct 14, 2016

txt文件内容可能是GBK、UTF-8、Unicode等不同的编码。
PageFactory中,读取文件等相关方法中默认使用的UTF-8编码。

/**
* 指针移到上一页页首
*/
private void pageUp() {
String strParagraph = "";
Vector lines = new Vector<>(); // 页面行
int paraSpace = 0;
mPageLineCount = mVisibleHeight / (mFontSize + mLineSpace);
while ((lines.size() < mPageLineCount) && (m_mbBufBeginPos > 0)) {
Vector paraLines = new Vector<>(); // 段落行
byte[] parabuffer = readParagraphBack(m_mbBufBeginPos); // 1.读取上一个段落

        m_mbBufBeginPos -= parabuffer.length; // 2.变换起始位置指针
        try {
            strParagraph = new String(parabuffer, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        strParagraph = strParagraph.replaceAll("\r\n", "  ");
        strParagraph = strParagraph.replaceAll("\n", " ");

        while (strParagraph.length() > 0) { // 3.逐行添加到lines
            int paintSize = mPaint.breakText(strParagraph, true, mVisibleWidth, null);
            paraLines.add(strParagraph.substring(0, paintSize));
            strParagraph = strParagraph.substring(paintSize);
        }
        lines.addAll(0, paraLines);

        while (lines.size() > mPageLineCount) { // 4.如果段落添加完,但是超出一页,则超出部分需删减
            try {
                m_mbBufBeginPos += lines.get(0).getBytes("UTF-8").length; // 5.删减行数同时起始位置指针也要跟着偏移
                lines.remove(0);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        m_mbBufEndPos = m_mbBufBeginPos; // 6.最后结束指针指向下一段的开始处
        paraSpace += mLineSpace;
        mPageLineCount = (mVisibleHeight - paraSpace) / (mFontSize + mLineSpace); // 添加段落间距,实时更新行数
    }
}
@smuyyh
Copy link
Owner

smuyyh commented Oct 14, 2016

恩。目前确实会有这个问题 正在修复

@smuyyh
Copy link
Owner

smuyyh commented Oct 19, 2016

@brook19 解决了,有空的时候麻烦帮忙再试下会不会乱码 我这边暂时没问题,支持GBK与UTF-8

@smuyyh smuyyh closed this as completed Oct 19, 2016
@brook19
Copy link
Author

brook19 commented Oct 19, 2016

可以了。BTW, 翻书效果仿真模式怎么没有了,应该可以切换。

@smuyyh
Copy link
Owner

smuyyh commented Oct 19, 2016

设置里面可以切换

@brook19
Copy link
Author

brook19 commented Oct 19, 2016

恩,不错。滑动翻页效果如果可以再优化下就更好了

@smuyyh
Copy link
Owner

smuyyh commented Oct 19, 2016

恩呐 尽量~

@noear
Copy link

noear commented Jun 26, 2019

超赞。。。有没有QQ群啊,加起来交流一下:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants