使用TextView和StaticLayout测量ImageSpan大小 [英] ImageSpan Size Measurement with TextView and StaticLayout

查看:257
本文介绍了使用TextView和StaticLayout测量ImageSpan大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的布局很简单,只包含一个TextView.

我想使用ImageSpan将图像加载到TextView中.

以下方法创建Spannable:

private Spannable getImageSpannable(int drawableId, int targetWidth, int targetHeight) {

    Bitmap originalBitmap = BitmapFactory.decodeResource(getResources(), drawableId);

    Bitmap bitmap = Bitmap.createScaledBitmap(originalBitmap, targetWidth, targetHeight, true);
    Drawable dr = new BitmapDrawable(getResources(), bitmap);
    dr.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());

    Spannable imageSpannable = new SpannableString("\uFFFC");
    ImageSpan imgSpan = new ImageSpan(dr, DynamicDrawableSpan.ALIGN_BOTTOM);

    imageSpannable.setSpan(imgSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return imageSpannable;
}

我使用这种方法来创建这样的内容:

public void setContent() {

    SpannableStringBuilder content = new SpannableStringBuilder();
    content.append(getImageSpannable(R.drawable.my_image, 100, 260));
    content.append("\n");

    txtContent.setText(content);
}

当我调用setContent()方法时,我的结果是这样的:

如您所见,ImageSpan与TextView的顶部之间存在很小的间隙.

这不是行距,因为我将行距设置为0.

有趣的一点是,当我从内容中删除"\ n"(在setContent方法中声明)时,该空间消失了.

还有一点是,当我尝试使用StaticLayout测量内容大小时,在第0行的末尾带有"\ n",它将返回270,而没有"\ n"则返回260.

这种行为给我带来了一些麻烦,因为我必须使用StaticLayout测量文本和ImageSpan并确定哪个巫婆可以适合TextView.

我感谢每个人都可以帮助我.

谢谢.

这是我的xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
    android:id="@+id/txtContent"
    android:layout_width="300dp"
    android:layout_height="500dp"
    android:background="#fed9f4"
    android:textSize="22sp"/>
</LinearLayout>

我做了一些测试,发现字体大小会影响ImageSpan渲染.

请问有人可以解释这种影响吗?

解决方案

我希望此方法有效

下面的代码行

public void setContent() {

    SpannableStringBuilder content = new SpannableStringBuilder();
    content.append(getImageSpannable(R.drawable.my_image, 100, 260));
    content.append("\n");

    txtContent.setText(content);
}

更改为

public void setContent() {

    SpannableStringBuilder content = new SpannableStringBuilder();
    content.append(getImageSpannable(R.drawable.my_image, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    content.append("\n");

    txtContent.setText(content);
}

并调整"R.drawable.my_image"尺寸的大小

I have a simple layout contains just one TextView.

I wanna load an image into TextView using ImageSpan.

Below method creates Spannable:

private Spannable getImageSpannable(int drawableId, int targetWidth, int targetHeight) {

    Bitmap originalBitmap = BitmapFactory.decodeResource(getResources(), drawableId);

    Bitmap bitmap = Bitmap.createScaledBitmap(originalBitmap, targetWidth, targetHeight, true);
    Drawable dr = new BitmapDrawable(getResources(), bitmap);
    dr.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());

    Spannable imageSpannable = new SpannableString("\uFFFC");
    ImageSpan imgSpan = new ImageSpan(dr, DynamicDrawableSpan.ALIGN_BOTTOM);

    imageSpannable.setSpan(imgSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return imageSpannable;
}

I use this method to create content like this:

public void setContent() {

    SpannableStringBuilder content = new SpannableStringBuilder();
    content.append(getImageSpannable(R.drawable.my_image, 100, 260));
    content.append("\n");

    txtContent.setText(content);
}

When I call setContent() method my result is something like this:

As you see there is small gap between ImageSpan and top of TextView.

This is not line spacing, because I set line spacing to 0.

And interesting point is when I remove "\n" from content(declared in setContent method) this space is gone.

And another point is that when I tried to measure content size using StaticLayout, with "\n" at the end bottom of line 0 it returns 270 and without "\n" it returns 260.

This behavior causes some difficulties for me, because I have to measure text and ImageSpan using StaticLayout and decide witch one can fit into TextView.

I appreciate everyone can help me.

Thanks.

Here's my xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
    android:id="@+id/txtContent"
    android:layout_width="300dp"
    android:layout_height="500dp"
    android:background="#fed9f4"
    android:textSize="22sp"/>
</LinearLayout>

I'v done some tests and I find that font size is affects ImageSpan rendering.

Can somebody explain this affect please?

解决方案

I hope this method works

The following line of code

public void setContent() {

    SpannableStringBuilder content = new SpannableStringBuilder();
    content.append(getImageSpannable(R.drawable.my_image, 100, 260));
    content.append("\n");

    txtContent.setText(content);
}

Change to

public void setContent() {

    SpannableStringBuilder content = new SpannableStringBuilder();
    content.append(getImageSpannable(R.drawable.my_image, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    content.append("\n");

    txtContent.setText(content);
}

And resize "R.drawable.my_image" dimensions

这篇关于使用TextView和StaticLayout测量ImageSpan大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆