List of usage examples for android.text TextPaint getTextWidths
public int getTextWidths(String text, float[] widths)
From source file:com.android.ex.chips.RecipientEditTextView.java
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);/*ww w. j av a 2 s . c o m*/ // 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; }
From source file:com.android.ex.chips.RecipientEditTextView.java
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);//w w w . j av a2 s . com // 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(ContextCompat.getColor(getContext(), 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; }