gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 15:11:35 28 4
gpt4 key购买 nike

我的项目 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com