图像显示在TextView中 [英] Show Image in TextView
本文介绍了图像显示在TextView中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要显示的图像在TextView中。我的图片保存在RES / raw目录。我尝试使用HTML.ImageGetter但无法找到同样的一个完整的参考。
解决方案
进口android.app.Activity;
进口android.graphics.drawable.Drawable;
进口android.os.Bundle;
进口android.text.Html;
进口android.text.Html.ImageGetter;
进口android.widget.TextView;
公共类TextImageActivity延伸活动{
INT imageNumber = 1; // int值检查,显示其图像
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
TextView的tvText =(TextView中)findViewById(R.id.text);
最后弦乐testContent =< HTML><身体GT;< B>测试与LT; / B>< I>斜体< / I>< BR />中
+< IMG SRC = \的icon.png \/>这是类似的测试,如果这件事作品+< IMG SRC = \a.png \/>中+
在一个更详细的&所述; /身体GT;&所述; / HTML>中;
tvText.setText(Html.fromHtml(testContent,imgGetter,NULL));
}
私人ImageGetter imgGetter =新ImageGetter(){
公众可绘制getDrawable(字符串源){
可绘制绘制= NULL;
如果(imageNumber == 1){
绘制= getResources()getDrawable(R.raw.icon)。
++ imageNumber;
}其他绘制= getResources()getDrawable(R.raw.a)。
drawable.setBounds(0,0,drawable.getIntrinsicWidth(),可拉伸
.getIntrinsicHeight());
返回绘制;
}
};
}
I wish to show Images in TextView. My Images are saved in res/raw directory. I tried using HTML.ImageGetter but could not find a complete reference for same.
解决方案
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.widget.TextView;
public class TextImageActivity extends Activity {
int imageNumber = 1; //int to check which image is displayed
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tvText = (TextView) findViewById(R.id.text);
final String testContent = "<html><body><b>Test</b><i>Italic</i><br/>"
+ "<img src=\"icon.png\"/>This is like testing if this thing works" + "<img src=\"a.png\"/>" +
" in a more elaborate</body></html>";
tvText.setText(Html.fromHtml(testContent, imgGetter, null));
}
private ImageGetter imgGetter = new ImageGetter() {
public Drawable getDrawable(String source) {
Drawable drawable = null;
if(imageNumber == 1) {
drawable = getResources().getDrawable(R.raw.icon);
++imageNumber;
} else drawable = getResources().getDrawable(R.raw.a);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable
.getIntrinsicHeight());
return drawable;
}
};
}
这篇关于图像显示在TextView中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文