TextView和Editext里面的ImageSpan居中对齐

做论坛或者空间的时候会有表情之类的,但发现表情嵌入到TextView里面不居中对齐了。
这里写图片描述

有时候的需求是要居中对齐的,特别是表情这样的小图。

刚开始一直看textview的代码,看能不能做下手脚,单找了好久,没发现哪里可以改,最后想想了下,iamgeSpan中应该有做东西,就看了一下imagespan的代码,果然发现了如下方法

@Override
        public int getSize(Paint paint, CharSequence text, int start, int end,
                FontMetricsInt fm)
public void draw(Canvas canvas, CharSequence text, int start, int end,
                float x, int top, int y, int bottom, Paint paint)

是不是可以解决了

@Override
        public int getSize(Paint paint, CharSequence text, int start, int end,
                FontMetricsInt fm) {
            Drawable d = getDrawable();
            Rect rect = d.getBounds();
            if (fm != null) {
                FontMetrics fontMetrics = paint.getFontMetrics();
                int fontHeight = (int) (fontMetrics.bottom - fontMetrics.top);
                int drHeight = rect.bottom - rect.top;
                int top = drHeight / 2 - fontHeight / 4;
                int bottom = drHeight / 2 + fontHeight / 4;
                fm.ascent = -bottom;
                fm.descent = top;
                fm.top = -bottom;
                fm.bottom = top;
            }
            return rect.right;
        }

改变图片的baseline,draw方法就不用做什么了
这里写图片描述

是不是居中了。代码简单,重写下imagespan就可以了
不上代码了

来个全部的代码

private class MyImageSpan extends ImageSpan {

        public MyImageSpan(Context context, int resourceId) {
            super(context, resourceId);
        }

        @Override
        public int getSize(Paint paint, CharSequence text, int start, int end,
                FontMetricsInt fm) {
            Drawable d = getDrawable();
            Rect rect = d.getBounds();
            if (fm != null) {
                FontMetrics fontMetrics = paint.getFontMetrics();
                int fontHeight = (int) (fontMetrics.bottom - fontMetrics.top);
                int drHeight = rect.bottom - rect.top;
                int top = drHeight / 2 - fontHeight / 4;
                int bottom = drHeight / 2 + fontHeight / 4;
                fm.ascent = -bottom;
                fm.descent = top;
                fm.top = -bottom;
                fm.bottom = top;
            }
            return rect.right;
        }

        @Override
        public void draw(Canvas canvas, CharSequence text, int start, int end,
                float x, int top, int y, int bottom, Paint paint) {
            // Drawable drawable = getDrawable();
            // canvas.save();
            // int transY = 0;
            // transY = ((bottom - top) - drawable.getBounds().bottom) / 2;
            // canvas.translate(x, transY);
            // drawable.draw(canvas);
            // canvas.restore();
            super.draw(canvas, text, start, end, x, top, y, bottom, paint);
        }
    }

对于内部类最好写成静态内部类。这是输入性能优化的了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值