本文整理汇总了Java中android.text.Layout.getLineEnd方法的典型用法代码示例。如果您正苦于以下问题:Java Layout.getLineEnd方法的具体用法?Java Layout.getLineEnd怎么用?Java Layout.getLineEnd使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.text.Layout
的用法示例。
在下文中一共展示了Layout.getLineEnd方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: onDraw
import android.text.Layout; //导入方法依赖的package包/类
@Override
protected void onDraw(Canvas canvas) {
TextPaint paint = getPaint();
paint.setColor(getCurrentTextColor());
paint.drawableState = getDrawableState();
String text = getText().toString();
float lineY = 0;
lineY += getTextSize() * 1f;
final Layout layout = getLayout();
final float desiredLineWidth = getMeasuredWidth();
for (int i = 0, lineCount = layout.getLineCount(); i < lineCount; i++) {
int lineStart = layout.getLineStart(i);
int lineEnd = layout.getLineEnd(i);
String line = text.substring(lineStart, lineEnd);
if (needScale(line) && i < lineCount - 1) {
drawScaledText(line, canvas, lineY, desiredLineWidth, paint);
} else {
canvas.drawText(line, 0, lineY, paint);
}
lineY += getLineHeight();
}
}
示例2: getLineForIndex
import android.text.Layout; //导入方法依赖的package包/类
/**
* Find the number of lines of text which must be shown in order to display the character at
* a given index.
*/
private int getLineForIndex(int index) {
Layout layout = getLayout();
int endLine = 0;
while (endLine < layout.getLineCount() && layout.getLineEnd(endLine) < index) {
endLine++;
}
// Since endLine is an index, add 1 to get the number of lines.
return endLine + 1;
}
示例3: getPrevLine
import android.text.Layout; //导入方法依赖的package包/类
/**
* @return the line above current cursor
*/
@Nullable
private CharSequence getPrevLine(Editable editable, Layout layout, int currentLine) {
if (currentLine - 1 < 0) return null;
int lineStart = layout.getLineStart(currentLine - 1);
int lineEnd = layout.getLineEnd(currentLine - 1);
return editable.subSequence(lineStart, lineEnd);
}
示例4: getNextLine
import android.text.Layout; //导入方法依赖的package包/类
@Nullable
protected CharSequence getNextLine(Editable editable, Layout layout, int currentLine) {
if (currentLine + 1 > layout.getLineCount() - 1) return null;
int lineStart = layout.getLineStart(currentLine + 1);
int lineEnd = layout.getLineEnd(currentLine + 1);
return editable.subSequence(lineStart, lineEnd);
}
示例5: getLineFromIndex
import android.text.Layout; //导入方法依赖的package包/类
/**
* Gets the lineInfo from the index of the letter in the text
*/
public static int getLineFromIndex(int index, int lineCount, Layout layout) {
int line;
int currentIndex = 0;
for (line = 0; line < lineCount; line++) {
currentIndex += layout.getLineEnd(line) - layout.getLineStart(line);
if (currentIndex > index) {
break;
}
}
return line;
}
示例6: goToLine
import android.text.Layout; //导入方法依赖的package包/类
/**
* move cursor to lineInfo
*
* @param line - lineInfo in editor, start at 0
*/
public void goToLine(int line) {
Layout layout = getLayout();
line = Math.min(line - 1, getLineCount() - 1);
line = Math.max(0, line);
if (layout != null) {
int index = layout.getLineEnd(line);
setSelection(index);
}
}
示例7: getLineFromIndex
import android.text.Layout; //导入方法依赖的package包/类
/**
* Gets the lineInfo from the index of the letter in the text
*/
public static int getLineFromIndex(int index, int lineCount, Layout layout) {
int line;
int currentIndex = 0;
for (line = 0; line < lineCount; line++) {
currentIndex += layout.getLineEnd(line) - layout.getLineStart(line);
if (currentIndex >= index) {
break;
}
}
return line;
}
示例8: adjustEllipsizeEndText
import android.text.Layout; //导入方法依赖的package包/类
private void adjustEllipsizeEndText(Layout layout) {
try {
final CharSequence originText = getText();
if(TextUtils.isEmpty(originText)){
return;
}
final CharSequence restSuffixText = originText.subSequence(
originText.length() - mEllipsizeIndex, originText.length());
final int width = layout.getWidth() - getPaddingLeft() - getPaddingRight();
final int maxLineCount = computeMaxLineCount(layout);
final int lastLineWidth = (int) layout.getLineWidth(maxLineCount - 1);
final int mLastCharacterIndex = layout.getLineEnd(maxLineCount - 1);
final int suffixWidth = (int) (Layout.getDesiredWidth(mEllipsizeText, getPaint()) +
Layout.getDesiredWidth(restSuffixText, getPaint())) + 1;
if (lastLineWidth + suffixWidth > width) {
final int widthDiff = lastLineWidth + suffixWidth - width;
final int removedCharacterCount = computeRemovedEllipsizeEndCharacterCount(widthDiff,
originText.subSequence(0, mLastCharacterIndex));
setText(originText.subSequence(0, mLastCharacterIndex - removedCharacterCount));
append(mEllipsizeText);
append(restSuffixText);
} else {
setText(originText.subSequence(0, mLastCharacterIndex));
append(mEllipsizeText);
append(restSuffixText);
}
}catch (IndexOutOfBoundsException e){
}
}
示例9: getLineFromIndex
import android.text.Layout; //导入方法依赖的package包/类
/**
* Gets the line from the index of the letter in the text
*
* @param index
* @param lineCount
* @param layout
* @return
*/
public static int getLineFromIndex(int index, int lineCount, Layout layout) {
int line;
int currentIndex = 0;
for (line = 0; line < lineCount; line++) {
currentIndex += layout.getLineEnd(line) - layout.getLineStart(line);
if (currentIndex > index) {
break;
}
}
return line;
}
示例10: drawTextWithJustify
import android.text.Layout; //导入方法依赖的package包/类
/**
* 重绘文字,两端对齐
*
* @param canvas
*/
private void drawTextWithJustify(Canvas canvas) {
// 文字画笔
TextPaint textPaint = getPaint();
textPaint.setColor(getCurrentTextColor());
textPaint.drawableState = getDrawableState();
String text_str = getText().toString();
// 当前所在行的Y向偏移
int currentLineOffsetY = getPaddingTop();
currentLineOffsetY += getTextSize();
Layout layout = getLayout();
//循环每一行,绘制文字
for (int i = 0; i < layout.getLineCount(); i++) {
int lineStart = layout.getLineStart(i);
int lineEnd = layout.getLineEnd(i);
//获取到TextView每行中的内容
String line_str = text_str.substring(lineStart, lineEnd);
// 获取每行字符串的宽度(不包括字符间距)
float desiredWidth = StaticLayout.getDesiredWidth(text_str, lineStart, lineEnd, getPaint());
if (isLineNeedJustify(line_str)) {
//最后一行不需要重绘
if (i == layout.getLineCount() - 1) {
canvas.drawText(line_str, getPaddingLeft(), currentLineOffsetY, textPaint);
} else {
drawJustifyTextForLine(canvas, line_str, desiredWidth, currentLineOffsetY);
}
} else {
canvas.drawText(line_str, getPaddingLeft(), currentLineOffsetY, textPaint);
}
//更新行Y向偏移
currentLineOffsetY += getLineHeight();
}
}
示例11: calculatorCharPositionToLeft
import android.text.Layout; //导入方法依赖的package包/类
/**
* 计算字符距离控件左侧的位移
*
* @param line 字符所在行
* @param charOffset 字符偏移量
*/
private float calculatorCharPositionToLeft(int line, int charOffset) {
String text_str = getText().toString();
Layout layout = getLayout();
int lineStart = layout.getLineStart(line);
int lineEnd = layout.getLineEnd(line);
String line_str = text_str.substring(lineStart, lineEnd);
if (line_str.equals("\n"))
return getPaddingLeft();
// 最左侧
if (lineStart == charOffset)
return getPaddingLeft();
// 最右侧
if (charOffset == lineEnd - 1)
return mViewTextWidth + getPaddingLeft();
float desiredWidth = StaticLayout.getDesiredWidth(text_str, lineStart, lineEnd, getPaint());
// 中间位置
// 计算相邻字符之间需要填充的宽度
// (TextView内容的实际宽度 - 该行字符串的宽度)/(字符个数-1)
float insert_blank = (mViewTextWidth - desiredWidth) / (line_str.length() - 1);
// 计算当前字符左侧所有字符的宽度
float allLeftCharWidth = StaticLayout.getDesiredWidth(text_str.substring(lineStart, charOffset), getPaint());
// 相邻字符之间需要填充的宽度 + 当前字符左侧所有字符的宽度
return insert_blank * (charOffset - lineStart) + allLeftCharWidth + getPaddingLeft();
}
示例12: updateHasNewLineArray
import android.text.Layout; //导入方法依赖的package包/类
public void updateHasNewLineArray(int lineCount, Layout layout, String text) {
boolean[] hasNewLineArray = new boolean[lineCount];
toCountLinesArray = new boolean[lineCount];
realLines = new int[lineCount];
if (TextUtils.isEmpty(text)) {
toCountLinesArray[0] = false;
realLines[0] = 0;
return;
}
if (lineCount == 0) return;
int i;
// for every lineInfo on the edittext
for (i = 0; i < lineCount; i++) {
// check if this lineInfo contains "\n"
if (layout.getLineEnd(i) == 0) {
hasNewLineArray[i] = false;
} else {
hasNewLineArray[i] = text.charAt(layout.getLineEnd(i) - 1) == '\n';
}
// if true
if (hasNewLineArray[i]) {
int j = i - 1;
while (j >= 0 && !hasNewLineArray[j]) {
j--;
}
toCountLinesArray[j + 1] = true;
}
}
toCountLinesArray[lineCount - 1] = true;
int realLine = 0;
for (i = 0; i < toCountLinesArray.length; i++) {
realLines[i] = realLine;
if (toCountLinesArray[i]) {
realLine++;
}
}
}
示例13: action
import android.text.Layout; //导入方法依赖的package包/类
private boolean action(int what, TextView widget, Spannable buffer) {
Layout layout = widget.getLayout();
int padding = widget.getTotalPaddingTop() + widget.getTotalPaddingBottom();
int areatop = widget.getScrollY();
int areabot = (widget.getHeight() + areatop) - padding;
int linetop = layout.getLineForVertical(areatop);
int linebot = layout.getLineForVertical(areabot);
int first = layout.getLineStart(linetop);
int last = layout.getLineEnd(linebot);
MyURLSpan[] candidates = (MyURLSpan[]) buffer.getSpans(first, last, MyURLSpan.class);
int a = Selection.getSelectionStart(buffer);
int b = Selection.getSelectionEnd(buffer);
int selStart = Math.min(a, b);
int selEnd = Math.max(a, b);
if (selStart < 0 && buffer.getSpanStart(FROM_BELOW) >= 0) {
selEnd = buffer.length();
selStart = selEnd;
}
if (selStart > last) {
selEnd = ActivityChooserViewAdapter.MAX_ACTIVITY_COUNT_UNLIMITED;
selStart = ActivityChooserViewAdapter.MAX_ACTIVITY_COUNT_UNLIMITED;
}
if (selEnd < first) {
selEnd = -1;
selStart = -1;
}
int beststart;
int bestend;
int i;
switch (what) {
case 1:
if (selStart != selEnd) {
MyURLSpan[] link = (MyURLSpan[]) buffer.getSpans(selStart, selEnd, MyURLSpan
.class);
if (link.length == 1) {
link[0].onClick(widget);
break;
}
return false;
}
return false;
case 2:
beststart = -1;
bestend = -1;
for (i = 0; i < candidates.length; i++) {
int end = buffer.getSpanEnd(candidates[i]);
if ((end < selEnd || selStart == selEnd) && end > bestend) {
beststart = buffer.getSpanStart(candidates[i]);
bestend = end;
}
}
if (beststart >= 0) {
Selection.setSelection(buffer, bestend, beststart);
return true;
}
break;
case 3:
beststart = ActivityChooserViewAdapter.MAX_ACTIVITY_COUNT_UNLIMITED;
bestend = ActivityChooserViewAdapter.MAX_ACTIVITY_COUNT_UNLIMITED;
for (i = 0; i < candidates.length; i++) {
int start = buffer.getSpanStart(candidates[i]);
if ((start > selStart || selStart == selEnd) && start < beststart) {
beststart = start;
bestend = buffer.getSpanEnd(candidates[i]);
}
}
if (bestend < Integer.MAX_VALUE) {
Selection.setSelection(buffer, beststart, bestend);
return true;
}
break;
}
return false;
}