6

我的项目活动之一包括用 string.xml 编写的长文本,我从 textview 中的资源中添加了一些图像,因为每个短语之后都有图像,然后是文本短语,然后是图像等等,

我使用以下代码从字符串中获取图像:

public class MainActivity extends Activity {

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

        TextView htmlTextView = (TextView) findViewById(R.id.day_tv);
        htmlTextView.setText(Html.fromHtml(getString(R.string.day), new ImageGetter(), null));
    }

    private class ImageGetter implements Html.ImageGetter {

        public Drawable getDrawable(String source) {
            int id = 0;
            if (source.equals("image1.jpg")) {
                id = R.drawable.a;
            } else if (source.equals("image2.jpg")) {
                id = R.drawable.b;
            } else if (source.equals("image3.jpg")) {
                id = R.drawable.c;
            } else if (source.equals("image4.jpg")) {
                id = R.drawable.d;
            } else if (source.equals("image5.jpg")) {
                id = R.drawable.e;
            }

            Drawable d = getResources().getDrawable(id);
            d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
            return d;
        }
    };

}

并在 string.xml 中编写 html img 标签:

<img src="image1.jpg">

我希望能够通过更改其宽度和高度来自定义每个图像,或者将其引用为可绘制样式以在图像周围添加边框。

我尝试使用波纹管代码敌人改变宽度和高度:

<img src="image1.jpg" width="100" height="150"> 

但它不起作用。

任何实现这一目标的建议将不胜感激,谢谢。

4

2 回答 2

2

我最终得到了这个解决方案,它允许更改每个图像的宽度和高度,但仍然没有实现一个点,即将图像引用为可绘制形状作为背景,仍然可以使用它:

 private class ImageGetter implements Html.ImageGetter {

    public Drawable getDrawable(String source) {
        int id,id1,id2,id3,id4 = 0;

        if (source.equals("image1.jpg")) {
            id = R.drawable.a;
            Drawable d = getResources().getDrawable(id);
            d.setBounds(0, 0, 80, 20);
         return d;
         }           
        else if (source.equals("image2.jpg")) {
            id1 = R.drawable.b;
            Drawable d1 = getResources().getDrawable(id1);
            d1.setBounds(0, 0, 100, 40);
         return d1;
         }           
        else if (source.equals("image3.jpg")) {
            id2 = R.drawable.c;
            Drawable d2 = getResources().getDrawable(id2);
            d2.setBounds(0, 0, 120, 60);
         return d2; 
         }          
         else if (source.equals("image4.jpg")) {
            id3 = R.drawable.d;                      
            Drawable d3 = getResources().getDrawable(id3);
            d3.setBounds(0, 0, 140, 80);           
        return d3;
         }
         else if (source.equals("image5.jpg")) {
             id4 = R.drawable.e;                      
             Drawable d4 = getResources().getDrawable(id4);
             d4.setBounds(0, 0, 160, 100);            
         return d4;
     }
        return null;
       };
      }
    }
于 2013-06-24T13:37:58.643 回答
1

问题出在Html.fromHtml()它只支持非常有限的一组标签和属性<img>支持,但不支持设置图像的宽度和高度。TextView可以通过(通过使用SpannableStringand )来实现它ImageSpan,但是它更复杂并且不是很灵活。

相反,您应该使用WebView正确处理 HTML 的 a 。

加载字符串很容易:

WebView webView = (WebView) findViewById(R.id.web_view);
webView.loadData(getResources(R.string.day), "text/html", null);

您可以在文档中找到更多示例。


只是为了比较,这里是一个没有使用 a 的例子WebView

protected void onCreate(Bundle savedInstanceState) {
    ...
    TextView textView = (TextView) findViewById(R.id.text_view);
    SpannableString ss = new SpannableString("Hello :)");

    // Draw the image with a size of 50x50
    Drawable d = getResources().getDrawable(R.drawable.happy_face);
    d.setBounds(0, 0, 50, 50);

    // Replace the ":)" in the string by our drawable
    ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
    ss.setSpan(span, 6, 8, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
    textView.setText(ss);
}

它会加载得更快,但你不能使用 HTML。

于 2013-06-22T18:55:30.183 回答