如何与多个文本视图和图像视图内容闯入页面,而不是使用Android中滚动视图 [英] How a content with multiple text views and image views break into pages instead of using a scroll view in android

查看:116
本文介绍了如何与多个文本视图和图像视图内容闯入页面,而不是使用Android中滚动视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我采取的Andr​​oid EPUB阅读器。我已经成功地读取EPUB文件,得到了内容为动态t​​extviews和imageviews。 所以,内容其实是与多个textviews和imageviews组成。

I am implementing an epub reader for android. I have successfully read the epub files and got the content to dynamic textviews and imageviews. So the content is actually made up with multiple textviews and imageviews.

目前我使用了一个滚动视图来读的书。现在我需要打破的网页全部内容(页面的内容应该根据屏幕尺寸)。

Currently I have used a scrollview to read the book. Now I need to break the whole content in to pages(content of a page should based on the screen size).

有关此我想到用ViewPager的。所以它可以用于容易页面之间滑动。但如果你有其他比我的解决方案将是一个很大的帮助。

For this I am thinking of using ViewPager. So it can be used to swipe between pages easily. But if you have solutions for other than that I will be a great help.

谁能帮助实现这一目标?

can anyone help to achieve this?

感谢。

推荐答案

下面是一些让你开始使用,下列组件措施的文本来填充屏幕和 addText 方法将返回这是不适合的文本。

Here is something to get you started with, the following component measures the text to fill the screen and the addTextmethod returns the text which was not fitted.

ImageView的结合测量身高,所以我只是做它用文字工作增加了更多的复杂性。你也可以调整值将被删除多少个字符来获得更快的迭代,并检查是否的可能性,这可能是与行更有效的进行计数本着高度结合起来。

Combining with ImageView adds more complexity for measuring the height so I just made it to work with text. You could also adjust the values how many characters are removed to get iteration faster and also check for possibilities if this could be done more efficiently with line count combined with line height.

请注意,该组件希望的长字符串,它会子串什么留给下一个页面。

Notice that the component wants a long string which it will substring to whats left for next page.

public class ReaderLinearLayout extends LinearLayout {

    private static final int TEXT_SIZE = 44;
    private final DisplayMetrics dm = new DisplayMetrics();

    public ReaderLinearLayout(Context context) {
        super(context);
    }

    public ReaderLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public String addText(String text) {
        int height = getScreenHeight();

        String textLeftFromEnd = text;

        int textHeight = getTextHeight(text);

        while (textHeight > height) {
            text = text.substring(0, text.length() - 1);
            textHeight = getTextHeight(text);
        }

        TextView textView = new TextView(getContext());
        textView.setText(text);
        textView.setTextColor(Color.BLACK);
        textView.setTextSize(TEXT_SIZE);
        addView(textView);

        return textLeftFromEnd.subSequence(text.length(),
                textLeftFromEnd.length()).toString();
    }

    public int getTextHeight(final String text) {
        TextView textView = new TextView(getContext());
        textView.setText(text);
        textView.setTextSize(TEXT_SIZE);
        TextPaint textPaint = textView.getPaint();

        return new StaticLayout(text.toString(), textPaint, getScreenWidth(),
                Alignment.ALIGN_NORMAL, 1.0f, 0.0f, true).getHeight();
    }

    public int getScreenHeight() {
        WindowManager wm = (WindowManager) getContext().getSystemService(
                Context.WINDOW_SERVICE);
        wm.getDefaultDisplay().getMetrics(dm);

        return dm.heightPixels;
    }

    public int getScreenWidth() {
        WindowManager wm = (WindowManager) getContext().getSystemService(
                Context.WINDOW_SERVICE);
        wm.getDefaultDisplay().getMetrics(dm);

        return dm.widthPixels;
    }
}

这篇关于如何与多个文本视图和图像视图内容闯入页面,而不是使用Android中滚动视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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