importandroid.graphics.Bitmap;importandroid.graphics.Canvas;importandroid.graphics.Paint;importandroid.graphics.drawable.Drawable;importandroid.support.annotation.NonNull;importandroid.text.style.ImageSpan;/** * Created by harvie on 2017/7/12 0012. */publicclassCenterAlignImageSpanextendsImageSpan{publicCenterAlignImageSpan(Drawabledrawable){super(drawable);}publicCenterAlignImageSpan(Bitmapb){super(b);}@Overridepublicvoiddraw(@NonNullCanvascanvas,CharSequencetext,intstart,intend,floatx,inttop,inty,intbottom,@NonNullPaintpaint){Drawableb=getDrawable();Paint.FontMetricsIntfm=paint.getFontMetricsInt();inttransY=(y+fm.descent+y+fm.ascent)/2-b.getBounds().bottom/2;//计算y方向的位移canvas.save();canvas.translate(x,transY);//绘制图片位移一段距离b.draw(canvas);canvas.restore();}}