TextView通过Html显示图片

		// 1.显示本地图片
		String str = "显示本地图片:<img src=\"/mnt/sdcard/temp/test.jpg\" />";
		mTextView.setText(Html.fromHtml(str, new Html.ImageGetter() {
			@Override
			public Drawable getDrawable(String s) {
				Drawable drawable = null;
				drawable = Drawable.createFromPath(s);
				drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
						drawable.getIntrinsicHeight());
				return drawable;
			}
		}, null));
		// --------------------------------------------------------------------------------------------------------------------------------------------------
		// 2.显示资源图片(例如:res\drawable-xxhdpi中的图片)
		String str = "显示资源图片:<img src=\"" + R.drawable.xing + "\" />";
		mTextView.setText(Html.fromHtml(str, new Html.ImageGetter() {
			@Override
			public Drawable getDrawable(String s) {
				Drawable drawable = null;
				int rId = Integer.parseInt(s);
				drawable = getResources().getDrawable(rId);
				drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
						drawable.getIntrinsicHeight());
				return drawable;
			}
		}, null));

		// --------------------------------------------------------------------------------------------------------------------------------------------------
		// 3.显示网络图片
		String str = "显示网络图片:<img src=\"https://www.baidu.com/img/bd_logo1.png\" />";
		mTextView.setText(Html.fromHtml(str, new Html.ImageGetter() {
			@Override
			public Drawable getDrawable(String s) {
				Drawable drawable = null;
				URL url;
				try {
					url = new URL(s);
					drawable = Drawable.createFromStream(url.openStream(), "");
				} catch (Exception e) {
					e.printStackTrace();
					return null;
				}
				drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
						drawable.getIntrinsicHeight());
				return drawable;
			}
		}, null));


你可能感兴趣的:(html,android,图片,textview)