首先一段html字符串
String source = "this is a test of <b>ImageGetter</b> it contains " +
"two images: <br/>" +
"<img src=\"http://developer.android.com/assets/images/dac_logo.png\"><br/>and<br/>" +
"<img src=\"http://developer.android.com/assets/images/icon_search.png\">";
然后ImageGetter里面的getDrawable(String souce)方法里面实现
- LevelListDrawable d = new LevelListDrawable();
- rawable empty = getResources().getDrawable(R.drawable.ic_launcher);
- d.addLevel(0, 0, empty);
- d.setBounds(0, 0, empty.getIntrinsicWidth(), empty.getIntrinsicHeight());
- new LoadImage().execute(source, d);
这里的LodImage类是一个异步加载类,详见代码了
- class LoadImage extends AsyncTask<Object, Void, Bitmap> {
- private LevelListDrawable mDrawable;
- @Override
- protected Bitmap doInBackground(Object... params) {
- String source = (String) params[0];
- mDrawable = (LevelListDrawable) params[1];
- Log.d(TAG, "doInBackground " + source);
- try {
- InputStream is = new URL(source).openStream();
- return BitmapFactory.decodeStream(is);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return null;
- }
- @Override
- protected void onPostExecute(Bitmap bitmap) {
- Log.d(TAG, "onPostExecute drawable " + mDrawable);
- Log.d(TAG, "onPostExecute bitmap " + bitmap);
- if (bitmap != null) {
- BitmapDrawable d = new BitmapDrawable(bitmap);
- mDrawable.addLevel(1, 1, d);
- mDrawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
- mDrawable.setLevel(1);
- // i don‘t know yet a better way to refresh TextView
- // mTv.invalidate() doesn‘t work as expected
- CharSequence t = mTv.getText();
- mTv.setText(t);
- }
- }
- }
目前还不是很了解这个LevelListDrawable,不知道这个addLevel(int low, int high, Drawable drawable)里的low和hight有什么用途,但是官网有个例子
- <level-list xmlns:android="http://schemas.android.com/apk/res/android">
- <item android:maxLevel="0" android:drawable="@drawable/ic_wifi_signal_1" />
- <item android:maxLevel="1" android:drawable="@drawable/ic_wifi_signal_2" />
- <item android:maxLevel="2" android:drawable="@drawable/ic_wifi_signal_3" />
- <item android:maxLevel="3" android:drawable="@drawable/ic_wifi_signal_4" />
- </level-list>
可能和这里的maxLevel有关,官网说android的手机充电的效果图就是使用这个类实现的,然后循环实现LevelListDrawable.setLevel(int)方法的。