下面列出了android.text.Layout#getLineBaseline ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected void drawFrame(Canvas canvas) {
Layout layout = mHTextView.getLayout();
int gapIndex = 0;
for (int i = 0; i < layout.getLineCount(); i++) {
int lineStart = layout.getLineStart(i);
int lineEnd = layout.getLineEnd(i);
float lineLeft = layout.getLineLeft(i);
float lineBaseline = layout.getLineBaseline(i);
String lineText = mText.subSequence(lineStart, lineEnd).toString();
for (int c = 0; c < lineText.length(); c++) {
int alpha = alphaList.get(gapIndex);
mPaint.setAlpha((int) ((255 - alpha) * progress + alpha));
canvas.drawText(String.valueOf(lineText.charAt(c)), lineLeft, lineBaseline, mPaint);
lineLeft += gapList.get(gapIndex++);
}
}
}
/**
* Return the offset of the widget's last text line baseline from the widget's top
* boundary. If this widget does not support baseline alignment, this method returns -1.
*
* @return the offset of the baseline of the last text line within the widget's bounds or -1
* if baseline alignment is not supported
*/
@Override
public int getBaseline() {
Layout layout = getLayout();
if (layout == null) {
return super.getBaseline();
}
int baselineOffset = super.getBaseline() - layout.getLineBaseline(0);
return baselineOffset + layout.getLineBaseline(layout.getLineCount() - 1);
}
/**
* @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) {
}
}
public void drawLeadingMargin(Canvas canvas, Paint paint, int x, int dir,
int top, int baseline, int bottom, CharSequence text,
int start, int end, boolean first, Layout layout) {
if (((Spanned)text).getSpanStart(this) == start) {
Style style = paint.getStyle();
paint.setStyle(Style.FILL);
float yPosition;
if (layout != null) {
int line = layout.getLineForOffset(start);
yPosition = (float)layout.getLineBaseline(line) - (float)this.bulletRadius * 2.0F;
} else {
yPosition = (float)(top + bottom) / 2.0F;
}
float xPosition = (float)(x + dir * this.bulletRadius);
if (canvas.isHardwareAccelerated()) {
if (this.mBulletPath == null) {
this.mBulletPath = new Path();
this.mBulletPath.addCircle(0.0F, 0.0F, (float)this.bulletRadius, Direction.CW);
}
canvas.save();
canvas.translate(xPosition, yPosition);
canvas.drawPath(this.mBulletPath, paint);
canvas.restore();
} else {
canvas.drawCircle(xPosition, yPosition, (float)this.bulletRadius, paint);
}
paint.setStyle(style);
}
}
public static void draw(TextView textView, Canvas canvas) {
Layout layout = textView.getLayout();
if (layout != null) {
CharSequence text = textView.getText();
if (text instanceof Spanned) {
Spanned spanned = (Spanned) text;
OverlineSpan[] spans = spanned.getSpans(0, spanned.length(), OverlineSpan.class);
if (spans != null && spans.length > 0) {
int paddingTop = textView.getTotalPaddingTop();
int paddingLeft = textView.getPaddingLeft();
int shift = (int) (textView.getTextSize() * 8f / 9f);
float thickness = textView.getTextSize() / 15f - 0.25f;
int color = textView.getCurrentTextColor();
PAINT.setColor(color);
PAINT.setStrokeWidth(thickness);
for (OverlineSpan span : spans) {
int start = spanned.getSpanStart(span);
int end = spanned.getSpanEnd(span);
int lineStart = layout.getLineForOffset(start);
int lineEnd = layout.getLineForOffset(end);
for (int i = lineStart; i <= lineEnd; i++) {
float left = i == lineStart ? layout.getPrimaryHorizontal(start) : layout.getLineLeft(i);
float right = i == lineEnd ? layout.getPrimaryHorizontal(end) : layout.getLineRight(i);
float top = layout.getLineBaseline(i) - shift + 0.5f;
canvas.drawLine(paddingLeft + left, paddingTop + top, paddingLeft + right,
paddingTop + top, PAINT);
}
}
}
}
}
}
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);
}
}