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>