怎么混合显示图片和文本
2014-06-17 11:21
351 查看
在有的Android应用中,需要在文字的中间插入一个图片,比如像下面图中所示的效果:
天气小图片显示在文字的后面,要实现此效果可以自己写一个View,但是也可以使用TextView结合android.text.Spanned来实现此效果。
Spanned的内容可以是一段html文本,图片就可以用img元素嵌入进去了,图片的内容可以根据img元素的src地址获取,也可以根据此src地址从保存在手机本地的资源文件里加载。下面是简单的示例代码:
TextView weather=(TextView) findViewById(R.id.weather);
Spanned info = null;
try
{
info = getWeather(defaultCity);
}
catch(Exception e){
}
if (info != null) {
weather.setText(info);
} else {
weather.setText("获取天气信息失败!");
}
Spanned getWeather(String city) {
String weatherData;//天气信息html片段
ImageGetter imgGetter = new Html.ImageGetter() {
@Override
public Drawable getDrawable(String url) {
Drawable drawable = null;
if (url.startsWith("/")) {
//图片url地址是个相对地址,需要变成绝对url地址
}
byte[] imgBuffer = null;
try {
//从网址url获取图片内容,保存在imgBuffer里
} catch (Exception e) {
return null;
}
String name = "";
int pos = url.lastIndexOf("/");
name = url.substring(pos + 1);//图片文件名
InputStream in = new ByteArrayInputStream(imgBuffer);
drawable = Drawable.createFromStream(in, name);//从输入流创建Drawable
try {
in.close();
} catch (IOException e) {
}
return drawable;
}
};
Spanned text = null;
try {
text = Html.fromHtml(weatherData, imgGetter, null);//创建一个Spanned
} catch (Exception e1) {
text = null;
}
return text;
}
代码比较简单。如果图片事先已经保存在资源文件里,那么就无需从网上去下载图片内容了,只需使用Drawable.createFromResourceStream这个方法从资源文件加载进来创建Drawable即可。
转自:http://blog.csdn.net/yelbosh/article/details/8039476
天气小图片显示在文字的后面,要实现此效果可以自己写一个View,但是也可以使用TextView结合android.text.Spanned来实现此效果。
Spanned的内容可以是一段html文本,图片就可以用img元素嵌入进去了,图片的内容可以根据img元素的src地址获取,也可以根据此src地址从保存在手机本地的资源文件里加载。下面是简单的示例代码:
TextView weather=(TextView) findViewById(R.id.weather);
Spanned info = null;
try
{
info = getWeather(defaultCity);
}
catch(Exception e){
}
if (info != null) {
weather.setText(info);
} else {
weather.setText("获取天气信息失败!");
}
Spanned getWeather(String city) {
String weatherData;//天气信息html片段
ImageGetter imgGetter = new Html.ImageGetter() {
@Override
public Drawable getDrawable(String url) {
Drawable drawable = null;
if (url.startsWith("/")) {
//图片url地址是个相对地址,需要变成绝对url地址
}
byte[] imgBuffer = null;
try {
//从网址url获取图片内容,保存在imgBuffer里
} catch (Exception e) {
return null;
}
String name = "";
int pos = url.lastIndexOf("/");
name = url.substring(pos + 1);//图片文件名
InputStream in = new ByteArrayInputStream(imgBuffer);
drawable = Drawable.createFromStream(in, name);//从输入流创建Drawable
try {
in.close();
} catch (IOException e) {
}
return drawable;
}
};
Spanned text = null;
try {
text = Html.fromHtml(weatherData, imgGetter, null);//创建一个Spanned
} catch (Exception e1) {
text = null;
}
return text;
}
代码比较简单。如果图片事先已经保存在资源文件里,那么就无需从网上去下载图片内容了,只需使用Drawable.createFromResourceStream这个方法从资源文件加载进来创建Drawable即可。
转自:http://blog.csdn.net/yelbosh/article/details/8039476
相关文章推荐
- Android开发系列八:怎么混合显示图片和文本
- Android开发系列:怎么混合显示图片和文本
- React Native实现图片文本混合显示
- 奇怪,Blend3 中怎么动态显示image图片呢?
- 奇怪,Blend3 中怎么动态显示image图片呢?
- 怎么让绑定的图片不间断滚动显示,且显示区域只显示部分图片?
- IE浏览器中png图片无法显示,该怎么办?
- WAP(wml)开发教程4--WML学习之显示文本和图片
- 怎么让电脑上的图片全屏显示呢
- 修改图片并不改变图片路径怎么显示修改后的图片
- WAP(wml)开发教程4--WML学习之显示文本和图片
- 怎么在同一图片框内随着鼠标移动来变换显示图片?
- 怎么能正常显示多行文本呢?
- android 怎么把网络图片生成一个Bitmap显示在ImageView中
- 通过UIWebView 显示图片 文本 pdf 文档 以及音乐和视频
- javascript 网页设计 怎么在同一位置显示几张不同的图片(图片自动变换)
- 在FireFox中,怎么让文本流压住Flash而显示在上面呢,
- 鼠标在文本上移动时层的显示与消失(以及层里显示图片)ASP.NET
- [VB.NET].NET精简框架下用VB.NET怎么实现在picturebox上显示不同图片???
- 怎么将html标签当做文本显示?