下面列出了android.text.Layout#getHeight ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void initOffsetHeight() {
int paddingTop;
int paddingBottom;
int mHeight;
int mLayoutHeight;
//获得内容面板
Layout mLayout = getLayout();
if (mLayout == null) return;
//获得内容面板的高度
mLayoutHeight = mLayout.getHeight();
//获取上内边距
paddingTop = getTotalPaddingTop();
//获取下内边距
paddingBottom = getTotalPaddingBottom();
//获得控件的实际高度
mHeight = getMeasuredHeight();
//计算滑动距离的边界
mOffsetHeight = mLayoutHeight + paddingTop + paddingBottom - mHeight;
if (mOffsetHeight <= 0) {
scrollTo(0, 0);
}
}
private void initOffsetHeight() {
int paddingTop;
int paddingBottom;
int mHeight;
int mLayoutHeight;
//获得内容面板
Layout mLayout = getLayout();
if (mLayout == null) return;
//获得内容面板的高度
mLayoutHeight = mLayout.getHeight();
//获取上内边距
paddingTop = getTotalPaddingTop();
//获取下内边距
paddingBottom = getTotalPaddingBottom();
//获得控件的实际高度
mHeight = getMeasuredHeight();
//计算滑动距离的边界
mOffsetHeight = mLayoutHeight + paddingTop + paddingBottom - mHeight;
if (mOffsetHeight <= 0) {
scrollTo(0, 0);
}
}
/**
* Flush view no matter what height and width the {@link WXDomObject} specifies.
* @param extra must be a {@link Layout} object, otherwise, nothing will happen.
*/
private void flushView(Object extra) {
if (extra instanceof Layout &&
getHostView() != null && !extra.equals(getHostView().getTextLayout())) {
final Layout layout = (Layout) extra;
/**The following if block change the height of the width of the textView.
* other part of the code is the same to updateExtra
*/
ViewGroup.LayoutParams layoutParams = getHostView().getLayoutParams();
if (layoutParams != null) {
layoutParams.height = layout.getHeight();
layoutParams.width = layout.getWidth();
getHostView().setLayoutParams(layoutParams);
}
getHostView().setTextLayout(layout);
getHostView().invalidate();
}
}
/**
* Flush view no matter what height and width the {@link WXDomObject} specifies.
* @param extra must be a {@link Layout} object, otherwise, nothing will happen.
*/
private void flushView(Object extra) {
if (extra instanceof Layout &&
getHostView() != null && !extra.equals(getHostView().getTextLayout())) {
final Layout layout = (Layout) extra;
/**The following if block change the height of the width of the textView.
* other part of the code is the same to updateExtra
*/
ViewGroup.LayoutParams layoutParams = getHostView().getLayoutParams();
if (layoutParams != null) {
layoutParams.height = layout.getHeight();
layoutParams.width = layout.getWidth();
getHostView().setLayoutParams(layoutParams);
}
getHostView().setTextLayout(layout);
getHostView().invalidate();
}
}
/**
* Prior to version 20, If the Layout specifies extra space between lines (either by spacingmult
* or spacingadd) the StaticLayout would erroneously add this space after the last line as well.
* This bug was fixed in version 20. This method calculates the extra space and reduces the height
* by that amount.
*
* @param layout The layout.
* @return The height of the layout.
*/
public static int getHeight(Layout layout) {
if (layout == null) {
return 0;
}
int extra = 0;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT_WATCH
&& layout instanceof StaticLayout) {
int line = Math.max(0, layout.getLineCount() - 1);
int above = layout.getLineAscent(line);
int below = layout.getLineDescent(line);
float originalSize = (below - above - layout.getSpacingAdd()) / layout.getSpacingMultiplier();
float ex = below - above - originalSize;
if (ex >= 0) {
extra = (int) (ex + 0.5);
} else {
extra = -(int) (-ex + 0.5);
}
}
return layout.getHeight() - extra;
}
@Override
public int getSize(
@NonNull Paint paint,
CharSequence text,
@IntRange(from = 0) int start,
@IntRange(from = 0) int end,
@Nullable Paint.FontMetricsInt fm) {
// it's our absolute requirement to have width of the canvas here... because, well, it changes
// the way we draw text. So, if we do not know the width of canvas we cannot correctly measure our text
if (layouts.size() > 0) {
if (fm != null) {
int max = 0;
for (Layout layout : layouts) {
final int height = layout.getHeight();
if (height > max) {
max = height;
}
}
// we store actual height
height = max;
// but apply height with padding
final int padding = theme.tableCellPadding() * 2;
fm.ascent = -(max + padding);
fm.descent = 0;
fm.top = fm.ascent;
fm.bottom = 0;
}
}
return width;
}
private MultiLineTexture(Layout layout) {
super(layout.getWidth(), layout.getHeight());
mLayout = layout;
}