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

2015年12月25日 Android 11条评论 阅读4536次

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
看了本文是不是觉得很赞,那就赶紧点击下面按钮分享给身边的朋友吧!

11 条评论

  1. avatar comsoftdesign

    <a href="http://www.gitmira.com/zoom-pegasus-32-blum%C3%A4nner-obuva">zoom pegasus 32 blum盲nner</a> <a href="http://www.glaceking.com/nike-flynit-air-max-verde-rosado-schuhee">nike flynit air max verde rosado</a> <a href="http://www.globalsipl.com/le-coq-sportif-femmes-chaussures-argent-gris-chaussuresg">le coq sportif femmes chaussures argent gris</a> <a href="http://www.gofishbaits.com/mennns-nike-air-max-1-bhm-obuvc">mennns nike air max 1 bhm</a> <a href="http://www.golyapoch.com/nike-kobe-10-elite-blanc-wolf-kurpesb">nike kobe 10 elite blanc wolf</a> <a href="http://www.goodbirdsclub.com/nike-air-max-tavas-corriendo-kurpese">nike air max tavas corriendo</a>
    comsoftdesign http://www.comsoftdesign.com/

  2. avatar olafhuth

    <a href="http://www.lauraundleon.com/volcom-blinker-hat-hats">volcom blinker hat</a> <a href="http://www.michaelphenow.com/air-max-ultra-br-plus-sneakere">air max ultra br plus</a> <a href="http://www.kguides.com/jordan-4-cem%C3%A6ndt-gr%C3%A5-2012-obuvc">jordan 4 cem忙ndt gr氓 2012</a> <a href="http://www.nvusforex.com/nike-lakers-44-jerry-west-green-salute-to-service-nba-swingman-jersey-nflw">nike lakers 44 jerry west green salute to service nba swingman jersey</a> <a href="http://www.dinerwaresb.com/detroit-lions-jason-hanson-jersey-nflv">detroit lions jason hanson jersey</a> <a href="http://www.slipoverfx.com/kids-nike-light-up-shoes-white-green-skog">kids nike light up shoes white green</a>
    olafhuth http://www.olafhuth.com/

  3. avatar polurnetsucks

    <a href="http://www.synthcourses.com/femminile-nike-air-max-2014-rosso-verde-kurpesb">femminile nike air max 2014 rosso verde</a> <a href="http://www.talentbschool.com/nike-philadelphia-eagles-13-nelson-agholor-ash-backer-t-shirt-nflr">nike philadelphia eagles 13 nelson agholor ash backer t shirt</a> <a href="http://www.chatavalanche.com/air-jordan-retro-30.5-oro-naranja-butyg">air jordan retro 30.5 oro naranja</a> <a href="http://www.demuslimah.com/mens-nike-dallas-cowboys-72-travis-frederick-limited-black-2016-salute-to-service-nfl-jersey-nflz">mens nike dallas cowboys 72 travis frederick limited black 2016 salute to service nfl jersey</a> <a href="http://www.getbizbuilder.com/limited-nike-black-mens-sean-davis-jersey-nfl-21-pittsburgh-steelers-2016-salute-to-service-nflw">limited nike black mens sean davis jersey nfl 21 pittsburgh steelers 2016 salute to service</a> <a href="http://www.harveenandraj.com/nike-air-huarache-hvit-metallic-s%C3%B8lv-chaussuresa">nike air huarache hvit metallic s酶lv</a>
    polurnetsucks http://www.polurnetsucks.com/

  4. avatar cbz431nj

    <a href="http://www.bq1fidt1.com/nike-air-force-1-low-tout-noir-croc-chaussuresd">nike air force 1 low tout noir croc</a> <a href="http://www.cavx545c.com/wmns-nike-free-balanza-amarillo-rojo-obuve">wmns nike free balanza amarillo rojo</a> <a href="http://www.t5vyq561.com/nike-lebron-ambassador-9-blau-gr%C3%BCn-kurpesd">nike lebron ambassador 9 blau gr眉n</a> <a href="http://www.359t5oos.com/nike-flyknit-lunar-1-verde-azul-negro-specs-schuhec">nike flyknit lunar 1 verde azul negro specs</a> <a href="http://www.e650nlg2.com/maschio-nike-shox-vendita-r4-butyg">maschio nike shox vendita r4</a> <a href="http://www.1yk6hs47.com/air-max-90-ripstop-armada-scarped">air max 90 ripstop armada</a>
    cbz431nj http://www.cbz431nj.com/

  5. avatar synexiemobile

    <a href="http://www.fhomestudio.com/nike-hyperdunk-on-verkauf-yarn-butyg">nike hyperdunk on verkauf yarn</a> <a href="http://www.ktfinans.com/nike-free-3.0-focfly-so-rosado-verde-scarpea">nike free 3.0 focfly so rosado verde</a> <a href="http://www.ladivorcecpa.com/adidas-john-wtutti-bianca-trim-schuhec">adidas john wtutti bianca trim</a> <a href="http://www.moxiepouch.com/air-jordan-9-low-bl%C3%A5-oransje-butyf">air jordan 9 low bl氓 oransje</a> <a href="http://www.pepsisite.com/nike-free-run-4.0-v3-naranja-rosado-butyf">nike free run 4.0 v3 naranja rosado</a> <a href="http://www.watfashion.com/zx-flux-reflective-grau-obuvf">zx flux reflective grau</a>
    synexiemobile http://www.synexiemobile.com/

  6. avatar kh746hau

    <a href="http://www.h8yuex60.com/schwarz-leder-nike-air-max-junior-butya">schwarz leder nike air max junior</a> <a href="http://www.bedkl965.com/nike-lebron-gul-hvit-scarpee">nike lebron gul hvit</a> <a href="http://www.0fn25hbu.com/ua-curry-3-gr%C3%B8n-bl%C3%A5-butyf">ua curry 3 gr酶n bl氓</a> <a href="http://www.ag367vcp.com/herren-nike-zoom-pegasus-32-gold-rot-butyf">herren nike zoom pegasus 32 gold rot</a> <a href="http://www.286zdvwi.com/nike-hyperdunk-2016-low-guld-gr%C3%A5-schuhea">nike hyperdunk 2016 low guld gr氓</a> <a href="http://www.cnvgx2q2.com/billig-jordan-7-chaussuresa">billig jordan 7</a>
    kh746hau http://www.kh746hau.com/

  7. 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/

  8. 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/

  9. 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/

  10. 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]

  11. 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