BubbleTextViewpublic class BubbleTextView extends android.widget.TextView TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
because we want to make the bubble taller than the text and TextView's clip is
too aggressive. |
Fields Summary |
---|
private static final float | CORNER_RADIUS | private static final float | PADDING_H | private static final float | PADDING_V | private final android.graphics.RectF | mRect | private android.graphics.Paint | mPaint | private boolean | mBackgroundSizeChanged | private android.graphics.drawable.Drawable | mBackground | private float | mCornerRadius | private float | mPaddingH | private float | mPaddingV |
Methods Summary |
---|
public void | draw(android.graphics.Canvas canvas)
final Drawable background = mBackground;
if (background != null) {
final int scrollX = mScrollX;
final int scrollY = mScrollY;
if (mBackgroundSizeChanged) {
background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
mBackgroundSizeChanged = false;
}
if ((scrollX | scrollY) == 0) {
background.draw(canvas);
} else {
canvas.translate(scrollX, scrollY);
background.draw(canvas);
canvas.translate(-scrollX, -scrollY);
}
}
final Layout layout = getLayout();
final RectF rect = mRect;
final int left = getCompoundPaddingLeft();
final int top = getExtendedPaddingTop();
rect.set(left + layout.getLineLeft(0) - mPaddingH,
top + layout.getLineTop(0) - mPaddingV,
Math.min(left + layout.getLineRight(0) + mPaddingH, mScrollX + mRight - mLeft),
top + layout.getLineBottom(0) + mPaddingV);
canvas.drawRoundRect(rect, mCornerRadius, mCornerRadius, mPaint);
super.draw(canvas);
| protected void | drawableStateChanged()
Drawable d = mBackground;
if (d != null && d.isStateful()) {
d.setState(getDrawableState());
}
super.drawableStateChanged();
| private void | init()
setFocusable(true);
mBackground = getBackground();
setBackgroundDrawable(null);
mBackground.setCallback(this);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setColor(getContext().getResources().getColor(R.color.bubble_dark_background));
final float scale = getContext().getResources().getDisplayMetrics().density;
mCornerRadius = CORNER_RADIUS * scale;
mPaddingH = PADDING_H * scale;
//noinspection PointlessArithmeticExpression
mPaddingV = PADDING_V * scale;
| protected boolean | setFrame(int left, int top, int right, int bottom)
if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
mBackgroundSizeChanged = true;
}
return super.setFrame(left, top, right, bottom);
| protected boolean | verifyDrawable(android.graphics.drawable.Drawable who)
return who == mBackground || super.verifyDrawable(who);
|
|