下面列出了android.graphics.Paint#getTextWidths ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Don't call this yourself -- exists for Paint to use internally.
* {@hide}
*/
public int getTextWidths(int start, int end, float[] widths, Paint p) {
checkRange("getTextWidths", start, end);
int ret;
if (end <= mGapStart) {
ret = p.getTextWidths(mText, start, end - start, widths);
} else if (start >= mGapStart) {
ret = p.getTextWidths(mText, start + mGapLength, end - start, widths);
} else {
char[] buf = TextUtils.obtain(end - start);
getChars(start, end, buf, 0);
ret = p.getTextWidths(buf, 0, end - start, widths);
TextUtils.recycle(buf);
}
return ret;
}
@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()));
}
/**
* 精确计算文字宽度
*
* @param paint 画笔
* @param str 字符串文本
*/
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(widths[j]);
}
}
return iRet;
}
public int getTextWidth(Paint mPaint, String str) {
float iSum = 0.0f;
if (!(str == null || str.equals(""))) {
int len = str.length();
float[] widths = new float[len];
mPaint.getTextWidths(str, widths);
for (int i = 0; i < len; i++) {
iSum = (float) (((double) iSum) + Math.ceil((double) widths[i]));
}
}
return (int) iSum;
}
private void init() {
mPointColor = ContextCompat.getColor(getContext(), R.color.easy_photos_fg_primary);
mTextColor = ContextCompat.getColor(getContext(), R.color.easy_photos_fg_primary);
mCenterTextColor = ContextCompat.getColor(getContext(), R.color.easy_photos_fg_accent);
mPointPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPointPaint.setStyle(Paint.Style.STROKE);
mPointPaint.setColor(mPointColor);
mPointPaint.setStrokeWidth(2);
mTextPaint = new Paint();
mTextPaint.setColor(mTextColor);
mTextPaint.setStyle(Paint.Style.STROKE);
mTextPaint.setAntiAlias(true);
mTextPaint.setTextSize(24f);
mTextPaint.setTextAlign(Paint.Align.LEFT);
mTextPaint.setAlpha(100);
mFontMetrics = mTextPaint.getFontMetricsInt();
mTextWidths = new float[1];
mTextPaint.getTextWidths("0", mTextWidths);
mCirclePaint = new Paint();
mCirclePaint.setStyle(Paint.Style.FILL);
mCirclePaint.setAlpha(255);
mCirclePaint.setAntiAlias(true);
}
private float getTextWidth(Paint paint, String str) {
float 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 += (float) Math.ceil(widths[j]);
}
}
return iRet;
}
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(widths[j]);
}
}
return iRet;
}
private void init() {
mPointColor = ContextCompat.getColor(getContext(), R.color.easy_photos_fg_primary);
mTextColor = ContextCompat.getColor(getContext(), R.color.easy_photos_fg_primary);
mCenterTextColor = ContextCompat.getColor(getContext(), R.color.easy_photos_fg_accent);
mPointPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPointPaint.setStyle(Paint.Style.STROKE);
mPointPaint.setColor(mPointColor);
mPointPaint.setStrokeWidth(2);
mTextPaint = new Paint();
mTextPaint.setColor(mTextColor);
mTextPaint.setStyle(Paint.Style.STROKE);
mTextPaint.setAntiAlias(true);
mTextPaint.setTextSize(24f);
mTextPaint.setTextAlign(Paint.Align.LEFT);
mTextPaint.setAlpha(100);
mFontMetrics = mTextPaint.getFontMetricsInt();
mTextWidths = new float[1];
mTextPaint.getTextWidths("0", mTextWidths);
mCirclePaint = new Paint();
mCirclePaint.setStyle(Paint.Style.FILL);
mCirclePaint.setAlpha(255);
mCirclePaint.setAntiAlias(true);
}
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;
}
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;
}
/**
* 获取文字的宽度
*
* @param paint 绘制文字的画笔
* @param str 需要计算宽度的字符串
* @return 返回字符串宽度
*/
public static float getTextWidth(Paint paint, String str) {
float textWidth = 0;
if (str != null && str.length() > 0) {
// 记录字符串中每个字符宽度的数组
float[] widths = new float[str.length()];
// 获取字符串中每个字符的宽度到数组
paint.getTextWidths(str, widths);
for (int i = 0; i < str.length(); i++) {
textWidth += (float) Math.ceil(widths[i]);
}
}
return textWidth;
}
/**
* 获取文字的宽度
*
* @param paint
* @param str
* @return
*/
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(widths[j]);
}
}
return iRet;
}
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;
}
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;
}
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(widths[j]);
}
}
return iRet;
}
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;
}
public int getTextWidth(Paint paint, String str) { //calculate text width
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;
}
public int getTextWidth(Paint paint, String str) { //calculate text width
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;
}
private void drawText(Canvas canvas) {
Map<Integer, String> lineNumbles = getLineNumbers();
Layout layout = getLayout();
int selectLine = getSelectLine();
int range[] = new int[4];
{// 计算需要绘制的行号所需要的范围
int clipLeft = 0;
int clipTop = (getScrollView().getScrollY() == 0) ? 0
: getExtendedPaddingTop() + getScrollView().getScrollY()
- getScrollView().getPaddingTop();
int clipRight = getWidth();
int clipBottom = clipTop + getScrollView().getHeight();
Rect rect = new Rect(clipLeft, clipTop, clipRight, clipBottom);
getLineRangeForDraw(rect, range);
}
int firstLine = range[0];
int lastLine = range[1];
if (firstLine < 0) {
return;
}
int previousLineBottom = layout.getLineTop(firstLine);
int previousLineEnd = layout.getLineStart(firstLine);
int lineCount = getLineCount();
Paint paint = getPaint();
for (int lineNum = firstLine; lineNum <= lastLine
&& lineNum < lineCount; lineNum++) {
int start = previousLineEnd;
previousLineEnd = layout.getLineStart(lineNum + 1);
int end = layout.getLineVisibleEnd(lineNum);
int ltop = previousLineBottom;
int lbottom = layout.getLineTop(lineNum + 1);
previousLineBottom = lbottom;
int lbaseline = lbottom - layout.getLineDescent(lineNum);
int left = getPaddingLeft();
// 绘制选择行背景
if (lineNum == selectLine) {
paint.setColor(lineNumberBackgroundColor);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(mLineNumberBgStrokeWidth);
canvas.drawRect(getPaddingLeft() - mLineNumberBgStrokeWidth, ltop, getRight() - getPaddingRight() + mLineNumberBgStrokeWidth, lbottom, paint);
paint.setStyle(Paint.Style.FILL);
}
// 绘制行号
String lineNumberText = lineNumbles.get(lineNum);
if (lineNumberText != null) {
paint.setColor(lineNumberColor);
canvas.drawText(lineNumberText, 0, lineNumberText.length(), mNumberPadding,
lbaseline, paint);
}
int textLength = getText().length();
// 绘制文字
if (start < textLength) {
//计算需要绘制的文字位置
//获取改行所有文字宽度
float[] widths = new float[end - start + 1];
paint.getTextWidths(getText(), start, end, widths);
//计算获取看到的文字第一个位置,和对应的偏移x
float firstNeedDrawPos[] = getLineFirstCharPosForDraw(widths);
int firstPos = (int) firstNeedDrawPos[0];
float offsetX = firstNeedDrawPos[1];
int maxOffX = getViewScrollX() + getVisibleWidth();
// 文字着色
for (int i = start + firstPos; i < end && i < textLength; ) {
if (offsetX > maxOffX) {
break;
}
int color = getCodeColor(i);
{
float fontWidths = widths[i - start];
int fontCount = 1;
for (int j = i + 1; j < end && j < textLength; j++) {
if (color == getCodeColor(j)) {
fontCount++;
fontWidths += widths[j - start];
} else {
break;
}
}
if (color == 0) {
color = defaultTextColor;
}
paint.setColor(color);
canvas.drawText(getText(), i, i + fontCount, left + offsetX, lbaseline, paint);
i += fontCount;
offsetX += fontWidths;
}
}
}
}
}
protected int drawLegend(Canvas canvas, DefaultRenderer defaultrenderer, String as[], int i, int j, int k, int l,
int i1, int j1, Paint paint, boolean flag)
{
float f = 32F;
if (defaultrenderer.isShowLegend())
{
float f1 = i;
float f2 = f + (float)((k + i1) - j1);
paint.setTextAlign(android.graphics.Paint.Align.LEFT);
paint.setTextSize(defaultrenderer.getLegendTextSize());
int k1 = Math.min(as.length, defaultrenderer.getSeriesRendererCount());
int l1 = 0;
while (l1 < k1)
{
SimpleSeriesRenderer simpleseriesrenderer = defaultrenderer.getSeriesRendererAt(l1);
float f3 = getLegendShapeWidth(l1);
float f4;
if (simpleseriesrenderer.isShowLegendItem())
{
String s = as[l1];
float af[];
float f5;
int i2;
if (as.length == defaultrenderer.getSeriesRendererCount())
{
paint.setColor(simpleseriesrenderer.getColor());
} else
{
paint.setColor(0xffcccccc);
}
af = new float[s.length()];
paint.getTextWidths(s, af);
f5 = 0.0F;
i2 = af.length;
for (int j2 = 0; j2 < i2; j2++)
{
f5 += af[j2];
}
float f6 = f5 + (10F + f3);
float f7 = f1 + f6;
if (l1 > 0 && getExceed(f7, defaultrenderer, j, l))
{
f1 = i;
f2 += defaultrenderer.getLegendTextSize();
float f9 = f + defaultrenderer.getLegendTextSize();
f7 = f1 + f6;
f4 = f9;
} else
{
f4 = f;
}
if (getExceed(f7, defaultrenderer, j, l))
{
float f8 = (float)j - f1 - f3 - 10F;
if (isVertical(defaultrenderer))
{
f8 = (float)l - f1 - f3 - 10F;
}
int k2 = paint.breakText(s, true, f8, af);
s = (new StringBuilder()).append(s.substring(0, k2)).append("...").toString();
}
if (!flag)
{
drawLegendShape(canvas, simpleseriesrenderer, f1, f2, l1, paint);
drawString(canvas, s, 5F + (f1 + f3), f2 + 5F, paint);
}
f1 += f6;
} else
{
f4 = f;
}
l1++;
f = f4;
}
}
return Math.round(f + defaultrenderer.getLegendTextSize());
}