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).
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?
Thanks.
Here is something to get you started with, the following component measures the text to fill the screen and the addText
method returns the text which was not fitted.
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;
}
}