Align text around ImageSpan center vertical

前端 未结 9 1830
抹茶落季
抹茶落季 2020-11-28 23:59

I have an ImageSpan inside of a piece of text. What I\'ve noticed is that the surrounding text is always drawn at the bottom of the text line -- to be more precise, the size

9条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-29 00:08

    ImageSpan imageSpan = new ImageSpan(d, ImageSpan.ALIGN_BOTTOM) {
                    public void draw(Canvas canvas, CharSequence text, int start,
                            int end, float x, int top, int y, int bottom,
                            Paint paint) {
                        Drawable b = getDrawable();
                        canvas.save();
    
                        int transY = bottom - b.getBounds().bottom;
                        // this is the key 
                        transY -= paint.getFontMetricsInt().descent / 2;
    
                        canvas.translate(x, transY);
                        b.draw(canvas);
                        canvas.restore();
                    }
                };
    

提交回复
热议问题