4

我想设置一个来自TextView以下SpannableString方法的:

Html.fromHtml(String source, Html.ImageGetter imageGetter, 
   Html.TagHandler tagHandler)

但是ImageGetter这里需要重写下面的方法:

public abstract Drawable getDrawable(String source)

因为我需要从互联网上获取可绘制对象,所以我必须异步进行,似乎不是。

如何让它发挥作用?谢谢。

4

3 回答 3

14

这些人做得很好,这是我使用 Square 的 Picasso 库的解决方案

//...
final TextView textView = (TextView) findViewById(R.id.description);
        Spanned spanned = Html.fromHtml(getIntent().getStringExtra(EXTRA_DESCRIPTION),
                new Html.ImageGetter() {
                    @Override
                    public Drawable getDrawable(String source) {
                        LevelListDrawable d = new LevelListDrawable();
                        Drawable empty = getResources().getDrawable(R.drawable.abc_btn_check_material);;
                        d.addLevel(0, 0, empty);
                        d.setBounds(0, 0, empty.getIntrinsicWidth(), empty.getIntrinsicHeight());
                        new ImageGetterAsyncTask(DetailActivity.this, source, d).execute(textView);

                        return d;
                    }
                }, null);
        textView.setText(spanned);
//...


class ImageGetterAsyncTask extends AsyncTask<TextView, Void, Bitmap> {


    private LevelListDrawable levelListDrawable;
    private Context context;
    private String source;
    private TextView t;

    public ImageGetterAsyncTask(Context context, String source, LevelListDrawable levelListDrawable) {
        this.context = context;
        this.source = source;
        this.levelListDrawable = levelListDrawable;
    }

    @Override
    protected Bitmap doInBackground(TextView... params) {
        t = params[0];
        try {
            Log.d(LOG_CAT, "Downloading the image from: " + source);
            return Picasso.with(context).load(source).get();
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    protected void onPostExecute(final Bitmap bitmap) {
        try {
            Drawable d = new BitmapDrawable(context.getResources(), bitmap);
            Point size = new Point();
            ((Activity) context).getWindowManager().getDefaultDisplay().getSize(size);
            // Lets calculate the ratio according to the screen width in px
            int multiplier = size.x / bitmap.getWidth();
            Log.d(LOG_CAT, "multiplier: " + multiplier);
            levelListDrawable.addLevel(1, 1, d);
            // Set bounds width  and height according to the bitmap resized size
            levelListDrawable.setBounds(0, 0, bitmap.getWidth() * multiplier, bitmap.getHeight() * multiplier);
            levelListDrawable.setLevel(1);
            t.setText(t.getText()); // invalidate() doesn't work correctly...
        } catch (Exception e) { /* Like a null bitmap, etc. */ }
    }
}

我的 2 美分……和平!

于 2015-04-17T18:31:10.957 回答
4

现在我正在使用 AsyncTask 下载图像ImageGetter

Spanned spannedContent = Html.fromHtml(htmlString, new ImageGetter() {

        @Override
        public Drawable getDrawable(String source) {
            new ImageDownloadAsyncTask().execute(textView, htmlString, source);
            return null;
        }
    }, null);

TextView并在下载图像时再次设置文本。

现在它起作用了。但是当我尝试TextView.postInvalidate()重新绘制下载的图像时它失败了。我必须setText()AsyncTask.

有谁知道为什么?

于 2010-09-22T07:06:26.997 回答
4

这是我的代码,它抓取了 html 字符串中的所有图像(它是从原始代码中简化的,所以我希望它可以工作):

private HashMap<String, Drawable> mImageCache = new HashMap<String, Drawable>();
private String mDescription = "...your html here...";

private void updateImages(final boolean downloadImages) {
    if (mDescription == null) return;
    Spanned spanned = Html.fromHtml(mDescription,
        new Html.ImageGetter() {
        @Override
        public Drawable getDrawable(final String source) {
            Drawable drawable = mImageCache.get(source);
            if (drawable != null) {
                return drawable;
            } else if (downloadImages) {
                new ImageDownloader(new ImageDownloader.ImageDownloadListener() {
                    @Override
                    public void onImageDownloadComplete(byte[] bitmapData) {
                        Drawable drawable = new BitmapDrawable(getResources(),
                                BitmapFactory.decodeByteArray(bitmapData, 0, bitmapData.length));
                        try {
                            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
                        } catch (Exception ex) {}
                        mImageCache.put(source, drawable);
                        updateImages(false);
                    }
                    @Override
                    public void onImageDownloadFailed(Exception ex) {}
                }).execute(source);
            }
            return null;
        }
    }, null);
    tvDescription.setText(spanned);
}

所以基本上这里发生的是 ImageGetter 将对 html 描述中的每个图像发出请求。如果该图像不在 mImageCache 数组中并且 downloadImages 为真,我们将运行一个异步任务来下载该图像。完成后,我们将可绘制对象添加到 hashmap 中,然后再次调用此方法(但将 downloadImages 设置为 false,因此我们不会冒无限循环的风险),其中可以使用第二次尝试。

有了这个,您将需要我使用的 ImageDownloader 类:

public class ImageDownloader extends AsyncTask {
    public interface ImageDownloadListener {
        public void onImageDownloadComplete(byte[] bitmapData);
        public void onImageDownloadFailed(Exception ex);
    }

    private ImageDownloadListener mListener = null;

    public ImageDownloader(ImageDownloadListener listener) {
        mListener = listener;
    }

    protected Object doInBackground(Object... urls) {
        String url = (String)urls[0];
        ByteArrayOutputStream baos = null;
        InputStream mIn = null;
        try {
            mIn = new java.net.URL(url).openStream();
            int bytesRead;
            byte[] buffer = new byte[64];
            baos = new ByteArrayOutputStream();
            while ((bytesRead = mIn.read(buffer)) > 0) {
                if (isCancelled()) return null;
                baos.write(buffer, 0, bytesRead);
            }
            return new AsyncTaskResult<byte[]>(baos.toByteArray());

        } catch (Exception ex) {
            return new AsyncTaskResult<byte[]>(ex);
        }
        finally {
            Quick.close(mIn);
            Quick.close(baos);
        }
    }

    protected void onPostExecute(Object objResult) {
        AsyncTaskResult<byte[]> result = (AsyncTaskResult<byte[]>)objResult;
        if (isCancelled() || result == null) return;
        if (result.getError() != null) {
            mListener.onImageDownloadFailed(result.getError());
        }
        else if (mListener != null)
            mListener.onImageDownloadComplete(result.getResult());
    }
}
于 2013-08-01T22:59:20.960 回答