public ContactChipSpan( CharSequence name, int height, int maxWidth, int paddingLeft, int paddingRight, Typeface typeface, int textColor, int textSize, int backgroundColor) { mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mPaint.setStyle(Paint.Style.FILL); mPaint.setColor(textColor); mPaint.setTypeface(typeface); mPaint.setTextSize(textSize); mTextPaint = new TextPaint(mPaint); mRect = new RectF(); mMatrix = new Matrix(); mContactName = name; mPaddingLeft = paddingLeft; mPaddingRight = paddingRight; mBackgroundColor = backgroundColor; mHeight = height; mWidth = Math.round( Math.min( maxWidth, mPaint.measureText(name, 0, name.length()) + paddingLeft + paddingRight + height)); int outerWidth = Math.max(0, mWidth - mPaddingLeft - mPaddingRight - mHeight); Paint.FontMetricsInt temp = mTextPaint.getFontMetricsInt(); BoringLayout.Metrics mMetrics = new BoringLayout.Metrics(); mMetrics.width = (int) Math.ceil(mTextPaint.measureText(mContactName, 0, mContactName.length())); mMetrics.ascent = temp.ascent; mMetrics.bottom = temp.bottom; mMetrics.descent = temp.descent; mMetrics.top = temp.top; mMetrics.leading = temp.leading; mBoringLayout = BoringLayout.make( mContactName, mTextPaint, outerWidth, Layout.Alignment.ALIGN_NORMAL, 1f, 1f, mMetrics, true, TextUtils.TruncateAt.END, outerWidth); }
@Override public void draw( Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) { canvas.save(); canvas.translate(x, top); float halfHeight = mHeight / 2f; mPaint.setShader(null); mPaint.setColor(mBackgroundColor); mRect.set(1, 0, mHeight + 1, mHeight); canvas.drawArc(mRect, 90, 180, true, mPaint); mRect.set(mWidth - mHeight, 0, mWidth, mHeight); canvas.drawArc(mRect, 270, 180, true, mPaint); mRect.set(halfHeight, 0, mWidth - halfHeight, mHeight); canvas.drawRect(mRect, mPaint); if (mBitmap != null) { mPaint.setShader(mBitmapShader); canvas.drawCircle(halfHeight, halfHeight, halfHeight, mPaint); } if (mContactName != null && mBoringLayout != null) { canvas.translate(mHeight + mPaddingLeft, (mHeight - mBoringLayout.getHeight()) / 2f); mBoringLayout.draw(canvas); } canvas.restore(); }