textView显示html带img标签的字符串(支持minSdkVersion>8)

public class Html_TextView_TestActivity extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		initImageLoad();
		TextView tv = (TextView) findViewById(R.id.textView1);
		String html = "<html><head><title>TextView使用HTML</title></head><body><p><strong>强调</strong></p><p><em>斜体</em></p>"
				+ "<p><a href=\"http://www.dreamdu.com/xhtml/\">超链接HTML入门</a>学习HTML!</p><p><font color=\"#aabb00\">颜色1"
				+ "</p><p><font color=\"#00bbaa\">颜色2</p><h1>标题1</h1><h3>标题2</h3><h6>标题3</h6><p>大于>小于<</p><p>"
				+ "下面是网络图片</p></body></html>";

		tv.setMovementMethod(ScrollingMovementMethod.getInstance());// 滚动
		tv.setText(Html.fromHtml(html));
		ImageGetter imgGetter = new Html.ImageGetter() {
			public Drawable getDrawable(String source) {
				Drawable drawable = null;
				URL url;
				try {
					url = new URL(source);
					drawable = Drawable.createFromStream(url.openStream(), ""); // 获取网路图片
				} catch (Exception e) {
					return null;
				}
				drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
						drawable.getIntrinsicHeight());
				return drawable;
			}
		};
		tv.append(Html.fromHtml("的顶顶顶顶顶顶顶顶顶顶顶顶顶顶<br><img src=\"http://img4q.duitang.com/uploads/item/201309/22/20130922162325_cFHSM.thumb.700_0.jpeg\"/>", new URLImageGetter(tv), null));
	}
	public class URLDrawable extends BitmapDrawable {

	    protected Bitmap bitmap;

	    @Override
	    public void draw(Canvas canvas) {
	        if (bitmap != null) {
	            canvas.drawBitmap(bitmap, 0, 0, getPaint());
	        }
	    }
	}
	public class URLImageGetter implements Html.ImageGetter {

	    TextView textView;

	    public URLImageGetter(TextView textView) {
	        this.textView = textView;
	    }

	    @Override
	    public Drawable getDrawable(String paramString) {

	        final URLDrawable urlDrawable = new URLDrawable();
	        ImageLoader.getInstance().loadImage(paramString, new SimpleImageLoadingListener() {
	            @Override
	            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
	                urlDrawable.bitmap = loadedImage;
	                urlDrawable.setBounds(0, 0, loadedImage.getWidth(), loadedImage.getHeight());
	                //textView.invalidate();
	                //为了防止图片重叠必须重新设置textView
	                textView.setText(textView.getText()); 
	            }
	        });
	        return urlDrawable;
	    }
	}
	private void initImageLoad() {
		DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder() //
				.showImageForEmptyUri(R.drawable.ic_launcher) //
				.showImageOnFail(R.drawable.ic_launcher) //
				.cacheInMemory(true) //
				.cacheOnDisk(true) //
				.build();//
		L.disableLogging();
		ImageLoaderConfiguration config = new ImageLoaderConfiguration//
		.Builder(getApplicationContext())//
				.defaultDisplayImageOptions(defaultOptions)//
				.discCacheSize(50 * 1024 * 1024)//
				.diskCacheFileCount(100)  // 可以缓存的文件数量
				.discCacheFileCount(100).writeDebugLogs()//
				.discCache(new LimitedAgeDiscCache(getCacheDir(), 7 * 24 * 60 * 60))// 自定义缓存路径,7天后自动清除缓存
				.build();//
		ImageLoader.getInstance().init(config);
	}
}


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.linf.test"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="19" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".Html_TextView_TestActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值