java - AsyncTask 图像 getter

标签 java android asynchronous

如何使用 AsyncTask 执行此操作? 这在 TextView 中显示了一些 html 内容。我想用 AsyncTask 来显示“正在加载”消息,因为这种方式花费了太多时间......:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_stire_detail,
            container, false);

    // Show the dummy content as text in a TextView.
    if (mItem != null) {
        TextView noteView = ((TextView) rootView
                .findViewById(R.id.stire_detail));

        String EntireStire = "<b>" + mItem.title + " </b> <br> <img src='"
                + mItem.photo + "' > " + " <br><small>" + mItem.description
                + " <br> " + mItem.content + "</small>";

        noteView.setText(Html.fromHtml(EntireStire, new MyImageGetter(),
                null));
        noteView.setMovementMethod(LinkMovementMethod.getInstance());
    }

    return rootView;
}

private class MyImageGetter implements Html.ImageGetter {

    @Override
    public Drawable getDrawable(String arg0) {
        Bitmap bitmap;

        try {
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(
                    (InputStream) new URL(arg0).getContent(), null, o);
            // The new size we want to scale to
            final int REQUIRED_SIZE = 200;

            // Find the correct scale value. It should be the power of 2.
            int scale = 1;
            while (o.outWidth / scale / 2 >= REQUIRED_SIZE
                    && o.outHeight / scale / 2 >= REQUIRED_SIZE)
                scale *= 2;

            // Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            // HKEY_CURRENT_USER\Software\Google\Chrome\Metro
            bitmap = BitmapFactory.decodeStream(
                    (InputStream) new URL(arg0).getContent(), null, o2);

            @SuppressWarnings("deprecation")
            Drawable drawable = new BitmapDrawable(bitmap);
            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
                    drawable.getIntrinsicHeight());
            return drawable;
        } catch (Exception e) {
            Drawable d = getResources().getDrawable(R.drawable.ic_launcher);
            d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
            return d;
        }
    }
}

我使用了来自 Android 的 Master/Detail Flow 模板。

最佳答案

在您的 onCreate 中,您需要将设置设置为“loading..”或其他内容,然后开始任务:

DownloadWebPageTask task = new DownloadWebPageTask();
    task.execute(new String[] { "http://www.myUrl.com" });

然后:

private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {
      //Download your pictures and prepare the text
      return response;
    }

    @Override
    protected void onPostExecute(String result) {
      textView.setText(result);
      //Better:
      ImageView.setbackground(....)
    }
  }

} 

额外说明

我个人会为图片使用 ImageView! textView 不是您需要的最佳选择,也不会轻易缩放。

关于java - AsyncTask 图像 getter ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18233839/

相关文章:

java - 如何知道从随机数组列表中使用的最后一个可绘制对象是什么?

android - 如何将 utf-16 字符放入 Android 字符串资源中?

android - 旋转Android手机时EditText丢失用户输入

django - 如何从基于 Django 的站点管理任务的停止或重新启动?

java - 相机X : How to call the analyze method with an image

java - 将项目从 Ant 移至 Maven 并将其分解

android - 应用程序在 froyo 和 Gingerbread 中工作正常但在 ICS 中不工作

c++ - 我可以进行异步 ODBC 调用吗?有引用资料吗?

angular - 在服务 http.get 调用完成后让组件执行函数

java - 嵌入式 Jetty 处理每条消息两次