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;
 }
  protected boolean needsEllipsis() {
    final int width = getAvailableWidth();
    if (width <= 0) {
      return false;
    }

    final Layout layout = getLayout();
    return (layout != null && layout.getLineWidth(0) > width);
  }
Exemple #3
0
  @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);
        }
      }
    }
  }
  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();
    }