本文整理汇总了Java中android.text.Layout.getOffsetForHorizontal方法的典型用法代码示例。如果您正苦于以下问题:Java Layout.getOffsetForHorizontal方法的具体用法?Java Layout.getOffsetForHorizontal怎么用?Java Layout.getOffsetForHorizontal使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.text.Layout
的用法示例。
在下文中一共展示了Layout.getOffsetForHorizontal方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: onTouchEvent
import android.text.Layout; //导入方法依赖的package包/类
public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
if (event.getAction() == 1) {
this.mLinkClicked = false;
this.mClickedLink = null;
int x = (((int) event.getX()) - widget.getTotalPaddingLeft()) + widget.getScrollX();
int y = (((int) event.getY()) - widget.getTotalPaddingTop()) + widget.getScrollY();
Layout layout = widget.getLayout();
int off = layout.getOffsetForHorizontal(layout.getLineForVertical(y), (float) x);
ClickableSpan[] link = (ClickableSpan[]) buffer.getSpans(off, off, ClickableSpan
.class);
if (link.length != 0) {
SensibleUrlSpan span = link[0];
this.mLinkClicked = span.onClickSpan(widget);
this.mClickedLink = span.getURL();
return this.mLinkClicked;
}
}
super.onTouchEvent(widget, buffer, event);
return false;
}
示例2: 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;
}
}
示例3: pointToPosition
import android.text.Layout; //导入方法依赖的package包/类
private int pointToPosition(int x, int y) {
x -= getCompoundPaddingLeft();
y -= getExtendedPaddingTop();
x += getScrollX();
y += getScrollY();
Layout layout = getLayout();
if (layout == null) {
return -1;
}
int line = layout.getLineForVertical(y);
int off = layout.getOffsetForHorizontal(line, x);
return off;
}
示例4: handleClickableSpan
import android.text.Layout; //导入方法依赖的package包/类
public static boolean handleClickableSpan(View view, Layout layout, Spanned buffer, Class<? extends Clickable> spanType, 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 -= view.getPaddingLeft();
y -= view.getPaddingTop();
x += view.getScrollX();
y += view.getScrollY();
int line = layout.getLineForVertical(y);
int off = layout.getOffsetForHorizontal(line, x);
Clickable[] link = buffer.getSpans(off, off, spanType);
if (link.length != 0) {
if (action == MotionEvent.ACTION_UP) {
link[0].onClick(view);
} else if (buffer instanceof Spannable) {
Selection.setSelection((Spannable) buffer,
buffer.getSpanStart(link[0]),
buffer.getSpanEnd(link[0]));
}
return true;
} else if (buffer instanceof Spannable) {
Selection.removeSelection((Spannable) buffer);
}
}
return false;
}
示例5: getPressedSpan
import android.text.Layout; //导入方法依赖的package包/类
public ITouchableSpan getPressedSpan(TextView textView, Spannable spannable, MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
x -= textView.getTotalPaddingLeft();
y -= textView.getTotalPaddingTop();
x += textView.getScrollX();
y += textView.getScrollY();
Layout layout = textView.getLayout();
int line = layout.getLineForVertical(y);
int off = layout.getOffsetForHorizontal(line, x);
if (x < layout.getLineLeft(line) || x > layout.getLineRight(line)) {
// 实际上没点到任何内容
off = -1;
}
ITouchableSpan[] link = spannable.getSpans(off, off, ITouchableSpan.class);
ITouchableSpan touchedSpan = null;
if (link.length > 0) {
touchedSpan = link[0];
}
return touchedSpan;
}
示例6: onTouchEvent
import android.text.Layout; //导入方法依赖的package包/类
@Override
public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
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);
MathSpannable[] spans = buffer.getSpans(off, off, MathSpannable.class);
if (spans.length != 0) {
return spans[0].onTouchEvent(event);
}
return false;
}
示例7: pointToPosition
import android.text.Layout; //导入方法依赖的package包/类
private int pointToPosition(int x, int y) {
x -= getCompoundPaddingLeft();
y -= getExtendedPaddingTop();
x += getScrollX();
y += getScrollY();
Layout layout = getLayout();
if (layout == null) {
return -1;
}
int line = layout.getLineForVertical(y);
int off = layout.getOffsetForHorizontal(line, x);
return off;
}
示例8: handleTouchEvent
import android.text.Layout; //导入方法依赖的package包/类
public boolean handleTouchEvent(MotionEvent event) {
int action = event.getAction();
if (action != MotionEvent.ACTION_UP
&& action != MotionEvent.ACTION_DOWN) {
return true;
} else {
int x = (int) event.getX();
int y = (int) event.getY();
x -= getTotalPaddingLeft();
y -= getTotalPaddingTop();
x += getScrollX();
y += getScrollY();
Layout layout = getLayout();
int line = layout.getLineForVertical(y);
int offset = layout.getOffsetForHorizontal(line, x);
float width = layout.getLineWidth(line);
if (y > width) {
offset = y;
}
if (!(getText() instanceof Spannable)) {
return true;
} else {
Spannable span = (Spannable) getText();
ClickableSpan[] clickSpan = span.getSpans(offset, offset,
ClickableSpan.class);
if (clickSpan == null || clickSpan.length == 0) {
am[] aam = span.getSpans(offset, offset, am.class);
if (aam != null && aam.length != 0)
return false;
return true;
} else {
return false;
}
}
}
}
示例9: 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) {
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);
ClickableSpan[] link =
buffer.getSpans(off, off, ClickableSpan.class);
if (link.length != 0) {
link[0].onClick(widget);
}
}
return super.onTouchEvent(widget, buffer, event);
}
示例10: getTouchedSpan
import android.text.Layout; //导入方法依赖的package包/类
/**
* Gets the span that was touched.
* @param tv {@link TextView}
* @param span {@link Spannable}
* @param e {@link MotionEvent}
* @return {@link TouchableSpan}
*/
private TouchableSpan getTouchedSpan(TextView tv, Spannable span, MotionEvent e) {
// Find the location in which the touch was made
int x = (int)e.getX();
int y = (int)e.getY();
// Ignore padding
x -= tv.getTotalPaddingLeft();
y -= tv.getTotalPaddingTop();
// Account for scrollable text
x += tv.getScrollX();
y += tv.getScrollY();
final Layout layout = tv.getLayout();
final int touchedLine = layout.getLineForVertical(y);
final int touchOffset = layout.getOffsetForHorizontal(touchedLine, x);
// Set bounds of the touched line
touchBounds.left = layout.getLineLeft(touchedLine);
touchBounds.top = layout.getLineTop(touchedLine);
touchBounds.right = layout.getLineRight(touchedLine);
touchBounds.bottom = layout.getLineBottom(touchedLine);
// Ensure the span falls within the bounds of the touch
TouchableSpan touchSpan = null;
if (touchBounds.contains(x, y)) {
// Find clickable spans that lie under the touched area
TouchableSpan[] spans = span.getSpans(touchOffset, touchOffset, TouchableSpan.class);
touchSpan = (spans.length > 0) ? spans[0] : null;
}
return touchSpan;
}
示例11: reactTagForTouch
import android.text.Layout; //导入方法依赖的package包/类
@Override
public int reactTagForTouch(float touchX, float touchY) {
Spanned text = (Spanned) getText();
int target = getId();
int x = (int) touchX;
int y = (int) touchY;
Layout layout = getLayout();
if (layout == null) {
// If the layout is null, the view hasn't been properly laid out yet. Therefore, we can't find
// the exact text tag that has been touched, and the correct tag to return is the default one.
return target;
}
int line = layout.getLineForVertical(y);
int lineStartX = (int) layout.getLineLeft(line);
int lineEndX = (int) layout.getLineRight(line);
// TODO(5966918): Consider extending touchable area for text spans by some DP constant
if (x >= lineStartX && x <= lineEndX) {
int index = layout.getOffsetForHorizontal(line, x);
// We choose the most inner span (shortest) containing character at the given index
// if no such span can be found we will send the textview's react id as a touch handler
// In case when there are more than one spans with same length we choose the last one
// from the spans[] array, since it correspond to the most inner react element
ReactTagSpan[] spans = text.getSpans(index, index, ReactTagSpan.class);
if (spans != null) {
int targetSpanTextLength = text.length();
for (int i = 0; i < spans.length; i++) {
int spanStart = text.getSpanStart(spans[i]);
int spanEnd = text.getSpanEnd(spans[i]);
if (spanEnd > index && (spanEnd - spanStart) <= targetSpanTextLength) {
target = spans[i].getReactTag();
targetSpanTextLength = (spanEnd - spanStart);
}
}
}
}
return target;
}
示例12: 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);
}
示例13: onTouchEvent
import android.text.Layout; //导入方法依赖的package包/类
@Override
public boolean onTouchEvent(TextView widget, Spannable text, MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
// 这里得到的x , y值只是相对于屏幕的位置
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 location = layout.getOffsetForHorizontal(layout.getLineForVertical(y), x);
clickableSpans = text.getSpans(location, location, ClickableSpan.class);
if (clickableSpans.length > 0) {
isClickTextView = false;
LogUtil.e("点击clickSpan位置");
// 选中点击位置
// Selection.setSelection(text, text.getSpanStart(clickableSpans[0]), text.getSpanEnd(clickableSpans[0]));
text.setSpan(colorSpan = new BackgroundColorSpan(selectedColor), text.getSpanStart(clickableSpans[0]), text.getSpanEnd(clickableSpans[0]), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return true;
} else {
isClickTextView = true;
widget.setBackgroundColor(Color.parseColor("#979595"));
LogUtil.e("点击的是正常textView的文字");
}
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
if (clickableSpans.length > 0) {
LogUtil.e("点击span的up11");
// 设置点击时间
clickableSpans[0].onClick(widget);
text.removeSpan(colorSpan);
// Selection.removeSelection(text);
return true;
} else {
LogUtil.e("点击textView文字的up11");
widget.setBackgroundColor(Color.parseColor("#00000000"));
}
break;
default:
if (colorSpan != null) {
text.removeSpan(colorSpan);
// Selection.removeSelection(text);
return true;
} else {
widget.setBackgroundColor(Color.parseColor("#00000000"));
}
break;
}
return super.onTouchEvent(widget, text, event);
}
示例14: 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);
ClickableSpan[] link = buffer.getSpans(off, off, ClickableSpan.class);
if (link.length != 0) {
if (action == MotionEvent.ACTION_UP) {
try {
link[0].onClick(widget);
} catch (Exception e) {
e.printStackTrace();
ToastUtil.show(R.string.not_foud_qq);
}
if(((URLSpan) link[0]).getURL() != null){
if(DonateActivity.zhifubao.startsWith(((URLSpan) link[0]).getURL())){
UrlCountUtil.onEvent(UrlCountUtil.CLICK_SETTINGS_ABOUT_DONATE);
}else if(DonateActivity.qqJump.startsWith(((URLSpan) link[0]).getURL())){
UrlCountUtil.onEvent(UrlCountUtil.CLICK_SETTINGS_JOIN_QQ);
}
}
} else if (action == MotionEvent.ACTION_DOWN) {
Selection.setSelection(buffer,
buffer.getSpanStart(link[0]),
buffer.getSpanEnd(link[0]));
}
return true;
} else {
Selection.removeSelection(buffer);
}
}
return super.onTouchEvent(widget, buffer, event);
}
示例15: onTouch
import android.text.Layout; //导入方法依赖的package包/类
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
if(!(v instanceof TextView)){
return false;
}
TextView textView = (TextView) v;
if (action == MotionEvent.ACTION_UP ||
action == MotionEvent.ACTION_DOWN) {
int x = (int) event.getX();
int y = (int) event.getY();
x -= textView.getTotalPaddingLeft();
y -= textView.getTotalPaddingTop();
x += textView.getScrollX();
y += textView.getScrollY();
Layout layout = textView.getLayout();
int line = layout.getLineForVertical(y);
int off = layout.getOffsetForHorizontal(line, x);
ClickableSpan[] link = spannable.getSpans(off, off, ClickableSpan.class);
if (link.length != 0) {
if (action == MotionEvent.ACTION_UP) {
link[0].onClick(textView);
} else if (action == MotionEvent.ACTION_DOWN) {
Selection.setSelection(spannable,
spannable.getSpanStart(link[0]),
spannable.getSpanEnd(link[0]));
}
return true;
} else {
Selection.removeSelection(spannable);
}
}
return false;
}