下面列出了android.text.Layout#getLineAscent ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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;
}
/**
* @param line - current line
* @param column - column of line
* @return Position (in pixels) for edittext at line and column
*/
public Point getDebugPosition(int line, int column, int gravity) {
Layout layout = getLayout();
if (layout != null) {
int pos = layout.getLineStart(line) + column;
int baseline = layout.getLineBaseline(line);
int ascent = layout.getLineAscent(line);
int offsetHorizontal = (int) layout.getPrimaryHorizontal(pos) + mLinePadding; //x
float y;
int offsetVertical = 0;
if (gravity == Gravity.BOTTOM) {
y = baseline + ascent;
if (verticalScroll != null) {
offsetVertical = (int) ((y + mCharHeight) - verticalScroll.getScrollY());
} else {
offsetVertical = (int) ((y + mCharHeight) - getScrollY());
}
return new Point(offsetHorizontal, offsetVertical);
} else if (gravity == Gravity.TOP) {
y = layout.getLineTop(line);
if (verticalScroll != null) {
offsetVertical = (int) (y - verticalScroll.getScrollY());
} else {
offsetVertical = (int) (y - getScrollY());
}
return new Point(offsetHorizontal, offsetVertical);
}
return new Point(offsetHorizontal, offsetVertical);
}
return new Point();
}
@Override
public void onPopupChangePosition() {
try {
Layout layout = getLayout();
if (layout != null) {
int pos = getSelectionStart();
int line = layout.getLineForOffset(pos);
int baseline = layout.getLineBaseline(line);
int ascent = layout.getLineAscent(line);
float x = layout.getPrimaryHorizontal(pos);
float y = baseline + ascent;
int offsetHorizontal = (int) x + mLinePadding;
setDropDownHorizontalOffset(offsetHorizontal);
int heightVisible = getHeightVisible();
int offsetVertical = 0;
if (verticalScroll != null) {
offsetVertical = (int) ((y + mCharHeight) - verticalScroll.getScrollY());
} else {
offsetVertical = (int) ((y + mCharHeight) - getScrollY());
}
int tmp = offsetVertical + getDropDownHeight() + mCharHeight;
if (tmp < heightVisible) {
tmp = offsetVertical + mCharHeight / 2;
setDropDownVerticalOffset(tmp);
} else {
tmp = offsetVertical - getDropDownHeight() - mCharHeight;
setDropDownVerticalOffset(tmp);
}
}
} catch (Exception ignored) {
}
}
protected void onPopupChangePosition() {
try {
Layout layout = getLayout();
if (layout != null) {
int pos = getSelectionStart();
int line = layout.getLineForOffset(pos);
int baseline = layout.getLineBaseline(line);
int ascent = layout.getLineAscent(line);
Rect bounds = new Rect();
Paint textPaint = getPaint();
String sample="A";
textPaint.getTextBounds(sample, 0, sample.length(), bounds);
int width = bounds.width()/sample.length();
float x = layout.getPrimaryHorizontal(pos);
float y = baseline + ascent;
int offsetHorizontal = (int) x + mGutterWidth;
setDropDownHorizontalOffset(offsetHorizontal);
int heightVisible = getHeightVisible();
int offsetVertical = (int) ((y + mCharHeight) - getScrollY());
int tmp = offsetVertical + getDropDownHeight() + mCharHeight;
//if (tmp < heightVisible) {
tmp = -h + ((offsetVertical*2 / (mCharHeight)) * (mCharHeight / 2))+(mCharHeight/2);
setDropDownVerticalOffset(tmp);
//((Activity)(mContext)).setTitle("ov :"+offsetVertical +" ch "+mCharHeight+" tmp"+tmp +"h "+h+"p:"+pos);
// } else {
// tmp = offsetVertical - getDropDownHeight() - mCharHeight;
// setDropDownVerticalOffset(tmp);
// ((Activity)(mContext)).setTitle(" 2 tmp :"+tmp);
// }
// int pos = getSelectionStart();
// int line = layout.getLineForOffset(pos);
// int baseline = layout.getLineBaseline(line);
// int ascent = layout.getLineAscent(line);
//
// float x = layout.getPrimaryHorizontal(pos);
// float y = baseline + ascent;
//
// int offsetHorizontal = (int) x + mGutterWidth;
// setDropDownHorizontalOffset(offsetHorizontal);
//
// // int heightVisible = getHeightVisible();
// int offsetVertical = (int) ((y + mCharHeight) - getScrollY());
//
// int tmp = offsetVertical + getDropDownHeight() + mCharHeight;
//// if (tmp < heightVisible) {
// tmp = -(offsetVertical + mCharHeight) + ((offsetVertical / mCharHeight) * (mCharHeight / 2));
// setDropDownVerticalOffset(tmp);
//// } else {
//// tmp = offsetVertical - getDropDownHeight() - mCharHeight;
//// setDropDownVerticalOffset(tmp);
//// }
}
} catch (Exception e) {
Logger.error(TAG, e);
}
}