canvas教程

图文垂直居中 TextView+SpannableString(2)

字号+ 作者:H5之家 来源:H5之家 2017-11-21 09:10 我要评论( )

这个是我最终的图文居中span,如下: public class CustomSpan extends ImageSpan {//图片适应文本行高度int resourceId;int textColor;float textRadio;int marginH;//左右间隔Rect rect;Drawable drawable;publicC

这个是我最终的图文居中span,如下:

public class CustomSpan extends ImageSpan { //图片适应文本行高度 int resourceId; int textColor; float textRadio; int marginH;//左右间隔 Rect rect; Drawable drawable; publicCustomSpan(Context context, int resourceId, int textColor, float textRadio, int marginH) { super(context,resourceId); this.resourceId= resourceId; drawable= context.getResources().getDrawable(resourceId); this.textRadio= textRadio; this.textColor= textColor; this.marginH= marginH; } @Override public int getSize(Paint paint,CharSequence text, intstart, intend,Paint.FontMetricsInt fm) { //设置图片块的宽度 CharSequence targetText=text.subSequence(start,end); Paint.FontMetricsInt fmPaint = paint.getFontMetricsInt(); int txtW = (int) Math.ceil(paint.measureText(targetText.toString())); int fontHeight = fmPaint.descent- fmPaint.ascent; rect=newRect(0,0,txtW,fontHeight); return rect.right+2*marginH; } /** * see detail message in android.text.TextLine * *@paramcanvasthe canvas, can be null if not rendering *@paramtextthe text to be draw *@paramstartthe text start position *@paramendthe text end position *@paramxthe edge of the replacement closest to the leading margin *@paramtopthe top of the line //文本所在改行的顶部 *@paramythe baseline //文本的基准线 *@parambottomthe bottom of the line //文本所在改行的底部 及下行的顶部,xml文件中的设置的行间距 会直接影响 bottom到baseline的距离 *@parampaintthe work paint */ @Override public void draw(Canvas canvas,CharSequence text, int start, int end, float x, int top, int y, int bottom,Paint paint) { CharSequence targetText=text.subSequence(start,end); intoldTextColor =paint.getColor(); floatoldTextSize =paint.getTextSize(); // canvas.drawLine(0,top,400,top,paint); // canvas.drawLine(0,y,400,y,paint); // canvas.drawLine(0,bottom,400,bottom,paint); Paint.FontMetricsInt fmPaint = paint.getFontMetricsInt(); int oldfontascent = fmPaint.ascent; paint.setColor(textColor); paint.setTextSize(oldTextSize*textRadio); fmPaint = paint.getFontMetricsInt(); int txtW = (int) Math.ceil(paint.measureText(targetText.toString())); int fontHeight = fmPaint.descent- fmPaint.ascent; canvas.save(); canvas.translate(marginH+x,y+oldfontascent);//移动到该块的原点 drawable.setBounds(rect);//背景的绘制 drawable.draw(canvas); Log.e("尺寸",rect.bottom-((rect.bottom-fontHeight))+","+(0-oldfontascent*textRadio)); canvas.translate((rect.right-txtW)/2.0f,rect.bottom/2.f+fontHeight/2-fmPaint.descent); //移动的值是相对的。移动到“块”中字体“服务中”baseline canvas.drawText(targetText.toString(),0,0,paint); paint.setColor(oldTextColor); paint.setTextSize(oldTextSize); canvas.restore(); } }

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • CanvasPaint在线版画图软件

    CanvasPaint在线版画图软件

    2017-11-20 15:01

  • 从零开始实现书籍翻页效果(四)

    从零开始实现书籍翻页效果(四)

    2017-11-09 15:08

  • Tkinter:图形用户界面编程

    Tkinter:图形用户界面编程

    2017-10-07 18:06

  • c uint16 byte

    c uint16 byte

    2017-09-14 15:02

网友点评
e