本文整理匯總了Java中android.text.Layout.getOffsetToLeftOf方法的典型用法代碼示例。如果您正苦於以下問題:Java Layout.getOffsetToLeftOf方法的具體用法?Java Layout.getOffsetToLeftOf怎麽用?Java Layout.getOffsetToLeftOf使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類android.text.Layout
的用法示例。
在下文中一共展示了Layout.getOffsetToLeftOf方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getPreciseOffset
import android.text.Layout; //導入方法依賴的package包/類
public static int getPreciseOffset(TextView textView, int x, int y) {
Layout layout = textView.getLayout();
if (layout != null) {
int topVisibleLine = layout.getLineForVertical(y);
int offset = layout.getOffsetForHorizontal(topVisibleLine, x);
int offsetX = (int) layout.getPrimaryHorizontal(offset);
if (offsetX > x) {
return layout.getOffsetToLeftOf(offset);
} else {
return offset;
}
} else {
return -1;
}
}
示例2: onTouchEvent
import android.text.Layout; //導入方法依賴的package包/類
@Override
public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_UP ||
action == MotionEvent.ACTION_DOWN) {
int x = (int) event.getX();
int y = (int) event.getY();
x -= widget.getTotalPaddingLeft();
y -= widget.getTotalPaddingTop();
x += widget.getScrollX();
y += widget.getScrollY();
Layout layout = widget.getLayout();
int line = layout.getLineForVertical(y);
int off = layout.getOffsetForHorizontal(line, x);
LongClickableSpan[] link = buffer.getSpans(off, off, LongClickableSpan.class);
if (link.length != 0) {
long currTime = System.currentTimeMillis();
LongClickableSpan l = link[0];
int ls = buffer.getSpanStart(l);
int le = buffer.getSpanEnd(l);
// 判斷點擊的點是否在Image範圍內
ClickableImageSpan[] is = buffer.getSpans(ls, le, ClickableImageSpan.class);
if (is.length > 0) {
if (!is[0].clicked(x)) {
Selection.removeSelection(buffer);
return false;
}
} else if (off < layout.getOffsetToLeftOf(ls) || off > layout.getOffsetToLeftOf(le + 1)) {
// 判斷點擊位置是否在鏈接範圍內
Selection.removeSelection(buffer);
return false;
}
if (action == MotionEvent.ACTION_UP) {
// 如果按下時間超過500毫秒,觸發長按事件
if (currTime - lastTime > MIN_INTERVAL) {
if (!l.onLongClick(widget)) {
// onLongClick返回false代表事件未處理,交由onClick處理
l.onClick(widget);
}
} else {
l.onClick(widget);
}
} else {
Selection.setSelection(buffer,
ls, le);
}
lastTime = currTime;
return true;
} else {
Selection.removeSelection(buffer);
return false;
}
}
return super.onTouchEvent(widget, buffer, event);
}