java - 自定义从 Html 字符串引用的图像

标签 java android html image

我的项目 Activity 之一包括用 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"> 

但它不起作用。

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

最佳答案

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

 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;
       };
      }
    }

关于java - 自定义从 Html 字符串引用的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17197701/

相关文章:

java - 我对象的ArrayList,indexOf问题

java - Groovy:动态方法调用及其参数

actionbar compat 中的 Android searchview 显示快速操作菜单,但停止获取键盘事件

html - 响应式选项卡式布局可能吗?

html - 当我调整浏览器大小时,我的 html 页面上的元素会移动

java - 如何实例化 java.lang.reflect.Method?

java - 如何让 GSON 将普通 String 属性视为 AtomicReference<String>?

android - ExoPlayer:删除播放按钮和时间线行之间的冗余空格

android - 为什么我的 android studio 在安装(运行)后没有自动启动应用程序

html - CSS:如何围绕居中元素对齐元素?