android:使用TextView实现网页图文混排效果

2015年12月25日 Android 5条评论 阅读4015次

android:使用TextView实现网页图文混排效果


新闻资讯问答博客等类型的网站在web中就表现出很丰富的现实效果,比如图片文字混合等。因此,此类网站在其对应的APP页面中,也应该是很友好的展示出图文混排效果。


在AndroidAPP中实现图文混排效果有多种办法,本文介绍的是使用TextView来实现图文混排效果


  1. 想实现网页那种图文混排,则首先要获取原来的HTML源码,这是属于网络请求接口返回的内容。

  2. 得到了原始网页的HTML,可以调用android.text.Html的如下方法
    public static Spanned fromHtml(String source, ImageGetter imageGetter, TagHandler tagHandler)
    从而得到Spanned对象

  3. 在android.widget.TextView源码中,有如下设置Text的方法
    public final void setText(CharSequence text)

  4. android.text.Spanned有如下的定义
    public interface Spanned extends CharSequence

  5. 根据3和4点可以知道调用TextView#setText(Spanned)就可以看到预期的效果

private TextView mTextView;
ImageGetter imgGetter = new Html.ImageGetter() {
    @Override
    public Drawable getDrawable(String source) {
        Drawable drawable = null;
        URL url;
        try {
            url = new URL(source);
            drawable = Drawable.createFromStream(url.openStream(), "");
        } catch (MalformedURLException e) {
            L.e("MalformedURLException: " + source, e);
        } catch (IOException e) {
            L.e("IOException: " + source, e);
        }
        if (drawable != null) {
            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
                    drawable.getIntrinsicHeight());
        }
        return drawable;
    }
};
private Spanned getSpanedText(String originalHtmlTxt) {
    Spanned result = Html.fromHtml(originalHtmlTxt);
    int length = result.length();
    ImageSpan[] imgSpans = result.getSpans(0, length, ImageSpan.class);
    for (int i = 0; i < imgSpans.length; i++) {
        ImageSpan is = imgSpans[i];
        Class<ImageSpan> clz = ImageSpan.class;
        try {
            Field field = clz.getDeclaredField("mDrawable");
            field.setAccessible(true);
            field.set(is, mTextView.getContext().getResources()
                .getDrawable(R.drawable.ic_img_not_ready));
        } catch (NoSuchFieldException e) {
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (NotFoundException e) {
            e.printStackTrace();
        }
    }
    result = Html.fromHtml(originalHtmlTxt, imgGetter, null);
    return result;
}
mTextView.setText(result);



最后说明的是getSpanedText()因为涉及网络图片读取,所以要放到子线程中异步处理。

至此,android:使用TextView实现新闻博客资讯等网页图文混排效果的介绍就结束了。
以后会继续分享用其他方法实现新闻博客资讯等APP中地网页图文混排效果。

分享本文至:

WRITTEN BY

avatar
本文标签:TextView
看了本文是不是觉得很赞,那就赶紧点击下面按钮分享给身边的朋友吧!

5 条评论

  1. avatar katiagrinkov

    <a href="http://www.mndrsngh.com/billig-nike-kobe-11-dam%C3%A4nner-blau-trainersg">billig nike kobe 11 dam盲nner blau</a> <a href="http://www.inenigroup.com/hombres-air-jordan-retro-12-rojo-shoesn">hombres air jordan retro 12 rojo</a> <a href="http://www.specpilot.com/jual-nike-free-socfly-trainersce">jual nike free socfly</a> <a href="http://www.davidmyre.com/nike-hypervenom-phantom-ii-fg-black-skor">nike hypervenom phantom ii fg black</a> <a href="http://www.gmanswindle.com/orange-green-mens-new-balance-993-shoes-trainersd">orange green mens new balance 993 shoes</a> <a href="http://www.aterisrail.com/nike-rosherun-hyperfuse-medium-navy-wolf-grey-trainersb">nike rosherun hyperfuse medium navy wolf grey</a>
    katiagrinkov http://www.katiagrinkov.com/

  2. avatar pascaltosi

    <a href="http://www.thegroceri.com/air-jordan-phase-pink-yellow-sportsc">air jordan phase pink yellow</a> <a href="http://www.rxforri.com/jordan-12-ovo-shanghai-sportse">jordan 12 ovo shanghai</a> <a href="http://www.tibetwoodcut.com/nike-air-max-90-sneakerboot-sp-skor">nike air max 90 sneakerboot sp</a> <a href="http://www.royalhoneywax.com/nike-lunarglide-8-herre-s%C3%B8lv-footf">nike lunarglide 8 herre s酶lv</a> <a href="http://www.kosarkomur.com/pandora-double-brown-leather-bracelet-pandorar">pandora double brown leather bracelet</a> <a href="http://www.hiussalipiilo.com/nike-roshe-all-black-one-fashion-trainersf">nike roshe all black one fashion</a>
    pascaltosi http://www.pascaltosi.com/

  3. avatar brendonrush

    <a href="http://www.araricambi.com/nobis-yatesy-jacket-nobisr">nobis yatesy jacket</a> <a href="http://www.paulacorley.com/billig-nike-lebron-11-dam%C3%A4nner-orange-trainersa">billig nike lebron 11 dam盲nner orange</a> <a href="http://www.lbsportsguys.com/links-of-london-restring-sweetie-jewelryr">links of london restring sweetie</a> <a href="http://www.iraqbody.com/nike-flyknit-lunar-3-lb-man-sneakere">nike flyknit lunar 3 lb man</a> <a href="http://www.debbiebissett.com/north-face-denali-2-mens-jacket-northfacer">north face denali 2 mens jacket</a> <a href="http://www.michellestill.com/charms-for-bracelets-pandora-style-pandorar">charms for bracelets pandora style</a>
    brendonrush http://www.brendonrush.com/

  4. avatar jindianhua

    <a href="http://www.supermarketes.com/bon-march%C3%A9-nike-air-max-95-femmes-vert-trainersa">bon march茅 nike air max 95 femmes vert</a> <a href="http://www.theblases.com/hermes-bracelet-green-jewelryr">hermes bracelet green</a> <a href="http://www.dandoelroll.com/nike-roshe-two-flyknit-gold-black-sneakerf">nike roshe two flyknit gold black</a> <a href="http://www.christinabox.com/the-north-face-steep-tech-women-northfacer">the north face steep tech women</a> <a href="http://www.segwaycr.com/pandora-bracelet-leather-pink-pandorar">pandora bracelet leather pink</a> <a href="http://www.amarthur.com/pandora-charms-family-love-pandorar">pandora charms family love</a>
    [url=http://www.jindianhua.com/]jindianhua[/url]

  5. avatar healtobfree

    <a href="http://www.floornhome.com/adidas-forum-mid-herren-runninge">adidas forum mid herren</a> <a href="http://www.juniorensea.com/michael-kors-jet-set-travel-leather-tote-mkr">michael kors jet set travel leather tote</a> <a href="http://www.jlredcross.com/bon-march%C3%A9-nike-dunk-high-femmes-or-sportsf">bon march茅 nike dunk high femmes or</a> <a href="http://www.synexiemobile.com/nike-limited-mens-pittsburgh-steelers-84-antonio-brown-80th-anniversary-throwback-nfl-jersey-89.99-nflv">nike limited mens pittsburgh steelers 84 antonio brown 80th anniversary throwback nfl jersey 89.99</a> <a href="http://www.minruteguide.com/womens-nike-air-max-90-blue-grey-shoesg">womens nike air max 90 blue grey</a> <a href="http://www.emigunindia.com/nike-free-4.0-v3-men-mens-nike-shoesb">nike free 4.0 v3 men mens nike</a>
    healtobfree http://www.healtobfree.com/

欢迎留言




用户登录

sitemap