package com.swago.room.adapter.bitmap;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.text.style.ImageSpan;

/**
 * @date 2022/1/18 21:13
 * description:
 */
public class CenterAlignImageSpan extends ImageSpan {
    public CenterAlignImageSpan(Drawable drawable, int verticalAlignment) {
        super(drawable, verticalAlignment);
    }

    @Override
    public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
        Drawable b = getDrawable();
        if (paint != null) {
            Paint.FontMetricsInt fm = paint.getFontMetricsInt();
            //计算y方向的位移
            int transY = (y + fm.descent + y + fm.ascent) / 2 - b.getBounds().bottom / 2;
            if (canvas != null) {
                canvas.save();
                //绘制图片位移一段距离
                canvas.translate(x, transY);
                b.draw(canvas);
                canvas.restore();
            }
        }

    }
}