图文混排(图片居中+文字变色)

package com.hoolai.erciyuan.ui.widget;


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

import java.lang.ref.WeakReference;

/**
 * Name: CenterImageSpan.java
 * Author: lilei
 * Date: 2018/5/6
 * Comment: //TODO 图文混排图片居中显示
 */
public class CenterImageSpan extends ImageSpan {

    private WeakReference<Drawable> mDrawableRef;

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

    @Override
    public int getSize(Paint paint, CharSequence text, int start, int end,
                       Paint.FontMetricsInt fontMetricsInt) {
        Drawable drawable = getDrawable();
        Rect rect = drawable.getBounds();
        if (fontMetricsInt != null) {
            Paint.FontMetricsInt fmPaint = paint.getFontMetricsInt();
            int fontHeight = fmPaint.descent - fmPaint.ascent;
            int drHeight = rect.bottom - rect.top;
            int centerY = fmPaint.ascent + fontHeight / 2;

            fontMetricsInt.ascent = centerY - drHeight / 2;
            fontMetricsInt.top = fontMetricsInt.ascent;
            fontMetricsInt.bottom = centerY + drHeight / 2;
            fontMetricsInt.descent = fontMetricsInt.bottom;
        }
        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 = getCachedDrawable();
        canvas.save();
        Paint.FontMetricsInt fmPaint = paint.getFontMetricsInt();
        int fontHeight = fmPaint.descent - fmPaint.ascent;
        int centerY = y + fmPaint.descent - fontHeight / 2;
        int transY = centerY - (drawable.getBounds().bottom - drawable.getBounds().top) / 2;
        canvas.translate(x, transY);
        drawable.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<>(d);
        }

        return d;
    }
}

集成ImageSpan类

具体使用

TextView txt_explain = view.findViewById(R.id.txt_personal_dialog_explain);
String explain = txt_explain.getText().toString();
int start = explain.length();
String explain_pink = "变色字体";
int end = explain.length() + explain_pink.length();
SpannableString spannableString = new SpannableString(explain + explain_pink);
//设置颜色
spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#ff427b")), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

//设置问号
ImageSpan image = new CenterImageSpan(mContext, R.mipmap.ic_question, DynamicDrawableSpan.ALIGN_BASELINE);

spannableString.setSpan(image, end - 1, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
txt_explain.setText(spannableString);


猜你喜欢

转载自blog.csdn.net/qq_25409587/article/details/80215613