要实现Textview中的图文混排需要让图片基于文字居中显示这个就需要自定义ImageSpan 并重写其ondraw的方法来实现,
先看效果那个红点和文字 就居中显示了
OK直接上代码
package com.vc.widget;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.text.style.ImageSpan;
import java.lang.ref.WeakReference;
/**
* imagespan 居中显示
*/
public class ImageSpanCentre extends ImageSpan {
public static final int CENTRE = 2;
public ImageSpanCentre(@NonNull Drawable d, int verticalAlignment) {
super(d, verticalAlignment);
}
public ImageSpanCentre(@NonNull Context context, @NonNull Bitmap b, int verticalAlignment) {
super(context, b, verticalAlignment);
}
@Override
public void draw(@NonNull Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, @NonNull Paint paint) {
Drawable b = getCachedDrawable();
canvas.save();
int transY = 0;
if (mVerticalAlignment == ALIGN_BASELINE) {
transY -= paint.getFontMetricsInt().descent;
}else if(mVerticalAlignment==ALIGN_BOTTOM){
transY= bottom - b.getBounds().bottom;
}else {
Paint.FontMetricsInt fontMetricsInt = paint.getFontMetricsInt();
// y 是基准线 Ascent是基准线 之上字符最高处的距离 descent 是基准线之下字符最低处的距离
transY= (y + fontMetricsInt.descent + y + fontMetricsInt.ascent) / 2 - b.getBounds().bottom/2;
}
canvas.translate(x, transY);
b.draw(canvas);
canvas.restore();
}
private Drawable getCachedDrawable() {
WeakReference<Drawable> wr = mDrawableRef;
Drawable d = null;
if (wr != null)
d = wr.get();
if (d == null) {
d = getDrawable();
mDrawableRef = new WeakReference<Drawable>(d);
}
return d;
}
private WeakReference<Drawable> mDrawableRef;
}
至此自定义的imageSpan 就OK了,只截传入ImageSpanCentre.CENTRE 就OK了,