textView顯示html帶img標籤的字串(支援minSdkVersion>8)
阿新 • • 發佈:2019-01-27
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>