//图文混排 String text = "商家折扣(-10%)" + " "; SpannableStringBuilder spannable = new SpannableStringBuilder(text); CenterImageSpan imageSpan = new CenterImageSpan(MyApplication.getInstance().getApplicationContext(), R.mipmap.ic_qa_14); spannable.setSpan(imageSpan,text.length() -1, text.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE); holder.mDataBinding.orderGoodsName.setText(spannable);
private class CenterImageSpan extends ImageSpan {
private CenterImageSpan(Context arg0, int arg1) {
super(arg0, arg1);
}
public int getSize(@NotNull Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
Drawable d = getDrawable();
Rect rect = d.getBounds();
if (fm != null) {
Paint.FontMetricsInt fmPaint=paint.getFontMetricsInt();
int fontHeight = fmPaint.bottom - fmPaint.top;
int drHeight=rect.bottom-rect.top;
int top= drHeight/2 - fontHeight/4;
int bottom=drHeight/2 + fontHeight/4;
fm.ascent=-bottom;
fm.top=-bottom;
fm.bottom=top;
fm.descent=top;
}
return rect.right;
}
@Override
public void draw(@NotNull Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, @NotNull Paint paint) {
Drawable b = getDrawable();
canvas.save();
int transY = 0;
transY = ((bottom-top) - b.getBounds().bottom)/2+top;
canvas.translate(x, transY);
b.draw(canvas);
canvas.restore();
}
}
最后
以上就是稳重皮皮虾最近收集整理的关于Android图文混排,以及居中处理的全部内容,更多相关Android图文混排内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复