欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android图文混排的几个实现思路

程序员文章站 2022-05-25 21:32:27
...

android图文混排的几个实现思路

1.使用textview的drawable

这种方式最简单直接,两种方式可以实现,一种是通过xml来设置,第二种是通过代码来设置
1.1

     <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableLeft="@mipmap/ic_launcher"
        android:text="Hello World!" />

1.2

TextView text2 = findViewById(R.id.text2);
        Drawable drawable = ContextCompat.getDrawable(this, R.mipmap.ic_launcher);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        //四个方向分别为坐上右下
        text2.setText("HELLO WORLD!");
        text2.setCompoundDrawables(drawable, null, null, null);

2.Html.fromHtml

  TextView text3 = findViewById(R.id.text3);
    String str = "<img src=\"" + R.mipmap.ic_launcher + "\">" + "Hello World!";
    text3.setText(Html.fromHtml(str, new MyImageGetter(), null));




 class MyImageGetter implements Html.ImageGetter {

    @Override
    public Drawable getDrawable(String source) {
        // 获取本地图片
        Drawable drawable = null;
        try {
            drawable = ContextCompat.getDrawable(getApplicationContext(), Integer.parseInt(source));
            // 必须设为图片的边际,不然TextView显示不出图片
            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
            // 将其返回
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return drawable;
    }
}

3.使用SpannerString

3.1

   TextView text4 = findViewById(R.id.text4);
        String str1 = "  " ;
        String str2 = str1 + "Hello World!";
        SpannableString spannableString = new SpannableString(str2);
        Drawable drawable2 = ContextCompat.getDrawable(this, R.mipmap.ic_launcher);
        drawable2.setBounds(0, 0, drawable2.getMinimumWidth(), drawable2.getMinimumHeight());
        ImageSpan imageSpan = new ImageSpan(drawable2, ImageSpan.ALIGN_BOTTOM);
        spannableString.setSpan(imageSpan,0,str1.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        text4.setText(spannableString);

3.2让图文居中展示
需要我们重写ImageSpan中的方法

    class MyImageSpan extends ImageSpan {

        public MyImageSpan(Drawable drawable) {
            super(drawable);

        }

        @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 = getDrawable();
            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();
        }
    }

参考自 blog.csdn.net/cuenca/article/details/52134333

最后是所有的效果图
Android图文混排的几个实现思路