@Override protected void animatePrepare(CharSequence text) { differentList.clear(); differentList.addAll(CharacterUtils.diff(mOldText, mText)); Rect bounds = new Rect(); mPaint.getTextBounds(mText.toString(), 0, mText.length(), bounds); mTextHeight = bounds.height(); }
@Override protected void animatePrepare(CharSequence text) { differentList.clear(); differentList.addAll(CharacterUtils.diff(mOldText, mText)); Rect bounds = new Rect(); mPaint.getTextBounds(mText.toString(), 0, mText.length(), bounds); mTextHeight = bounds.height(); }
private void drawSection(Canvas c, int left, int right, View child, RecyclerView.LayoutParams params, int position) { c.drawRect(left, child.getTop() - params.topMargin - mSectionHeight, right, child.getTop() - params.topMargin, mBgPaint); mTextPaint.getTextBounds(mData.get(position).getSection(), 0, mData.get(position).getSection().length(), mBounds); c.drawText(mData.get(position).getSection(), child.getPaddingLeft(), child.getTop() - params.topMargin - (mSectionHeight / 2 - mBounds.height() / 2), mTextPaint); }
@Override public int getContentLeft() { Rect bound = new Rect(); String longestString = ""; if (getText().toString().contains("\n")) { String[] brokenStrings = getText().toString().split("\\n"); for (String each : brokenStrings) { if (each.length() > longestString.length()) longestString = each; } } else { longestString = getText().toString(); } getPaint().getTextBounds(longestString, 0, longestString.length(), bound); int contentWidth = bound.width(); return getLeft() + getWidth() / 2 - contentWidth / 2; }
@Override public int getContentRight() { Rect bound = new Rect(); String longestString = ""; if (getText().toString().contains("\n")) { String[] brokenStrings = getText().toString().split("\\n"); for (String each : brokenStrings) { if (each.length() > longestString.length()) longestString = each; } } else { longestString = getText().toString(); } getPaint().getTextBounds(longestString, 0, longestString.length(), bound); int contentWidth = bound.width(); return getLeft() + getWidth() / 2 + contentWidth / 2; }
@Override public void draw(Canvas canvas) { // Center drawable within available bounds int boundsWidth = getBounds().width(); int boundsHeight = getBounds().height(); int dimen = Math.min(boundsWidth, boundsHeight); paint.setTextSize(dimen); String textValue = MaterialIconUtils.getIconString(icon.ordinal()); paint.getTextBounds(textValue, 0, 1, mCachedRect); float textBottom = getBounds().top + (boundsHeight - mCachedRect.height()) / 2f + mCachedRect.height() - mCachedRect.bottom; canvas.drawText(textValue, getBounds().left + boundsWidth / 2f, textBottom, paint); }
@Override public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) { int pos = ((LinearLayoutManager) (parent.getLayoutManager())).findFirstVisibleItemPosition(); if (pos < 0) return; if (mData == null || mData.isEmpty()) return; String section = mData.get(pos).getSection(); View child = parent.findViewHolderForLayoutPosition(pos).itemView; boolean flag = false; if ((pos + 1) < mData.size()) { if (null != section && !section.equals(mData.get(pos + 1).getSection())) { if (child.getHeight() + child.getTop() < mSectionHeight) { c.save(); flag = true; c.translate(0, child.getHeight() + child.getTop() - mSectionHeight); } } } c.drawRect(parent.getPaddingLeft(), parent.getPaddingTop(), parent.getRight() - parent.getPaddingRight(), parent.getPaddingTop() + mSectionHeight, mBgPaint); mTextPaint.getTextBounds(section, 0, section.length(), mBounds); c.drawText(section, child.getPaddingLeft(), parent.getPaddingTop() + mSectionHeight - (mSectionHeight / 2 - mBounds.height() / 2), mTextPaint); if (flag) c.restore(); }
GifBadge(Context context) { if (bitmap == null) { final DisplayMetrics dm = context.getResources().getDisplayMetrics(); final float density = dm.density; final float scaledDensity = dm.scaledDensity; final TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint .SUBPIXEL_TEXT_FLAG); textPaint.setTypeface(Typeface.create(TYPEFACE, TYPEFACE_STYLE)); textPaint.setTextSize(TEXT_SIZE * scaledDensity); final float padding = PADDING * density; final float cornerRadius = CORNER_RADIUS * density; final Rect textBounds = new Rect(); textPaint.getTextBounds(GIF, 0, GIF.length(), textBounds); height = (int) (padding + textBounds.height() + padding); width = (int) (padding + textBounds.width() + padding); bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmap.setHasAlpha(true); final Canvas canvas = new Canvas(bitmap); final Paint backgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG); backgroundPaint.setColor(BACKGROUND_COLOR); if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP){ canvas.drawRoundRect(0, 0, width, height, cornerRadius, cornerRadius, backgroundPaint); } // punch out the word 'GIF', leaving transparency textPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); canvas.drawText(GIF, padding, height - padding, textPaint); } paint = new Paint(); }
newLayout .getPaint() .getTextBounds(customEllipsisText.toString(), 0, customEllipsisText.length(), bounds);
if (!TextUtils.isEmpty(text)) { TextPaint textPaint = getPaint(); textPaint.getTextBounds(text.toString(), 0, text.length(), textBounds); } else { textBounds.setEmpty();
/** * @param displayName The name used to create the letter for the tile * @return A {@link Bitmap} that contains a letter used in the English * alphabet or digit, if there is no letter or digit available, a * default image is shown instead */ public Bitmap getLetterTile(String displayName) { // workaround if(displayName == null || displayName.length() == 0) return null; final Bitmap bitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888); final char firstChar = displayName.charAt(0); final Canvas c = mCanvas; c.setBitmap(bitmap); c.drawColor(pickColor(displayName)); if (isLetterOrDigit(firstChar)) { mFirstChar[0] = Character.toUpperCase(firstChar); mPaint.setTextSize(mTileLetterFontSize); mPaint.getTextBounds(mFirstChar, 0, 1, mBounds); c.drawText(mFirstChar, 0, 1, mWidth / 2, mHeight / 2 + (mBounds.bottom - mBounds.top) / 2, mPaint); } else { // (32 - 24) / 2 = 4 c.drawBitmap(mDefaultBitmap, ViewUtil.dpToPx(4), ViewUtil.dpToPx(4), null); } return bitmap; }
/** * @param displayName The name used to create the letter for the tile * @return A circular {@link Bitmap} that contains a letter used in the English * alphabet or digit, if there is no letter or digit available, a * default image is shown instead */ public Bitmap getCircularLetterTile(String displayName) { // workaround if(displayName == null || displayName.length() == 0) displayName = "."; final Bitmap bitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888); final char firstChar = displayName.charAt(0); final Canvas c = mCanvas; c.setBitmap(bitmap); c.drawColor(pickColor(displayName)); if (isLetterOrDigit(firstChar)) { mFirstChar[0] = Character.toUpperCase(firstChar); mPaint.setTextSize(mTileLetterFontSize); mPaint.getTextBounds(mFirstChar, 0, 1, mBounds); c.drawText(mFirstChar, 0, 1, mWidth / 2, mHeight / 2 + (mBounds.bottom - mBounds.top) / 2, mPaint); } else { // (32 - 24) / 2 = 4 c.drawBitmap(mDefaultBitmap, ViewUtil.dpToPx(4), ViewUtil.dpToPx(4), null); } return getCircularBitmap(bitmap); }
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (id != 0) { android.widget.TextView textView = ((ViewGroup) getParent()).findViewById(id); if (text == null) text = textView.getText(); paint = textView.getPaint(); if (layout == null) layout = new StaticLayout(text, paint, getMeasuredWidth(), Layout.Alignment.ALIGN_NORMAL, 1, 0, true); String firstLine = text.subSequence(0, layout.getLineEnd(0)).toString(); paint.getTextBounds(firstLine, 0, firstLine.length(), rect); baseline = Math.abs(rect.top); rect.top = -layout.getLineAscent(0) + rect.top; String lastLine = text.subSequence(layout.getLineStart(layout.getLineCount() - 1), layout.getLineEnd(layout.getLineCount() - 1)).toString(); paint.getTextBounds(lastLine, 0, lastLine.length(), rect2); rect.bottom = layout.getHeight() - layout.getLineDescent(layout.getLineCount() - 1) + rect2.bottom; setMeasuredDimension(getMeasuredWidth(), rect.height() + getPaddingTop() + getPaddingBottom()); } }
mFirstChar[0] = Character.toUpperCase(firstChar); mPaint.setTextSize(mTileLetterFontSize); mPaint.getTextBounds(mFirstChar, 0, 1, mBounds); c.drawText(mFirstChar, 0, 1, 0 + width / 2, 0 + height / 2 + (mBounds.bottom - mBounds.top) / 2, mPaint);
Rect rect = new Rect(); String strTxt = getText().toString(); tp.getTextBounds(strTxt, 0, strTxt.length(), rect); int scrollingLen = rect.width() + getWidth(); rect = null;
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { mTextPaint.getTextBounds(mText, 0, mText.length(), text_bounds); setMeasuredDimension( measureWidth(widthMeasureSpec),
TextPaint textPaint = getPaint(); Rect size = new Rect(); textPaint.getTextBounds(mLabel, 0, mLabel.length(), size); setPadding(mPaddingLeft + size.width(), getPaddingTop(), getPaddingRight(), getPaddingBottom()); super.onDraw(canvas);
/** * Lays out textview to (text dimensions + padding) * system font scale * Padding based on screen density * * @param tv The TextView to layout */ private void layoutTextView(TextView tv) { final float fontScale = getResources().getConfiguration().fontScale; /* * Multiplier for padding based on phone density * Density of 2 does not need padding */ final float densityMultiplier = getContext().getResources().getDisplayMetrics().density - 2; Rect bounds = new Rect(); paintText.getTextBounds(tv.getText().toString().toCharArray(), 0, tv.getText().length(), bounds); tv.layout(0, 0, (int) ((bounds.width() + (33 * densityMultiplier)) * fontScale), (int) ((bounds.height() + (22 * densityMultiplier)) * fontScale)); } }
@Override public int getContentRight() { Rect bound = new Rect(); getPaint().getTextBounds(getText().toString(), 0, getText().length(), bound); int contentWidth = bound.width(); return getLeft() + getWidth() / 2 + contentWidth / 2; }
@Override public int getContentLeft() { Rect bound = new Rect(); getPaint().getTextBounds(getText().toString(), 0, getText().length(), bound); int contentWidth = bound.width(); return getLeft() + getWidth() / 2 - contentWidth / 2; }