List of usage examples for android.graphics Paint measureText
public float measureText(CharSequence text, int start, int end)
From source file:org.cocos2dx.lib.Cocos2dxBitmap.java
private static LinkedList<String> divideStringWithMaxWidth(Paint paint, String content, int width) { int charLength = content.length(); int start = 0; int tempWidth = 0; LinkedList<String> strList = new LinkedList<String>(); /*/*from w ww. ja v a2s.com*/ * Break a String into String[] by the width & should wrap the word */ for (int i = 1; i <= charLength; ++i) { tempWidth = (int) Math.ceil(paint.measureText(content, start, i)); if (tempWidth >= width) { int lastIndexOfSpace = content.substring(0, i).lastIndexOf(" "); if (lastIndexOfSpace != -1 && lastIndexOfSpace > start) { /** * Should wrap the word */ strList.add(content.substring(start, lastIndexOfSpace)); i = lastIndexOfSpace; } else { /* * Should not exceed the width */ if (tempWidth > width) { strList.add(content.substring(start, i - 1)); /* * compute from previous char */ --i; } else { strList.add(content.substring(start, i)); } } // remove spaces at the beginning of a new line while (content.indexOf(i++) == ' ') { } start = i; } } /* * Add the last chars */ if (start < charLength) { strList.add(content.substring(start)); } return strList; }
From source file:org.cocos2dx.lib.Cocos2dxBitmap.java
private static TextProperty computeTextProperty(String content, Paint paint, int maxWidth, int maxHeight) { FontMetricsInt fm = paint.getFontMetricsInt(); int h = (int) Math.ceil(fm.bottom - fm.top); int maxContentWidth = 0; String[] lines = splitString(content, maxHeight, maxWidth, paint); if (maxWidth != 0) { maxContentWidth = maxWidth;//from w ww . j av a2 s.c o m } else { /* * Compute the max width */ int temp = 0; for (String line : lines) { temp = (int) Math.ceil(paint.measureText(line, 0, line.length())); if (temp > maxContentWidth) { maxContentWidth = temp; } } } return new TextProperty(maxContentWidth, h, lines); }
From source file:org.mariotaku.twidere.text.OriginalStatusSpan.java
@Override public int getSize(final Paint paint, final CharSequence text, final int start, final int end, final Paint.FontMetricsInt fm) { paint.setTextSize(paint.getTextSize() * 0.8f); return (int) paint.measureText(text, start, end) + mPadding * 2; }
From source file:org.mariotaku.twidere.text.OriginalStatusSpan.java
@Override public void draw(final Canvas canvas, final CharSequence text, final int start, final int end, final float x, final int top, final int y, final int bottom, final Paint paint) { if (!(paint instanceof TextPaint)) return;/*from w ww .j ava2s .c o m*/ final TextPaint tp = (TextPaint) paint; mBounds.left = x; mBounds.right = x + paint.measureText(text, start, end) + mPadding * 2; mBounds.top = top; mBounds.bottom = bottom; final int innerTextColor = TwidereColorUtils.getContrastYIQ(tp.linkColor, ThemeUtils.ACCENT_COLOR_THRESHOLD, mDarkLightColors[0], mDarkLightColors[1]); mPaint.setStyle(Paint.Style.FILL); mPaint.setColor(tp.linkColor); mBounds.inset(mPaint.getStrokeWidth() / 2, mPaint.getStrokeWidth() / 2); canvas.drawRoundRect(mBounds, mCornerRadius, mCornerRadius, mPaint); mBounds.inset(-mPaint.getStrokeWidth() / 2, -mPaint.getStrokeWidth() / 2); mPaint.setStyle(Paint.Style.STROKE); mPaint.setColor( ColorUtils.compositeColors(ColorUtils.setAlphaComponent(innerTextColor, 0x80), tp.linkColor)); mBounds.inset(mPaint.getStrokeWidth() / 2, mPaint.getStrokeWidth() / 2); canvas.drawRoundRect(mBounds, mCornerRadius, mCornerRadius, mPaint); paint.setColor(innerTextColor); canvas.drawText(text, start, end, x + mPadding, top + (bottom - top) / 2 - (paint.descent() + paint.ascent()) / 2, paint); }
From source file:com.github.kubatatami.RoundedView.java
private void drawText(Canvas canvas) { Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setTextSize(getResources().getDimension(R.dimen.item_circle_text_size)); Rect areaRect = new Rect(0, 0, canvas.getWidth(), canvas.getHeight()); RectF bounds = new RectF(areaRect); bounds.right = paint.measureText(text, 0, text.length()); bounds.bottom = paint.descent() - paint.ascent(); bounds.left += (areaRect.width() - bounds.right) / 2.0f; bounds.top += (areaRect.height() - bounds.bottom) / 2.0f; paint.setColor(Color.WHITE);// ww w .j av a 2 s.c o m canvas.drawText(text, bounds.left, bounds.top - paint.ascent(), paint); }
From source file:ggikko.me.steppertest.stepper.RoundedView.java
private void drawText(Canvas canvas) { Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setTextSize(getResources().getDimension(R.dimen.item_circle_text_size)); Rect areaRect = new Rect(0, 0, canvas.getWidth(), canvas.getHeight()); RectF bounds = new RectF(areaRect); bounds.right = paint.measureText(text, 0, text.length()); bounds.bottom = paint.descent() - paint.ascent(); bounds.left += (areaRect.width() - bounds.right) / 2.0f; bounds.top += (areaRect.height() - bounds.bottom) / 2.0f; paint.setColor(Color.WHITE);/*from w w w.j ava 2 s . co m*/ canvas.drawText(text, bounds.left, bounds.top - paint.ascent(), paint); }
From source file:com.zzq.viewpagerindicator.TitlePageIndicator.java
private Rect calcBounds(int index, Paint paint) { // Calculate the text bounds Rect bounds = new Rect(); CharSequence title = getTitle(index); bounds.right = (int) paint.measureText(title, 0, title.length()); bounds.bottom = (int) (paint.descent() - paint.ascent()); return bounds; }
From source file:com.chibatching.imgindicatortab.ImgIndicatorTab.java
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (mViewPager == null) { return;// ww w.ja va2 s .c o m } int tabCount = mViewPager.getAdapter().getCount(); if (tabCount == 0) { return; } if (mCurrentPage >= tabCount) { setCurrentItem(tabCount - 1); } float tabWidth = (float) (getWidth() - getPaddingLeft() - getPaddingRight()) / tabCount; float centerY = getPaddingTop() + (float) (getHeight() - getPaddingTop() - getPaddingBottom()) / 2; for (int i = 0; i < tabCount; i++) { float tabCenterX = (tabWidth * i) + getPaddingLeft() + tabWidth / 2; CharSequence text = mViewPager.getAdapter().getPageTitle(i); Paint textPaint; if (mTextCurrentPage == i) { textPaint = mSelectedTextPaint; } else { textPaint = mDeselectedTextPaint; } Paint.FontMetrics metrics = textPaint.getFontMetrics(); float textWidth = textPaint.measureText(text, 0, text.length()); float textX = tabCenterX - textWidth / 2; float textY = centerY - (metrics.ascent + metrics.descent) / 2; canvas.drawText(text, 0, text.length(), textX, textY, textPaint); } if (mIndicator == null) { return; } Bitmap indicatorBitmap = ((BitmapDrawable) mIndicator).getBitmap(); int bitmapWidth = indicatorBitmap.getWidth(); int bitmapHeight = indicatorBitmap.getHeight(); float scale = 1f; if (mFitIndicator || bitmapWidth > tabWidth) { scale = tabWidth / bitmapWidth; } if (bitmapHeight * scale > getHeight()) { scale = getHeight() / bitmapHeight; } float tabCenterX = tabWidth * (mCurrentPage + mPositionOffset) + getPaddingLeft() + tabWidth / 2; float bitmapX = tabCenterX - (bitmapWidth * scale) / 2; float bitmapY = getHeight() - bitmapHeight * scale; // Set matrix for bitmap mMatrix.reset(); mMatrix.postScale(scale, scale); mMatrix.postTranslate(bitmapX, bitmapY); canvas.drawBitmap(indicatorBitmap, mMatrix, mIndicatorPaint); }
From source file:me.futuretechnology.util.ui.TitlePageIndicator.java
/** * Calculate the bounds for a view's title *//*from w w w . j a v a 2 s . c om*/ private Rect calcBounds(int index, Paint paint) { // calculate the text bounds Rect bounds = new Rect(); CharSequence title = getTitle(index); bounds.right = (int) paint.measureText(title, 0, title.length()); bounds.bottom = (int) paint.getTextSize(); // (paint.descent() - paint.ascent()); return bounds; }
From source file:com.pc.pager.indicator.TitlePageIndicator.java
/** * Calculate the bounds for a view's title * @param index/* w ww. ja v a 2 s . co m*/ * @param paint * @return */ private Rect calcBounds(int index, Paint paint) { // Calculate the text bounds Rect bounds = new Rect(); CharSequence title = getTitle(index); bounds.right = (int) paint.measureText(title, 0, title.length()) + TextMaginLeftAndRight; bounds.bottom = (int) (paint.descent() - paint.ascent()); return bounds; }