Beispiel #1
0
 public static synchronized float glyphWidth(char glyph, String metricString, TextPaint paint) {
   if (!mCache.containsKey(metricString)) {
     mCache.put(metricString, new HashMap<Character, Float>());
   }
   Map<Character, Float> map = mCache.get(metricString);
   if (!map.containsKey(glyph)) {
     char[] glyphs = new char[] {glyph};
     float[] widths = new float[1];
     paint.getTextWidths(glyphs, 0, 1, widths);
     map.put(glyph, widths[0]);
     return widths[0];
   }
   return map.get(glyph);
 }
  private void init(Context context, AttributeSet attrs) {
    mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
    mTextPaint.setStyle(Paint.Style.FILL);
    mTextPaint.setTextAlign(Paint.Align.LEFT);

    mSpacing = new float[1];
    mTextPaint.getTextWidths(new char[] {' '}, 0, 1, mSpacing);

    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.IconicLabelView);
    setIcon(a.getDrawable(R.styleable.IconicLabelView_label_icon));
    setBackground(a.getColor(R.styleable.IconicLabelView_bg_color, -1));
    setCornerRadius(a.getFloat(R.styleable.IconicLabelView_corner_radius, 5.0f));
    setTextColor(a.getColor(R.styleable.IconicLabelView_text_color, -1));
    setTextSize(a.getDimensionPixelSize(R.styleable.IconicLabelView_text_size, 14));
    setTexts(
        a.getString(R.styleable.IconicLabelView_text1),
        a.getString(R.styleable.IconicLabelView_text2));
    a.recycle();
  }
  /**
   * Returns the advance widths for a uniform left-to-right run of text with no style changes in the
   * middle of the run. If any style is replacement text, the first character will isCancelled the
   * width of the replacement and the remaining characters will isCancelled a width of 0.
   *
   * @param paint the paint, will not be modified
   * @param workPaint a paint to modify; on return will reflect the original paint plus the effect
   *     of all spans on the run
   * @param text the text
   * @param start the start of the run
   * @param end the limit of the run
   * @param widths array to receive the advance widths of the characters. Must be at least a large
   *     as (end - start).
   * @param fmi FontMetrics information; can be null
   * @return the actual number of widths returned
   */
  public static int getTextWidths(
      TextPaint paint,
      TextPaint workPaint,
      Spanned text,
      int start,
      int end,
      float[] widths,
      Paint.FontMetricsInt fmi) {
    MetricAffectingSpan[] spans = text.getSpans(start, end, MetricAffectingSpan.class);

    ReplacementSpan replacement = null;
    workPaint.set(paint);

    for (MetricAffectingSpan span : spans) {
      if (span instanceof ReplacementSpan) {
        replacement = (ReplacementSpan) span;
      } else {
        span.updateMeasureState(workPaint);
      }
    }

    if (replacement == null) {
      workPaint.getFontMetricsInt(fmi);
      workPaint.getTextWidths(text, start, end, widths);
    } else {
      int wid = replacement.getSize(workPaint, text, start, end, fmi);

      if (end > start) {
        widths[0] = wid;
        for (int i = start + 1; i < end; i++) {
          widths[i - start] = 0;
        }
      }
    }
    return end - start;
  }