开发者

android:how to display image in the textview

How can I display a image for html in a textview? Here is the resource

<blockquote>小浪观剧团:<p>【郭德纲《今夜有戏-济公传》】@北京德云社 15周年庆@郭德纲 相声专场演出,最新一季《济公传》,天津卫视@今夜有戏 整合全程播出。 http://t.cn/asLuvf 点击进入“德云社15周年系列演出新浪独家视频专题”http://t.cn/aogRf9<br/><a href='http://ww3.sinaimg.cn/large/5e1f33eftw1dm3fva4u3lj.jpg' target='_blank' ><img src='http://ww3.sinaimg.cn/thumbnail/5e1f33eftw1dm3fva4u3lj.jpg'><\/a><\/p><blockquote><p><a href='javascript:Action(\"sinat:qq357068756@163.com\",\"RT:3368361715304896\",\"\")'><img src='http://www.shisoft.net/images/retweet.png' /><\/a><\/p>

I know I should use the Html.fromHtml(String source, Html.ImageGetter imageGetter, Html.TagHandler tagHandler)

But I don't know how开发者_JS百科 to get the ImageGetter, Can anyone help?


This worked for me,

Implementing a ImageGetter is straight and simple; you pass an ImageGetter object to be use to fetch the images that later will be use to fill the placeholders. In order to fetch the images the ImageGetter object must implement the getDrawable method. The getDrawable method must return the Drawable object that will fill the placeholder; but, if null is returned the generic image placeholder will be used instead (see implementation below).

For ImageGetter you need to override the method below:

public Drawable getDrawable (String  source)

To get images from the application resources first in the text file one inserts an html image tag like this:

<img src="my_image">

Note that "my_image" is just a name of a drawable not a path. Then use this code to diplay the text with images in TextView

  myTextView.setText(Html.fromHtml(myText, new ImageGetter() {                 
    @Override
    public Drawable getDrawable(String source) {
     Drawable drawFromPath;
     int path = myActivity.this.getResources().getIdentifier(source, "drawable", "com.package..."); 
     drawFromPath = (Drawable) myActivity.this.getResources().getDrawable(path);
     drawFromPath.setBounds(0, 0, drawFromPath.getIntrinsicWidth(), drawFromPath.getIntrinsicHeight());
     return drawFromPath;
    }
}, null));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜