ImageSpan Size Measurement with TextView and StaticLayout

北战南征 提交于 2020-01-07 06:24:34

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/41422461/imagespan-size-measurement-with-textview-and-staticlayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!