本文整理匯總了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;
}