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 updateTextSize() { int length = CHARACTERS.length; float[] widths = new float[length]; mPaint.getTextWidths(CHARACTERS, 0, length, widths); mCharWidth = 0.0f; for (float f : widths) { mCharWidth = Math.max(mCharWidth, f); } Paint.FontMetrics fm = mPaint.getFontMetrics(); mCharHeight = fm.bottom - fm.top; }
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 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; } }
p.getTextWidths(editable.toString(), widths); float curWidth = 0.0f; int lastWSPos = -1;
private float[] buildTextPositions(String text, float y, Paint paint) { float[] widths = new float[text.length()]; // initially get the widths for each char int n = paint.getTextWidths(text, widths); // now popuplate the array, interleaving spaces for the Y values float[] pos = new float[n * 2]; float accumulatedX = 0; for (int i = 0; i < n; i++) { pos[i*2 + 0] = accumulatedX; pos[i*2 + 1] = y; accumulatedX += widths[i]; } return pos; }
private float[] buildTextPositions(String text, float y, Paint paint) { float[] widths = new float[text.length()]; // initially get the widths for each char int n = paint.getTextWidths(text, widths); // now popuplate the array, interleaving spaces for the Y values float[] pos = new float[n * 2]; float accumulatedX = 0; for (int i = 0; i < n; i++) { pos[i*2 + 0] = accumulatedX; pos[i*2 + 1] = y; accumulatedX += widths[i]; } return pos; }
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; }
@Override public float getWidthAt(int index) { float[] tab = (this.widths==null) ? null : this.widths.get(); if (tab==null) { tab = new float[this.characters.length]; Paint paint = getPaint(); paint.getTextWidths(this.characters, 0, tab.length, tab); this.widths = new SoftReference<>(tab); } return tab[index]; }
private void drawReading(Canvas canvas){ Path path = new Path(); String message = String.format("%d", (int)this.mCurrentSpeed); float[] widths = new float[message.length()]; readingPaint.getTextWidths(message, widths); float advance = 0; for(double width:widths) advance += width; path.moveTo(centerX - advance/2, centerY); path.lineTo(centerX + advance/2, centerY); canvas.drawTextOnPath(message, path, 0f, 0f, readingPaint); }
/** * 获取Text宽度 * * @param paint * @param str 文本内容 * @return */ private int getTextWidth(Paint paint, String str) { int mRet = 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++) { mRet += (int) Math.ceil(widths[j]); } } return mRet; } }
@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())); }
private void showText(Canvas canvas, String text, Paint.Align align) { // mPaint.setTextAlign(align); Rect bounds = new Rect(); float[] widths = new float[text.length()]; int count = mPaint.getTextWidths(text, 0, text.length(), widths); float w = mPaint.measureText(text, 0, text.length()); mPaint.getTextBounds(text, 0, text.length(), bounds); mPaint.setColor(0xFF88FF88); canvas.drawRect(bounds, mPaint); mPaint.setColor(Color.BLACK); canvas.drawText(text, 0, 0, mPaint); float[] pts = new float[2 + count*2]; float x = 0; float y = 0; pts[0] = x; pts[1] = y; for (int i = 0; i < count; i++) { x += widths[i]; pts[2 + i*2] = x; pts[2 + i*2 + 1] = y; } mPaint.setColor(Color.RED); mPaint.setStrokeWidth(0); canvas.drawLine(0, 0, w, 0, mPaint); mPaint.setStrokeWidth(5); canvas.drawPoints(pts, 0, (count + 1) << 1, mPaint); }
private void showText(Canvas canvas, String text, Paint.Align align) { // mPaint.setTextAlign(align); Rect bounds = new Rect(); float[] widths = new float[text.length()]; int count = mPaint.getTextWidths(text, 0, text.length(), widths); float w = mPaint.measureText(text, 0, text.length()); mPaint.getTextBounds(text, 0, text.length(), bounds); mPaint.setColor(0xFF88FF88); canvas.drawRect(bounds, mPaint); mPaint.setColor(Color.BLACK); canvas.drawText(text, 0, 0, mPaint); float[] pts = new float[2 + count*2]; float x = 0; float y = 0; pts[0] = x; pts[1] = y; for (int i = 0; i < count; i++) { x += widths[i]; pts[2 + i*2] = x; pts[2 + i*2 + 1] = y; } mPaint.setColor(Color.RED); mPaint.setStrokeWidth(0); canvas.drawLine(0, 0, w, 0, mPaint); mPaint.setStrokeWidth(5); canvas.drawPoints(pts, 0, (count + 1) << 1, mPaint); }
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); }
@Override public void draw() { e_canvas = b_engine.getCanvas(); if (b_width == 0 || b_height == 0) { float[] widths = new float[1]; // 获取单个汉字的宽度 b_textPaint.getTextWidths("蛤", widths); b_text = b_text != null ? b_text : ""; b_textWidth = widths[0] * b_text.length(); b_singleWidth = widths[0]; b_width = (int) (b_text.length() * widths[0] + 2 * DisplayUtils.dip2px(16)); b_height = (int) (widths[0] + 2 * DisplayUtils.dip2px(8)); } int x = b_position.x; int y = b_position.y; int w = (int) (b_width * b_scale.x); int h = (int) (b_height * b_scale.y); if (!b_firstInit) { b_rect = new Rect(x, y, x + w, y + h); b_firstInit = true; } if (!b_zoomCenter) { b_rect = new Rect(x, y, x + w, y + h); } e_canvas.drawRect(b_rect, paint); e_canvas.drawText(b_text, x + (b_width / 2 - b_textWidth / 2), y + (b_height / 2 + b_singleWidth / 2), b_textPaint); }
private void init(int d_time) { this.left_X = (int) (DisplayUtils.dip2px(2)); this.left_Y = (int) (DisplayUtils.dip2px(2)); this.textPaint = new Paint(Paint.ANTI_ALIAS_FLAG); this.textPaint.setColor(colorMap.get(LogLevel.VERBOSE)); this.textPaint.setTextSize(20); float[] widths = new float[1]; textPaint.getTextWidths("H", widths); this.logListSize = (int) ((UIdefaultData.screenHeight - DisplayUtils.dip2px(4)) / widths[0] + DisplayUtils.dip2px(4)); this.line_width = (int) (widths[0] + DisplayUtils.dip2px(2)); Log.e("listX", left_X + " "); LogHandler.init(new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case LogHandler.LOGCAT: while (logLinesList.size() >= logListSize) logLinesList.remove(0); logLinesList.add((LogHandler.Line) msg.obj); break; } } }, d_time); LogHandler.getInstance(); }
@Override protected void onDraw(Canvas canvas) { Log.d("RandomTextView","draw"); if (firstIn) { firstIn = false; super.onDraw(canvas); p = getPaint(); Paint.FontMetricsInt fontMetrics = p.getFontMetricsInt(); measuredHeight = getMeasuredHeight(); Log.d("EEEEEEE", "onDraw: " + measuredHeight); baseline = (measuredHeight - fontMetrics.bottom + fontMetrics.top) / 2 - fontMetrics.top; float[] widths = new float[4]; p.getTextWidths("9999", widths); f0 = widths[0]; invalidate(); } drawNumber(canvas); // if (auto) { // for (int j = 0; j < numLength; j++) { // // pianyiliangSum[j] -= pianyilianglist[j]; // postInvalidateDelayed(17); // } // } }
p.getTextWidths(editable.toString(), widths); float curWidth = 0.0f; int lastWSPos = -1;