Java Code Examples for android.graphics.Paint.getTextWidths()

The following are Jave code examples for showing how to use getTextWidths() of the android.graphics.Paint class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: CommentView   File: TextCell.java   View Source Code Vote up 6 votes
public int a(Paint paint, int i, int i2, float[] fArr) {
    int i3 = 0;
    if (!TextUtils.isEmpty(this.text)) {
        int i4 = i + i2;
        if (i4 > this.text.length()) {
            i4 = this.text.length();
        }
        if (i < i4) {
            Typeface d = d(paint);
            if ((this.useDefaultFont || !this.useHanyiColorFont) && (paint instanceof ETPaint)) {
                paint = ((ETPaint) paint).a();
            }
            i3 = paint.getTextWidths(this.text, i, i4, fArr);
            if (d != null) {
                paint.setTypeface(d);
            }
        }
    }
    return i3;
}
 
Example 2
Project: revolution-irc   File: FixedWidthTimestampSpan.java   View Source Code Vote up 6 votes
@Override
public int getSize(@NonNull Paint paint, CharSequence text, int start, int end,
                   @Nullable Paint.FontMetricsInt fm) {
    paint.getTextWidths(MEASURE_NUMBER_CHARS, mNumberWidths);
    float mw = 0.f;
    char ld = '0';
    for (int i = MEASURE_NUMBER_CHARS.length() - 1; i >= 0; --i) {
        if (mNumberWidths[i] > mw) {
            mNumberWidths[i] = mw;
            ld = MEASURE_NUMBER_CHARS.charAt(i);
        }
    }
    CharSequence s = text.subSequence(start - mPreOffset, start);
    String rs = sMatchNumbersRegex.matcher(s).replaceAll(String.valueOf(ld));
    return (int) (paint.measureText(rs) - paint.measureText(s, 0, s.length()));
}
 
Example 3
Project: GitHub   File: ShoppingView.java   View Source Code Vote up 5 votes
private int getTextWidth(Paint paint, String str) {
    int iRet = 0;
    if (str != null && str.length() > 0) {
        int len = str.length();
        float[] widths = new float[len];
        paint.getTextWidths(str, widths);
        for (int j = 0; j < len; j++) {
            iRet += (int) Math.ceil(widths[j]);
        }
    }
    return iRet;
}
 
Example 4
Project: GitHub   File: EastAsianWidth.java   View Source Code Vote up 5 votes
@Override
public void measure(char[] charArray, int start, int end,
		byte[] wideAttribute, Paint paint, int charWidth) {
	paint.getTextWidths(charArray, start, end, mWidths);
	final int N = end - start;
	for (int i = 0; i < N; i++)
		wideAttribute[i] = (byte) (((int) mWidths[i] != charWidth) ?
				AndroidCharacter.EAST_ASIAN_WIDTH_WIDE :
				AndroidCharacter.EAST_ASIAN_WIDTH_NARROW);
}
 
Example 5
Project: GitHub   File: WheelView.java   View Source Code Vote up 5 votes
public int getTextWidth(Paint paint, String str) {//计算文字宽度
    int iRet = 0;
    if (str != null && str.length() > 0) {
        int len = str.length();
        float[] widths = new float[len];
        paint.getTextWidths(str, widths);
        for (int j = 0; j < len; j++) {
            iRet += (int) Math.ceil(widths[j]);
        }
    }
    return iRet;
}
 
Example 6
Project: timePicker   File: WheelView.java   View Source Code Vote up 5 votes
public int getTextWidth(Paint paint, String str) {
    int iRet = 0;
    if (str != null && str.length() > 0) {
        int len = str.length();
        float[] widths = new float[len];
        paint.getTextWidths(str, widths);
        for (int j = 0; j < len; j++) {
            iRet += (int) Math.ceil(widths[j]);
        }
    }
    return iRet;
}
 
Example 7
Project: Android-PickerDialog   File: WheelView.java   View Source Code Vote up 5 votes
public int getTextWidth(Paint paint, String str) {
    int iRet = 0;
    if (str != null && str.length() > 0) {
        int len = str.length();
        float[] widths = new float[len];
        paint.getTextWidths(str, widths);
        for (int j = 0; j < len; j++) {
            iRet += (int) Math.ceil(widths[j]);
        }
    }
    return iRet;
}
 
Example 8
Project: AndroidPicker   File: WheelView.java   View Source Code Vote up 5 votes
public int getTextWidth(Paint paint, String str) {//计算文字宽度
    int iRet = 0;
    if (str != null && str.length() > 0) {
        int len = str.length();
        float[] widths = new float[len];
        paint.getTextWidths(str, widths);
        for (int j = 0; j < len; j++) {
            iRet += (int) Math.ceil(widths[j]);
        }
    }
    return iRet;
}
 
Example 9
Project: xlight_android_native   File: WheelView.java   View Source Code Vote up 5 votes
public int getTextWidth(Paint paint, String str) {
    int iRet = 0;
    if (str != null && str.length() > 0) {
        int len = str.length();
        float[] widths = new float[len];
        paint.getTextWidths(str, widths);
        for (int j = 0; j < len; j++) {
            iRet += (int) Math.ceil(widths[j]);
        }
    }
    return iRet;
}
 
Example 10
Project: CXJPadProject   File: WheelView.java   View Source Code Vote up 5 votes
private int obtainTextWidth(Paint paint, String str) {
    int iRet = 0;
    if (str != null && str.length() > 0) {
        int len = str.length();
        float[] widths = new float[len];
        paint.getTextWidths(str, widths);
        for (int j = 0; j < len; j++) {
            iRet += (int) Math.ceil(widths[j]);
        }
    }
    return iRet;
}
 
Example 11
Project: boohee_v5.6   File: ViewUtils.java   View Source Code Vote up 5 votes
public static int getTextWidth(Paint paint, String str) {
    int iRet = 0;
    if (str != null && str.length() > 0) {
        int len = str.length();
        float[] widths = new float[len];
        paint.getTextWidths(str, widths);
        for (int j = 0; j < len; j++) {
            iRet += (int) Math.ceil((double) widths[j]);
        }
    }
    return iRet;
}
 
Example 12
Project: RLibrary   File: WheelView.java   View Source Code Vote up 5 votes
public int getTextWidth(Paint paint, String str) {//计算文字宽度
    int iRet = 0;
    if (str != null && str.length() > 0) {
        int len = str.length();
        float[] widths = new float[len];
        paint.getTextWidths(str, widths);
        for (int j = 0; j < len; j++) {
            iRet += (int) Math.ceil(widths[j]);
        }
    }
    return iRet;
}
 
Example 13
Project: buildAPKsApps   File: AbstractChart.java   View Source Code Vote up 4 votes
/**
 * Draws the chart legend.
 * 
 * @param canvas the canvas to paint to
 * @param renderer the series renderer
 * @param titles the titles to go to the legend
 * @param left the left X value of the area to draw to
 * @param right the right X value of the area to draw to
 * @param y the y value of the area to draw to
 * @param width the width of the area to draw to
 * @param height the height of the area to draw to
 * @param legendSize the legend size
 * @param paint the paint to be used for drawing
 * @param calculate if only calculating the legend size
 * 
 * @return the legend height
 */
protected int drawLegend(Canvas canvas, DefaultRenderer renderer, String[] titles, int left,
    int right, int y, int width, int height, int legendSize, Paint paint, boolean calculate) {
  float size = 32;
  if (renderer.isShowLegend()) {
    float currentX = left;
    float currentY = y + height - legendSize + size;
    paint.setTextAlign(Align.LEFT);
    paint.setTextSize(renderer.getLegendTextSize());
    int sLength = Math.min(titles.length, renderer.getSeriesRendererCount());
    for (int i = 0; i < sLength; i++) {
      SimpleSeriesRenderer r = renderer.getSeriesRendererAt(i);
      final float lineSize = getLegendShapeWidth(i);
      if (r.isShowLegendItem()) {
        String text = titles[i];
        if (titles.length == renderer.getSeriesRendererCount()) {
          paint.setColor(r.getColor());
        } else {
          paint.setColor(Color.LTGRAY);
        }
        float[] widths = new float[text.length()];
        paint.getTextWidths(text, widths);
        float sum = 0;
        for (float value : widths) {
          sum += value;
        }
        float extraSize = lineSize + 10 + sum;
        float currentWidth = currentX + extraSize;

        if (i > 0 && getExceed(currentWidth, renderer, right, width)) {
          currentX = left;
          currentY += renderer.getLegendTextSize();
          size += renderer.getLegendTextSize();
          currentWidth = currentX + extraSize;
        }
        if (getExceed(currentWidth, renderer, right, width)) {
          float maxWidth = right - currentX - lineSize - 10;
          if (isVertical(renderer)) {
            maxWidth = width - currentX - lineSize - 10;
          }
          int nr = paint.breakText(text, true, maxWidth, widths);
          text = text.substring(0, nr) + "...";
        }
        if (!calculate) {
          drawLegendShape(canvas, r, currentX, currentY, i, paint);
          drawString(canvas, text, currentX + lineSize + 5, currentY + 5, paint);
        }
        currentX += extraSize;
      }
    }
  }
  return Math.round(size + renderer.getLegendTextSize());
}
 
Example 14
Project: exciting-app   File: MergeFactory.java   View Source Code Vote up 4 votes
public void drawTextRange(Canvas canvas, String text, RectF textRect, Paint textPaint, int color, int shadowColor) {
    Paint.FontMetrics fm = textPaint.getFontMetrics();
    // 计算行高
    //float textHeight = (int) (Math.ceil(fm.descent - fm.ascent));
    float textHeight = fm.bottom - fm.top - 8;
    float rectHeight = textRect.height();
    if (textHeight > rectHeight)
        return;
    // 字体宽度
    float widthText = textPaint.measureText(text);
    float rectWidth = textRect.width() * scaleImg;
    // 行数
    float line = widthText / rectWidth;
    float baseline = ((textRect.bottom + textRect.top) * scaleImg - fm.bottom - fm.top) / 2;
    if (line <= 1) {
        textPaint.setColor(shadowColor);
        canvas.drawText(text, textRect.centerX() * scaleImg - 2, baseline, textPaint);
        canvas.drawText(text, textRect.centerX() * scaleImg + 2, baseline, textPaint);
        canvas.drawText(text, textRect.centerX() * scaleImg, baseline + 2, textPaint);
        canvas.drawText(text, textRect.centerX() * scaleImg, baseline - 2, textPaint);
        textPaint.setColor(color);
        canvas.drawText(text, textRect.centerX() * scaleImg, baseline, textPaint);
    } else {
        baseline -= (textHeight / 2 + 12);
        float[] widths = new float[text.length()];
        textPaint.getTextWidths(text, widths);
        int newline = formatFloatToInt(line);
        // 计算基线
        //float contentHeight = newline * textHeight;
        //if (contentHeight < rectHeight) {
        //    baseline = textRect.top + (rectHeight - contentHeight) / 2 + textHeight;
        //}
        float baseWidth = 0;
        // idline为行数
        for (int index = 0, idline = 0, lastindex = 0; index < widths.length; index++) {
            // 本行宽度
            float temp = (baseWidth + widths[index]);
            float dis = rectWidth - temp;
            if (index + 1 != widths.length) {
                if (dis >= (widths[index + 1])) {
                    baseWidth = temp;
                    continue;
                }
                textPaint.setColor(shadowColor);
                canvas.drawText(text, lastindex, index + 1, (textRect.centerX() * scaleImg) - 2, baseline + idline * textHeight, textPaint);
                canvas.drawText(text, lastindex, index + 1, (textRect.centerX() * scaleImg) + 2, baseline + idline * textHeight, textPaint);
                canvas.drawText(text, lastindex, index + 1, (textRect.centerX() * scaleImg), baseline + idline * textHeight + 2, textPaint);
                canvas.drawText(text, lastindex, index + 1, (textRect.centerX() * scaleImg), baseline + idline * textHeight - 2, textPaint);
                textPaint.setColor(color);
                canvas.drawText(text, lastindex, index + 1, (textRect.centerX() * scaleImg), baseline + idline * textHeight, textPaint);

                baseWidth = 0;
                lastindex = index + 1;
                idline++;
                if ((baseline + idline * textHeight) > (textRect.bottom * scaleImg)) {
                    break;
                }
                continue;
            }

            if ((baseline + idline * textHeight) > (textRect.bottom * scaleImg)) {
                break;
            }
            textPaint.setColor(shadowColor);
            canvas.drawText(text, lastindex, index + 1, (textRect.centerX() * scaleImg) - 2, baseline + idline * textHeight, textPaint);
            canvas.drawText(text, lastindex, index + 1, (textRect.centerX() * scaleImg) + 2, baseline + idline * textHeight, textPaint);
            canvas.drawText(text, lastindex, index + 1, (textRect.centerX() * scaleImg), baseline + idline * textHeight + 2, textPaint);
            canvas.drawText(text, lastindex, index + 1, (textRect.centerX() * scaleImg), baseline + idline * textHeight - 2, textPaint);
            textPaint.setColor(color);
            canvas.drawText(text, lastindex, index + 1, (textRect.centerX() * scaleImg), baseline + idline * textHeight, textPaint);
        }
    }
}