當前位置: 首頁>>代碼示例>>Java>>正文


Java Layout.getOffsetForHorizontal方法代碼示例

本文整理匯總了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;
}
 
開發者ID:JackChan1999,項目名稱:boohee_v5.6,代碼行數:21,代碼來源:LinkUtils.java

示例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;
    }
}
 
開發者ID:shenhuanet,項目名稱:SelectableTextProvider,代碼行數:18,代碼來源:TextLayoutUtil.java

示例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;
}
 
開發者ID:XecureIT,項目名稱:PeSanKita-android,代碼行數:18,代碼來源:RecipientsEditor.java

示例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;
}
 
開發者ID:lsjwzh,項目名稱:FastTextView,代碼行數:36,代碼來源:ClickableSpanUtil.java

示例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;
}
 
開發者ID:coopese,項目名稱:qmui,代碼行數:26,代碼來源:QMUILinkTouchDecorHelper.java

示例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;
}
 
開發者ID:tranleduy2000,項目名稱:floating_calc,代碼行數:24,代碼來源:CalculatorEditText.java

示例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;
}
 
開發者ID:sdrausty,項目名稱:buildAPKsApps,代碼行數:19,代碼來源:RecipientsEditor.java

示例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;
            }
        }
    }
}
 
開發者ID:hsj-xiaokang,項目名稱:OSchina_resources_android,代碼行數:41,代碼來源:TweetTextView.java

示例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);
}
 
開發者ID:rafjordao,項目名稱:Nird2,代碼行數:29,代碼來源:ArticleMovementMethod.java

示例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;
}
 
開發者ID:tylersuehr7,項目名稱:social-text-view,代碼行數:41,代碼來源:AccurateMovementMethod.java

示例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;
}
 
開發者ID:qq565999484,項目名稱:RNLearn_Project1,代碼行數:45,代碼來源:ReactTextView.java

示例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);
}
 
開發者ID:nichbar,項目名稱:Aequorea,代碼行數:64,代碼來源:LongClickableLinkMovementMethod.java

示例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);
        }
 
開發者ID:HelloChenJinJun,項目名稱:TestChat,代碼行數:56,代碼來源:CustomMoveMethod.java

示例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);
}
 
開發者ID:l465659833,項目名稱:Bigbang,代碼行數:52,代碼來源:CountLinkMovementMethod.java

示例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;
}
 
開發者ID:Pingsh,項目名稱:Mix,代碼行數:42,代碼來源:TouchTextView.java


注:本文中的android.text.Layout.getOffsetForHorizontal方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。