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