private void sendTextBitmap(LiveViewAdapter mLiveViewAdapter, int mPluginId, String txt) { // Empty bitmap and link the canvas to it Bitmap bitmap = null; try { bitmap = Bitmap.createBitmap(128, 128, Bitmap.Config.RGB_565); } catch (IllegalArgumentException e) { return; } Canvas canvas = new Canvas(bitmap); // Set the text properties in the canvas TextPaint textPaint = new TextPaint(); textPaint.setTextSize(18); textPaint.setColor(Color.WHITE); // Create the text layout and draw it to the canvas Layout textLayout = new StaticLayout(txt, textPaint, 128, Layout.Alignment.ALIGN_CENTER, 1, 1, false); textLayout.draw(canvas); try { mLiveViewAdapter.sendImageAsBitmap( mPluginId, PluginUtils.centerX(bitmap), PluginUtils.centerY(bitmap), bitmap); } catch (Exception e) { Log.d(PluginConstants.LOG_TAG, "Failed to send bitmap", e); } }
@Override public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) { if (event.getAction() != MotionEvent.ACTION_UP) return super.onTouchEvent(widget, buffer, 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); URLSpan[] link = buffer.getSpans(off, off, URLSpan.class); if (link.length != 0) { onLinkClick(link[0].getURL()); } return true; }
private boolean shouldEnableDescription(TextView mDescription) { Layout layout = mDescription.getLayout(); if (layout != null && layout.getEllipsisCount(mDescription.getLineCount() - 1) > 0) { return true; } return false; }
@Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { if (mOnLayout == null) { mOnLayout = makeLayout(mTextOn); } if (mOffLayout == null) { mOffLayout = makeLayout(mTextOff); } mTrackDrawable.getPadding(mTempRect); final int maxTextWidth = Math.max(mOnLayout.getWidth(), mOffLayout.getWidth()); final int switchWidth = Math.max( mSwitchMinWidth, maxTextWidth * 2 + mThumbTextPadding * 4 + mTempRect.left + mTempRect.right); final int switchHeight = mTrackDrawable.getIntrinsicHeight(); mThumbWidth = maxTextWidth + mThumbTextPadding * 2; mSwitchWidth = switchWidth; mSwitchHeight = switchHeight; super.onMeasure(widthMeasureSpec, heightMeasureSpec); final int measuredHeight = getMeasuredHeight(); if (measuredHeight < switchHeight) { setMeasuredDimension(getMeasuredWidth(), switchHeight); } }
/** {@inheritDoc} */ public void displaySuggestions(boolean display) { // If nothing to change, return early if (display == isDisplayingSuggestions() || mMentionsEditText == null) { return; } // Change view depending on whether suggestions are being shown or not if (display) { disableSpellingSuggestions(true); mTextCounterView.setVisibility(View.GONE); mSuggestionsList.setVisibility(View.VISIBLE); mPrevEditTextParams = mMentionsEditText.getLayoutParams(); mPrevEditTextBottomPadding = mMentionsEditText.getPaddingBottom(); mMentionsEditText.setPadding( mMentionsEditText.getPaddingLeft(), mMentionsEditText.getPaddingTop(), mMentionsEditText.getPaddingRight(), mMentionsEditText.getPaddingTop()); int height = mMentionsEditText.getPaddingTop() + mMentionsEditText.getLineHeight() + mMentionsEditText.getPaddingBottom(); mMentionsEditText.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, height)); mMentionsEditText.setVerticalScrollBarEnabled(false); int cursorLine = getCurrentCursorLine(); Layout layout = mMentionsEditText.getLayout(); if (layout != null) { int lineTop = layout.getLineTop(cursorLine); mMentionsEditText.scrollTo(0, lineTop); } // Notify action listener that list was shown if (mActionListener != null) { mActionListener.onSuggestionsDisplayed(); } } else { disableSpellingSuggestions(false); mTextCounterView.setVisibility(View.VISIBLE); mSuggestionsList.setVisibility(View.GONE); mMentionsEditText.setPadding( mMentionsEditText.getPaddingLeft(), mMentionsEditText.getPaddingTop(), mMentionsEditText.getPaddingRight(), mPrevEditTextBottomPadding); if (mPrevEditTextParams == null) { mPrevEditTextParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); } mMentionsEditText.setLayoutParams(mPrevEditTextParams); mMentionsEditText.setVerticalScrollBarEnabled(true); // Notify action listener that list was hidden if (mActionListener != null) { mActionListener.onSuggestionsHidden(); } } requestLayout(); invalidate(); }
/** * Calculates control width and creates text layouts * * @param widthSize the input layout width * @param mode the layout mode * @return the calculated control width */ private int calculateLayoutWidth(int widthSize, int mode) { initResourcesIfNecessary(); int width = widthSize; int maxLength = getMaxTextLength(); if (maxLength > 0) { float textWidth = FloatMath.ceil(Layout.getDesiredWidth("0", itemsPaint)); itemsWidth = (int) (maxLength * textWidth); } else { itemsWidth = 0; } itemsWidth += ADDITIONAL_ITEMS_SPACE; // make it some more labelWidth = 0; if (label != null && label.length() > 0) { labelWidth = (int) FloatMath.ceil(Layout.getDesiredWidth(label, valuePaint)); } boolean recalculate = false; if (mode == MeasureSpec.EXACTLY) { width = widthSize; recalculate = true; } else { width = itemsWidth + labelWidth + 2 * PADDING; if (labelWidth > 0) { width += LABEL_OFFSET; } // Check against our minimum width width = Math.max(width, getSuggestedMinimumWidth()); if (mode == MeasureSpec.AT_MOST && widthSize < width) { width = widthSize; recalculate = true; } } if (recalculate) { // recalculate width int pureWidth = width - LABEL_OFFSET - 2 * PADDING; if (pureWidth <= 0) { itemsWidth = labelWidth = 0; } if (labelWidth > 0) { double newWidthItems = (double) itemsWidth * pureWidth / (itemsWidth + labelWidth); itemsWidth = (int) newWidthItems; labelWidth = pureWidth - itemsWidth; } else { itemsWidth = pureWidth + LABEL_OFFSET; // no label } } if (itemsWidth > 0) { createLayouts(itemsWidth, labelWidth); } return width; }
/** @return current line number of the cursor, or -1 if no cursor */ public int getCurrentCursorLine() { int selectionStart = mMentionsEditText.getSelectionStart(); Layout layout = mMentionsEditText.getLayout(); if (layout != null && !(selectionStart == -1)) { return layout.getLineForOffset(selectionStart); } return -1; }
@Override public boolean onTouch(View v, MotionEvent event) { Layout layout = ((TextView) v).getLayout(); int x = (int) event.getX(); int y = (int) event.getY(); int offset = 0; if (layout != null) { int line = layout.getLineForVertical(y); offset = layout.getOffsetForHorizontal(line, x); } TextView tv = (TextView) v; SpannableString value = SpannableString.valueOf(tv.getText()); LinkMovementMethod.getInstance().onTouchEvent(tv, value, event); switch (event.getActionMasked()) { case MotionEvent.ACTION_DOWN: WeburlSpan[] urlSpans = value.getSpans(0, value.length(), WeburlSpan.class); boolean find = false; int findStart = 0; int findEnd = 0; for (WeburlSpan urlSpan : urlSpans) { int start = value.getSpanStart(urlSpan); int end = value.getSpanEnd(urlSpan); if (start <= offset && offset <= end) { find = true; findStart = start; findEnd = end; break; } } if (find) { BackgroundColorSpan bkcolor = new BackgroundColorSpan(0xff89660f); value.setSpan(bkcolor, findStart, findEnd, Spanned.SPAN_INCLUSIVE_INCLUSIVE); ((TextView) v).setText(value); } return find; case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: BackgroundColorSpan[] backgroundColorSpans = value.getSpans(0, value.length(), BackgroundColorSpan.class); for (BackgroundColorSpan bkcolor : backgroundColorSpans) { value.removeSpan(bkcolor); ((TextView) v).setText(value); } break; } return false; }
protected boolean needsEllipsis() { final int width = getAvailableWidth(); if (width <= 0) { return false; } final Layout layout = getLayout(); return (layout != null && layout.getLineWidth(0) > width); }
@Override public boolean onTouchEvent(MotionEvent event) { CharSequence text = getText(); int action = event.getAction(); if (!(text instanceof Spannable)) { return super.onTouchEvent(event); } Spannable buffer = (Spannable) text; if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_MOVE) { TextView widget = this; 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); URLSpan[] link = buffer.getSpans(off, off, URLSpan.class); if (link.length != 0) { if (action == MotionEvent.ACTION_UP) { if (mCurrentLink == link[0]) { link[0].onClick(widget); } mCurrentLink = null; buffer.removeSpan(mLinkFocusStyle); } else if (action == MotionEvent.ACTION_DOWN) { mCurrentLink = link[0]; buffer.setSpan( mLinkFocusStyle, buffer.getSpanStart(link[0]), buffer.getSpanEnd(link[0]), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } return true; } } mCurrentLink = null; buffer.removeSpan(mLinkFocusStyle); return super.onTouchEvent(event); }
public boolean onTouchEvent(TextView widget, final Spannable buffer, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) position = event.getY(); // used to see if the user scrolled or not if (!(event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_DOWN)) { if (Math.abs((position - event.getY())) > 10) { handler.removeCallbacksAndMessages(null); Log.v(LogUtil.getTag(), "POSITION NOT CLICK IS " + event.getY()); } return super.onTouchEvent(widget, buffer, event); } Log.v(LogUtil.getTag(), "POSITION IS " + position); comm = (SpoilerRobotoTextView) widget; 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); final int off = layout.getOffsetForHorizontal(line, x); link = buffer.getSpans(off, off, URLSpan.class); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: clickHandled = false; if (link.length != 0) { handler.postDelayed(longClicked, android.view.ViewConfiguration.getLongPressTimeout()); } break; case MotionEvent.ACTION_UP: handler.removeCallbacksAndMessages(null); if (!clickHandled) { // regular click if (link.length != 0) { int i = 0; if (sequence != null) { i = ((Spannable) sequence).getSpanEnd(link[0]); } onLinkClick(link[0].getURL(), i); } } break; } return true; }
private float getMaxLineWidth(Layout layout) { float max_width = 0.0f; int lines = layout.getLineCount(); for (int i = 0; i < lines; i++) { if (layout.getLineWidth(i) > max_width) { max_width = layout.getLineWidth(i); } } return max_width; }
private boolean right(TextView widget, Spannable buffer) { if (DBG) { Log.d(LOG_TAG, "--- right:"); } Layout layout = widget.getLayout(); int to = layout.getOffsetToRightOf(getEndPos(widget)); mManager.setSelectedEndPos(to); mManager.onCursorMoved(); return true; }
private Integer findOffsetForPosition(float x, float y) { if (childView == null || childView.getLayout() == null) { return null; } Layout layout = this.childView.getLayout(); int line = layout.getLineForVertical((int) y); return layout.getOffsetForHorizontal(line, x); }
@Override protected void onDraw(Canvas canvas) { final int width = getWidth(); final int height = getHeight() - getPaddingBottom(); final Drawable secondary = mSecondary; if (secondary != null) { final int secondaryHeight = secondary.getIntrinsicHeight(); final float[] vertTicks = mVert.getTickPoints(); for (float y : vertTicks) { final int bottom = (int) Math.min(y + secondaryHeight, height); secondary.setBounds(0, (int) y, width, bottom); secondary.draw(canvas); } } final Drawable primary = mPrimary; if (primary != null) { final int primaryWidth = primary.getIntrinsicWidth(); final int primaryHeight = primary.getIntrinsicHeight(); final float[] horizTicks = mHoriz.getTickPoints(); for (float x : horizTicks) { final int right = (int) Math.min(x + primaryWidth, width); primary.setBounds((int) x, 0, right, height); primary.draw(canvas); } } mBorder.setBounds(0, 0, width, height); mBorder.draw(canvas); final int padding = mLabelStart != null ? mLabelStart.getHeight() / 8 : 0; final Layout start = mLabelStart; if (start != null) { final int saveCount = canvas.save(); canvas.translate(0, height + padding); start.draw(canvas); canvas.restoreToCount(saveCount); } final Layout mid = mLabelMid; if (mid != null) { final int saveCount = canvas.save(); canvas.translate((width - mid.getWidth()) / 2, height + padding); mid.draw(canvas); canvas.restoreToCount(saveCount); } final Layout end = mLabelEnd; if (end != null) { final int saveCount = canvas.save(); canvas.translate(width - end.getWidth(), height + padding); end.draw(canvas); canvas.restoreToCount(saveCount); } }
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // Draw the switch int switchLeft = mSwitchLeft; int switchTop = mSwitchTop; int switchRight = mSwitchRight; int switchBottom = mSwitchBottom; mTrackDrawable.setBounds(switchLeft, switchTop, switchRight, switchBottom); mTrackDrawable.draw(canvas); canvas.save(); mTrackDrawable.getPadding(mTempRect); int switchInnerLeft = switchLeft + mTempRect.left; int switchInnerTop = switchTop + mTempRect.top; int switchInnerRight = switchRight - mTempRect.right; int switchInnerBottom = switchBottom - mTempRect.bottom; canvas.clipRect(switchInnerLeft, switchTop, switchInnerRight, switchBottom); mThumbDrawable.getPadding(mTempRect); final int thumbPos = (int) (mThumbPosition + 0.5f); int thumbLeft = switchInnerLeft - mTempRect.left + thumbPos; int thumbRight = switchInnerLeft + thumbPos + mThumbWidth + mTempRect.right; mThumbDrawable.setBounds(thumbLeft, switchTop, thumbRight, switchBottom); mThumbDrawable.draw(canvas); // mTextColors should not be null, but just in case if (mTextColors != null) { mTextPaint.setColor( mTextColors.getColorForState(getDrawableState(), mTextColors.getDefaultColor())); } mTextPaint.drawableState = getDrawableState(); Layout switchText = getTargetCheckedState() ? mOnLayout : mOffLayout; if (switchText != null) { canvas.translate( (thumbLeft + thumbRight) / 2 - switchText.getWidth() / 2, (switchInnerTop + switchInnerBottom) / 2 - switchText.getHeight() / 2); switchText.draw(canvas); } canvas.restore(); }
private void sendItemTextBitmap( LiveViewAdapter mLiveViewAdapter, int mPluginId, String tags, String item) { Log.d(LOG_TAG, "sendItemTextBitmap(): item: " + item); // Empty bitmap and link the canvas to it Bitmap bitmap = null; try { bitmap = Bitmap.createBitmap(128, 128, Bitmap.Config.RGB_565); } catch (IllegalArgumentException e) { return; } Canvas canvas = new Canvas(bitmap); // Set the text properties in the canvas TextPaint textPaint = new TextPaint(); textPaint.setTextSize(14); textPaint.setAntiAlias(true); textPaint.setColor(Color.WHITE); // Create the text layout and draw it to the canvas Layout textLayout = new StaticLayout(item, textPaint, 128, Layout.Alignment.ALIGN_NORMAL, 1, 1, false); textLayout.draw(canvas); // textLayout.getWidth(); // textLayout.getHeight(); if (!TextUtils.isEmpty(tags)) { // now smaller text textPaint.setTextSize(10); canvas.translate(1, textLayout.getHeight()); // Create the text layout and draw it to the canvas textLayout = new StaticLayout(tags, textPaint, 128, Layout.Alignment.ALIGN_NORMAL, 1, 1, false); textLayout.draw(canvas); } try { mLiveViewAdapter.sendImageAsBitmap(mPluginId, 1, 1, bitmap); } catch (Exception e) { Log.d(PluginConstants.LOG_TAG, "Failed to send bitmap", e); } }
/** 覆寫觸控事件,分辨出是否為連結的點擊。 */ @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 (widget instanceof LinkTextView) { // 如果實體是LinkTextView ((LinkTextView) widget).linkHit = true; } if (action == MotionEvent.ACTION_UP) { // 放開時 // link[0].onClick(widget); // 開啟連結 String url = ((URLSpan) link[0]).getURL(); Utils.clickUrl(widget, url); } else if (action == MotionEvent.ACTION_DOWN) { // 按下時 Selection.setSelection( buffer, buffer.getSpanStart(link[0]), buffer.getSpanEnd(link[0])); // 選擇連結 } return true; } else { // 不是連結點擊 if (widget instanceof LinkTextView) { // 如果實體是LinkTextView ((LinkTextView) widget).linkHit = false; } Selection.removeSelection(buffer); Touch.onTouchEvent(widget, buffer, event); return false; } } return Touch.onTouchEvent(widget, buffer, event); }
@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; }
@Override public void draw(Canvas canvas) { Layout layout = getLayout(); RectF rectf = mrectf; int left = getCompoundPaddingLeft(); int top = getExtendedPaddingTop(); rectf.set( left + layout.getLineLeft(0) - mPaddingH, top + layout.getLineTop(0) - mPaddingV, Math.min( left + layout.getLineRight(0), getScrollX() + getWidth() - getCompoundPaddingRight()), top + layout.getLineBottom(0) + mPaddingV); canvas.drawRoundRect(rectf, mCornerRadius, mCornerRadius, mpaint); super.draw(canvas); }
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int specModeW = MeasureSpec.getMode(widthMeasureSpec); if (specModeW != MeasureSpec.EXACTLY) { Layout layout = getLayout(); int linesCount = layout.getLineCount(); if (linesCount > 1) { float textRealMaxWidth = 0; for (int n = 0; n < linesCount; ++n) { textRealMaxWidth = Math.max(textRealMaxWidth, layout.getLineWidth(n)); } int w = Math.round(textRealMaxWidth); if (w < getMeasuredWidth()) { super.onMeasure(MeasureSpec.makeMeasureSpec(w, MeasureSpec.AT_MOST), heightMeasureSpec); } } } }
/** @param canvas */ private void drawMETARText(Canvas canvas) { /* * Draw TFRs, weather */ if (mPref.shouldTFRAndMETARShow()) { /* * Write weather report * Use a static layout for showing as overlay and formatted to fit */ float top = getHeight() / mTextDiv * 2 + mFontHeight; if (null != mWeatherLayout) { mPaint.setColor(mWeatherColor); mPaint.setShadowLayer(SHADOW, SHADOW, SHADOW, Color.BLACK); canvas.drawRect(SHADOW, top, getWidth() - SHADOW, mWeatherLayout.getHeight() + top, mPaint); canvas.save(); canvas.translate(SHADOW + 2, top); mPaint.setShadowLayer(0, 0, 0, Color.BLACK); mWeatherLayout.draw(canvas); canvas.restore(); } } }
@Override public boolean onTouchEvent(MotionEvent event) { TextView widget = (TextView) this; Object text = widget.getText(); if (text instanceof Spanned) { Spannable buffer = (Spannable) text; 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) { link[0].onClick(widget); } else if (action == MotionEvent.ACTION_DOWN) { Selection.setSelection( buffer, buffer.getSpanStart(link[0]), buffer.getSpanEnd(link[0])); } return true; } } } return false; }
@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) { link[0].onClick(widget); } commentView.invalidate(); return true; } else { // Changed this to propagate events PostView.this.onTouchEvent(event); return true; } } else { PostView.this.onTouchEvent(event); return true; } }
private Layout makeLayout(CharSequence text) { final CharSequence transformed = (mSwitchTransformationMethod != null) ? mSwitchTransformationMethod.getTransformation(text, this) : text; return new StaticLayout( transformed, mTextPaint, (int) FloatMath.ceil(Layout.getDesiredWidth(transformed, mTextPaint)), Layout.Alignment.ALIGN_NORMAL, 1.f, 0, true); }
private TouchableSpan 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); TouchableSpan[] link = spannable.getSpans(off, off, TouchableSpan.class); TouchableSpan touchedSpan = null; if (link.length > 0) { touchedSpan = link[0]; } return touchedSpan; }
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; } } } }
private int chooseSize(PopupWindow pop, View parentView, CharSequence text, TextView tv) { int wid = tv.getPaddingLeft() + tv.getPaddingRight(); int ht = tv.getPaddingTop() + tv.getPaddingBottom(); /* * Figure out how big the text would be if we laid it out to the * full width of this view minus the border. */ int cap = width - wid; Layout l = new StaticLayout(text, tv.getPaint(), cap, Layout.Alignment.ALIGN_NORMAL, 1, 0, true); float max = 0; for (int i = 0; i < l.getLineCount(); i++) { max = Math.max(max, l.getLineWidth(i)); } /* * Now set the popup size to be big enough for the text plus the border. */ pop.setWidth(width); pop.setHeight(ht + l.getHeight()); return l.getHeight(); }
private Layout makeLabel(CharSequence text) { final Resources res = getResources(); final TextPaint paint = new TextPaint(Paint.ANTI_ALIAS_FLAG); paint.density = res.getDisplayMetrics().density; paint.setCompatibilityScaling(res.getCompatibilityInfo().applicationScale); paint.setColor(mLabelColor); paint.setTextSize(mLabelSize); return new StaticLayout( text, paint, (int) Math.ceil(Layout.getDesiredWidth(text, paint)), Layout.Alignment.ALIGN_NORMAL, 1.f, 0, true); }
private static int getLineAtCoordinate(float y, TextView tv, Layout layout) { if (tv.getLayout() == null) { y -= tv.getCompoundPaddingTop(); } else { y -= tv.getTotalPaddingTop(); } // Clamp the position to inside of the view. y = Math.max(0.0f, y); float bottom = tv.getHeight() - 1; if (tv.getLayout() == null) { bottom -= tv.getCompoundPaddingBottom(); } else { bottom -= tv.getTotalPaddingBottom(); } y = Math.min(bottom, y); y += tv.getScrollY(); return layout.getLineForVertical((int) y); }