public class IconTextSpan extends ReplacementSpan {
private static final String TAG = "IconTextSpan";
private Context mContext;
private String mText; //Icon内文字
private float mTextSize; //文字大小
private int mWidth;
private float mRightMargin;
private TextPaint textPaint;
private Drawable mDrawable;
private Rect mTextRect;
public IconTextSpan(Context context, String text, Drawable drawable) {
this.mContext = context;
this.mText = text;
this.mDrawable = drawable;
initPaint();
}
public void initPaint() {
}
/**
* 设置字体颜色
* @param textcolor
*/
public void setTextColor(int textcolor){
textPaint.setColor(mContext.getResources().getColor(textcolor));
}
/**
* 设置右边距
*
* @param rightMarginDpValue
*/
public void setRightMarginDpValue(int rightMarginDpValue) {
this.mRightMargin = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, rightMarginDpValue, mContext.getResources().getDisplayMetrics());
}
/**
* 设置字体大小
* @param textSize
*/
public void setTextSize(int textSize){
this.mTextSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, textSize, mContext.getResources().getDisplayMetrics());
textPaint.setTextSize(mTextSize);
}
@Override
public int getSize(@NonNull Paint paint, CharSequence charSequence, int i, int i1, @Nullable Paint.FontMetricsInt fontMetricsInt) {
if (mTextRect == null) {
mTextRect = new Rect();
textPaint = new TextPaint(paint);
textPaint.setColor(mContext.getApplicationContext().getResources().getColor(android.R.color.white));
mTextSize = changeintTopx(8);
textPaint.setTextSize(mTextSize);
textPaint.getTextBounds(mText, 0, mText.length(), mTextRect);
float padding = changeintTopx(8);
Bitmap bm = null;
try {
BitmapDrawable bd = (BitmapDrawable) mDrawable;
bm = bd.getBitmap();
} catch (Exception e) {
e.printStackTrace();
}
if (bm == null) {
mWidth = (int) (mTextRect.width() + padding * 2) + 20;
} else {
mWidth = bm.getWidth();
}
}
return mWidth;
}
/**
* 2倍图 字体左侧24px 字体理图4px parent自带8dp padding
* <p>
* 图片top =
*/
@Override
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
Paint.FontMetrics metrics = paint.getFontMetrics();
//float textHeight = metrics.descent - metrics.ascent;
try {
BitmapDrawable bd = (BitmapDrawable) mDrawable;
int bitmapTop = (int) ((y + metrics.descent + y + metrics.ascent) / 2 - mDrawable.getBounds().bottom / 2);
Bitmap bm = bd.getBitmap();
canvas.drawBitmap(bm, x, bitmapTop, textPaint);
float bitmapCenter = bitmapTop + mDrawable.getIntrinsicHeight() / 2f;
// drawCenterLine(canvas,x,bitmapCenter);
Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
// int ascent = (int) fontMetrics.top;
// int decent = (int) fontMetrics.bottom;
float left = x + changeintTopx(12 + 4);
RectF textRectF = new RectF(left,bitmapCenter - mTextRect.height() / 2f,left + mTextRect.width(),bitmapCenter + mTextRect.height() / 2f);
// Paint rectPaint = new Paint();
// rectPaint.setColor(Color.parseColor("#33ff0000"));
// canvas.drawRect(textRectF, rectPaint);
canvas.drawText(mText, textRectF.left, textRectF.bottom-fontMetrics.descent , textPaint);
} catch (Exception e) {
e.printStackTrace();
}
}
public void drawTextCenter(Canvas canvas, float x, float textCenter) {
Paint paintCenter = new Paint();
paintCenter.setColor(Color.RED);
canvas.drawLine(x, textCenter, x + mDrawable.getIntrinsicWidth(), textCenter, paintCenter);
}
public void drawTextAscent(Canvas canvas, float x, float bitmapCenter, int ascent) {
Paint paintCenter = new Paint();
paintCenter.setColor(Color.RED);
canvas.drawLine(x, bitmapCenter + ascent, x + mDrawable.getIntrinsicWidth(), bitmapCenter + ascent, paintCenter);
}
//span 中心线
public void drawCenterLine(Canvas canvas, float x, float bitmapCenter) {
Paint paintCenter = new Paint();
paintCenter.setColor(Color.BLUE);
canvas.drawLine(x, bitmapCenter, x + mDrawable.getIntrinsicWidth(), bitmapCenter, paintCenter);
}
/**
* 把字体大小sp转成px
*/
public float changeintTopx(int i) {
float pxsize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, i, mContext.getResources().getDisplayMetrics());
return pxsize;
}
}
IconTextSpan,在图片上添加数字生产SpannableString
于 2021-06-21 13:44:27 首次发布