Android图文混排

首先上一张图,看看显示效果

Android图文混排_第1张图片

代码实现

自定义ImageSpan

package com.zhoujian.imagespan;

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;

/**
 * Created by zhoujian on 2017/7/2.
 */

public class CustomImageSpan extends ImageSpan {

    //自定义对齐方式--与文字中间线对齐
    private int ALIGN_FONTCENTER = 2;

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

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

    @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(); //调用imageSpan中的方法获取drawable对象
        canvas.save();

        //获取画笔的文字绘制时的具体测量数据
        Paint.FontMetricsInt fm = paint.getFontMetricsInt();

        //系统原有方法,默认是Bottom模式)
        int transY = bottom - drawable.getBounds().bottom;
        if (mVerticalAlignment == ALIGN_BASELINE) {
            transY -= fm.descent;
        } else if (mVerticalAlignment == ALIGN_FONTCENTER) {
            //此处加入判断, 如果是自定义的居中对齐
            //与文字的中间线对齐(这种方式不论是否设置行间距都能保障文字的中间线和图片的中间线是对齐的)
            // y+ascent得到文字内容的顶部坐标,y+descent得到文字的底部坐标,(顶部坐标+底部坐标)/2=文字内容中间线坐标
            transY = ((y + fm.descent) + (y + fm.ascent)) / 2 - drawable.getBounds().bottom / 2;
        }

        canvas.translate(x, transY);
        drawable.draw(canvas);
        canvas.restore();
    }

    /**
     * 重写getSize方法,只有重写该方法后,才能保证不论是图片大于文字还是文字大于图片,都能实现中间对齐
     */
    public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
        Drawable d = getDrawable();
        Rect rect = d.getBounds();
        if (fm != null) {
            Paint.FontMetricsInt fmPaint = paint.getFontMetricsInt();
            int fontHeight = fmPaint.bottom - fmPaint.top;
            int drHeight = rect.bottom - rect.top;

            int top = drHeight / 2 - fontHeight / 4;
            int bottom = drHeight / 2 + fontHeight / 4;

            fm.ascent = -bottom;
            fm.top = -bottom;
            fm.bottom = top;
            fm.descent = top;
        }
        return rect.right;
    }
}

MainActivity.java

package com.zhoujian.imagespan;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.SpannableString;
import android.text.Spanned;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {


    private TextView tv_test1;
    private TextView tv_test2;
    private ImageView imageview1;
    private ImageView imageview2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();

        clickEvent();
    }

    private void clickEvent()
    {
        imageview1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "喜欢~开心!", Toast.LENGTH_SHORT).show();
            }
        });

        imageview2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "不喜欢~难过!", Toast.LENGTH_SHORT).show();
            }
        });

    }

    private void initView() {


        tv_test1 = (TextView) findViewById(R.id.tv_test1);
        tv_test2 = (TextView) findViewById(R.id.tv_test2);
        imageview1 = (ImageView) findViewById(R.id.imageview1);
        imageview2 = (ImageView) findViewById(R.id.imageview2);


        SpannableString spannableString1 = new SpannableString("点击 按钮表示喜欢!");
        //调用自定义的imageSpan,实现文字与图片的横向居中对齐
        CustomImageSpan imageSpan1 = new CustomImageSpan(this, R.mipmap.smalllike, 2);
        //setSpan插入内容的时候,起始位置不替换,会替换起始位置到终止位置间的内容,含终止位置。
        //Spanned.SPAN_EXCLUSIVE_EXCLUSIVE模式用来控制是否同步设置新插入的内容与start/end 位置的字体样式,此处没设置具体字体,所以可以随意设置
        spannableString1.setSpan(imageSpan1, 2, 3, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        tv_test1.setText(spannableString1);


        SpannableString spannableString2 = new SpannableString("点击 按钮表示不喜欢!");
        //调用自定义的imageSpan,实现文字与图片的横向居中对齐
        CustomImageSpan imageSpan2 = new CustomImageSpan(this, R.mipmap.smallunlike, 2);
        //setSpan插入内容的时候,起始位置不替换,会替换起始位置到终止位置间的内容,含终止位置。
        //Spanned.SPAN_EXCLUSIVE_EXCLUSIVE模式用来控制是否同步设置新插入的内容与start/end 位置的字体样式,此处没设置具体字体,所以可以随意设置
        spannableString2.setSpan(imageSpan2, 2, 3, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        tv_test2.setText(spannableString2);

    }
}

你可能感兴趣的:(Android基础)