Java Code Examples for android.text.TextPaint#getTextWidths()
The following examples show how to use
android.text.TextPaint#getTextWidths() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SuggestionStripLayoutHelper.java From openboard with GNU General Public License v3.0 | 6 votes |
private static int getTextWidth(@Nullable final CharSequence text, final TextPaint paint) { if (TextUtils.isEmpty(text)) { return 0; } final int length = text.length(); final float[] widths = new float[length]; final int count; final Typeface savedTypeface = paint.getTypeface(); try { paint.setTypeface(getTextTypeface(text)); count = paint.getTextWidths(text, 0, length, widths); } finally { paint.setTypeface(savedTypeface); } int width = 0; for (int i = 0; i < count; i++) { width += Math.round(widths[i] + 0.5f); } return width; }
Example 2
Source File: SuggestionStripLayoutHelper.java From AOSP-Kayboard-7.1.2 with Apache License 2.0 | 6 votes |
private static int getTextWidth(@Nullable final CharSequence text, final TextPaint paint) { if (TextUtils.isEmpty(text)) { return 0; } final int length = text.length(); final float[] widths = new float[length]; final int count; final Typeface savedTypeface = paint.getTypeface(); try { paint.setTypeface(getTextTypeface(text)); count = paint.getTextWidths(text, 0, length, widths); } finally { paint.setTypeface(savedTypeface); } int width = 0; for (int i = 0; i < count; i++) { width += Math.round(widths[i] + 0.5f); } return width; }
Example 3
Source File: SuggestionStripLayoutHelper.java From Indic-Keyboard with Apache License 2.0 | 6 votes |
private static int getTextWidth(@Nullable final CharSequence text, final TextPaint paint) { if (TextUtils.isEmpty(text)) { return 0; } final int length = text.length(); final float[] widths = new float[length]; final int count; final Typeface savedTypeface = paint.getTypeface(); try { paint.setTypeface(getTextTypeface(text)); count = paint.getTextWidths(text, 0, length, widths); } finally { paint.setTypeface(savedTypeface); } int width = 0; for (int i = 0; i < count; i++) { width += Math.round(widths[i] + 0.5f); } return width; }
Example 4
Source File: KeyPreviewView.java From openboard with GNU General Public License v3.0 | 5 votes |
private static float getTextWidth(final String text, final TextPaint paint) { if (TextUtils.isEmpty(text)) { return 0.0f; } final int len = text.length(); final float[] widths = new float[len]; final int count = paint.getTextWidths(text, 0, len, widths); float width = 0; for (int i = 0; i < count; i++) { width += widths[i]; } return width; }
Example 5
Source File: KeyPreviewView.java From LokiBoard-Android-Keylogger with Apache License 2.0 | 5 votes |
private static float getTextWidth(final String text, final TextPaint paint) { if (TextUtils.isEmpty(text)) { return 0.0f; } final int len = text.length(); final float[] widths = new float[len]; final int count = paint.getTextWidths(text, 0, len, widths); float width = 0; for (int i = 0; i < count; i++) { width += widths[i]; } return width; }
Example 6
Source File: Styled.java From PowerFileExplorer with GNU General Public License v3.0 | 5 votes |
/** * 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 get the width of the replacement and the * remaining characters will get 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) { // Jota Text Editor // MetricAffectingSpan[] spans = // text.getSpans(start, end, MetricAffectingSpan.class); // // ReplacementSpan replacement = null; workPaint.set(paint); // // for (int i = 0; i < spans.length; i++) { // MetricAffectingSpan span = spans[i]; // 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; }
Example 7
Source File: KeyPreviewView.java From simple-keyboard with Apache License 2.0 | 5 votes |
private static float getTextWidth(final String text, final TextPaint paint) { if (TextUtils.isEmpty(text)) { return 0.0f; } final int len = text.length(); final float[] widths = new float[len]; final int count = paint.getTextWidths(text, 0, len, widths); float width = 0; for (int i = 0; i < count; i++) { width += widths[i]; } return width; }
Example 8
Source File: KeyPreviewView.java From AOSP-Kayboard-7.1.2 with Apache License 2.0 | 5 votes |
private static float getTextWidth(final String text, final TextPaint paint) { if (TextUtils.isEmpty(text)) { return 0.0f; } final int len = text.length(); final float[] widths = new float[len]; final int count = paint.getTextWidths(text, 0, len, widths); float width = 0; for (int i = 0; i < count; i++) { width += widths[i]; } return width; }
Example 9
Source File: Styled.java From JotaTextEditor with Apache License 2.0 | 5 votes |
/** * 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 get the width of the replacement and the * remaining characters will get 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) { // Jota Text Editor // MetricAffectingSpan[] spans = // text.getSpans(start, end, MetricAffectingSpan.class); // // ReplacementSpan replacement = null; workPaint.set(paint); // // for (int i = 0; i < spans.length; i++) { // MetricAffectingSpan span = spans[i]; // 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; }
Example 10
Source File: KeyPreviewView.java From Indic-Keyboard with Apache License 2.0 | 5 votes |
private static float getTextWidth(final String text, final TextPaint paint) { if (TextUtils.isEmpty(text)) { return 0.0f; } final int len = text.length(); final float[] widths = new float[len]; final int count = paint.getTextWidths(text, 0, len, widths); float width = 0; for (int i = 0; i < count; i++) { width += widths[i]; } return width; }
Example 11
Source File: RecipientEditTextView.java From ChipsLibrary with Apache License 2.0 | 5 votes |
private Bitmap createSelectedChip(final RecipientEntry contact,final TextPaint paint) { // Ellipsize the text so that it takes AT MOST the entire width of the // autocomplete text entry area. Make sure to leave space for padding // on the sides. final int height=(int)mChipHeight; final int deleteWidth=height; final float[] widths=new float[1]; paint.getTextWidths(" ",widths); final String createChipDisplayText=createChipDisplayText(contact); final float calculateAvailableWidth=calculateAvailableWidth(); final CharSequence ellipsizedText=ellipsizeText(createChipDisplayText,paint,calculateAvailableWidth-deleteWidth-widths[0]); // Make sure there is a minimum chip width so the user can ALWAYS // tap a chip without difficulty. final int width=Math.max(deleteWidth*2,(int)Math.floor(paint.measureText(ellipsizedText,0,ellipsizedText.length()))+mChipPadding*2+deleteWidth); // Create the background of the chip. final Bitmap tmpBitmap=Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888); final Canvas canvas=new Canvas(tmpBitmap); if(mChipBackgroundPressed!=null) { mChipBackgroundPressed.setBounds(0,0,width,height); mChipBackgroundPressed.draw(canvas); paint.setColor(sSelectedTextColor); // Vertically center the text in the chip. canvas.drawText(ellipsizedText,0,ellipsizedText.length(),mChipPadding,getTextYOffset((String)ellipsizedText,paint,height),paint); // Make the delete a square. final Rect backgroundPadding=new Rect(); mChipBackgroundPressed.getPadding(backgroundPadding); mChipDelete.setBounds(width-deleteWidth+backgroundPadding.left,0+backgroundPadding.top,width-backgroundPadding.right,height-backgroundPadding.bottom); mChipDelete.draw(canvas); } else Log.w(TAG,"Unable to draw a background for the chips as it was never set"); return tmpBitmap; }
Example 12
Source File: RecipientEditTextView.java From talk-android with MIT License | 4 votes |
private Bitmap createChipBitmap(RecipientEntry contact, TextPaint paint, Bitmap icon, Drawable background) { if (background == null) { Log.w(TAG, "Unable to draw a background for the chips as it was never set"); return Bitmap.createBitmap( (int) mChipHeight * 2, (int) mChipHeight, Bitmap.Config.ARGB_8888); } Rect backgroundPadding = new Rect(); background.getPadding(backgroundPadding); // Ellipsize the text so that it takes AT MOST the entire width of the // autocomplete text entry area. Make sure to leave space for padding // on the sides. int height = (int) mChipHeight + getResources().getDimensionPixelSize(R.dimen.extra_chip_height); // Since the icon is a square, it's width is equal to the maximum height it can be inside // the chip. int iconWidth = height - backgroundPadding.top - backgroundPadding.bottom; float[] widths = new float[1]; paint.getTextWidths(" ", widths); CharSequence ellipsizedText = ellipsizeText(createChipDisplayText(contact), paint, calculateAvailableWidth() - iconWidth - widths[0] - backgroundPadding.left - backgroundPadding.right); int textWidth = (int) paint.measureText(ellipsizedText, 0, ellipsizedText.length()); // Make sure there is a minimum chip width so the user can ALWAYS // tap a chip without difficulty. int width = Math.max(iconWidth * 2, textWidth + (mChipPadding * 2) + iconWidth + backgroundPadding.left + backgroundPadding.right); // Create the background of the chip. Bitmap tmpBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(tmpBitmap); // Draw the background drawable background.setBounds(height / 2, 0, width, height); background.draw(canvas); // Draw the text vertically aligned int textX = shouldPositionAvatarOnRight() ? mChipPadding + backgroundPadding.left : width - backgroundPadding.right - mChipPadding - textWidth; paint.setColor(0xFF5C5C5C); paint.setAntiAlias(true); canvas.drawText(ellipsizedText, 0, ellipsizedText.length(), textX, getTextYOffset(ellipsizedText.toString(), paint, height), paint); if (icon != null) { // Draw the icon icon = ChipsUtil.getClip(icon); int iconX = shouldPositionAvatarOnRight() ? width - backgroundPadding.right - iconWidth : backgroundPadding.left; RectF src = new RectF(0, 0, icon.getWidth(), icon.getHeight()); RectF dst = new RectF(0, 0, height, height); drawIconOnCanvas(icon, canvas, paint, src, dst); } return tmpBitmap; }
Example 13
Source File: TextChipsEditView.java From talk-android with MIT License | 4 votes |
private Bitmap createChipBitmap(RecipientEntry contact, TextPaint paint, Bitmap icon, Drawable background) { if (background == null) { Log.w(TAG, "Unable to draw a background for the chips as it was never set"); return Bitmap.createBitmap( (int) mChipHeight * 2, (int) mChipHeight, Bitmap.Config.ARGB_8888); } Rect backgroundPadding = new Rect(); background.getPadding(backgroundPadding); // Ellipsize the text so that it takes AT MOST the entire width of the // autocomplete text entry area. Make sure to leave space for padding // on the sides. int height = (int) mChipHeight; // Since the icon is a square, it's width is equal to the maximum height it can be inside // the chip. int iconWidth = height - backgroundPadding.top - backgroundPadding.bottom; float[] widths = new float[1]; paint.getTextWidths(" ", widths); CharSequence ellipsizedText = ellipsizeText(createChipDisplayText(contact), paint, calculateAvailableWidth() - iconWidth - widths[0] - backgroundPadding.left - backgroundPadding.right); int textWidth = (int) paint.measureText(ellipsizedText, 0, ellipsizedText.length()); // Make sure there is a minimum chip width so the user can ALWAYS // tap a chip without difficulty. // int width = Math.max(iconWidth * 2, textWidth + (mChipPadding * 2) + iconWidth // + backgroundPadding.left + backgroundPadding.right); int width = mIconPadding + icon.getWidth() + textWidth + mFontPadding * 2; // Create the background of the chip. Bitmap tmpBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(tmpBitmap); // Draw the background drawable background.setBounds(0, 0, width, height); background.draw(canvas); // Draw the text vertically aligned paint.setColor(0xFF5C5C5C); paint.setAntiAlias(true); Paint.FontMetrics fontMetrics = paint.getFontMetrics(); float textY = height / 2 - fontMetrics.descent + (fontMetrics.descent - fontMetrics.ascent) / 2; getTextYOffset(ellipsizedText.toString(), paint, height); canvas.drawText(ellipsizedText, 0, ellipsizedText.length(), mIconPadding + icon.getWidth() + mFontPadding, textY, paint); if (icon != null) { // Draw the icon canvas.drawBitmap(icon, mIconPadding, mChipHeight / 2 - icon.getHeight() / 2, paint); } return tmpBitmap; }
Example 14
Source File: RecipientEditTextView.java From ChipsLibrary with Apache License 2.0 | 4 votes |
private Bitmap createUnselectedChip(final RecipientEntry contact,final TextPaint paint,final boolean leaveBlankIconSpacer) { // Ellipsize the text so that it takes AT MOST the entire width of the // autocomplete text entry area. Make sure to leave space for padding // on the sides. final int height=(int)mChipHeight; int iconWidth=height; final float[] widths=new float[1]; paint.getTextWidths(" ",widths); final float availableWidth=calculateAvailableWidth(); final String chipDisplayText=createChipDisplayText(contact); final CharSequence ellipsizedText=ellipsizeText(chipDisplayText,paint,availableWidth-iconWidth-widths[0]); // Make sure there is a minimum chip width so the user can ALWAYS // tap a chip without difficulty. final int width=Math.max(iconWidth*2,(int)Math.floor(paint.measureText(ellipsizedText,0,ellipsizedText.length()))+mChipPadding*2+iconWidth); // Create the background of the chip. final Bitmap tmpBitmap=Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888); final Canvas canvas=new Canvas(tmpBitmap); final Drawable background=getChipBackground(contact); if(background!=null) { background.setBounds(0,0,width,height); background.draw(canvas); // Don't draw photos for recipients that have been typed in OR generated on the fly. final long contactId=contact.getContactId(); final boolean drawPhotos=isPhoneQuery() ? contactId!=RecipientEntry.INVALID_CONTACT : contactId!=RecipientEntry.INVALID_CONTACT&&contactId!=RecipientEntry.GENERATED_CONTACT&&!TextUtils.isEmpty(contact.getDisplayName()); if(drawPhotos) { byte[] photoBytes=contact.getPhotoBytes(); // There may not be a photo yet if anything but the first contact address // was selected. if(photoBytes==null&&contact.getPhotoThumbnailUri()!=null) { // TODO: cache this in the recipient entry? getAdapter().fetchPhoto(contact,contact.getPhotoThumbnailUri()); photoBytes=contact.getPhotoBytes(); } Bitmap photo; if(photoBytes!=null) photo=BitmapFactory.decodeByteArray(photoBytes,0,photoBytes.length); else // TODO: can the scaled down default photo be cached? photo=mDefaultContactPhoto; // Draw the photo on the left side. if(photo!=null) { final RectF src=new RectF(0,0,photo.getWidth(),photo.getHeight()); final Rect backgroundPadding=new Rect(); mChipBackground.getPadding(backgroundPadding); final RectF dst=new RectF(width-iconWidth+backgroundPadding.left,0+backgroundPadding.top,width-backgroundPadding.right,height-backgroundPadding.bottom); final Matrix matrix=new Matrix(); matrix.setRectToRect(src,dst,Matrix.ScaleToFit.FILL); canvas.drawBitmap(photo,matrix,paint); } } else if(!leaveBlankIconSpacer||isPhoneQuery()) iconWidth=0; paint.setColor(getContext().getResources().getColor(android.R.color.black)); // Vertically center the text in the chip. canvas.drawText(ellipsizedText,0,ellipsizedText.length(),mChipPadding,getTextYOffset((String)ellipsizedText,paint,height),paint); } else Log.w(TAG,"Unable to draw a background for the chips as it was never set"); return tmpBitmap; }