实现一个小说阅读器,首先要把小说内容一页一页的显示在手机界面上,那么问题就来了,如何计算一个页面所能容纳的字数呢?
在这里小星介绍一个基于TextView的实现方法,废话不多说,我们直接上源码:
class ReadingTextView extends TextView {
public ReadingTextView(Context context) {
super(context);
}
public ReadingTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ReadingTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public int getEstimatedLength() {
int height = getHeight();
int lineHeight = getLineHeight();
int linecount = height / lineHeight;
float textSize = getTextSize();
float linewords = getWidth() / textSize;
return (int) (linecount * linewords);
}
}
计算公式&