【问题标题】:AsyncTask ImageGetterAsyncTask ImageGetter
【发布时间】:2013-08-14 13:59:42
【问题描述】:

如何使用 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 模板。

【问题讨论】:

    标签: java android asynchronous


    【解决方案1】:

    在你的onCreate中,你需要将set设置为“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 不是您所需要的最佳选择,也不容易扩展。

    【讨论】:

    • 我建议使用Future,但是你的做法更适合android。看起来不错!
    • 我不想下载图片。基本上我使用 Html.ImageGetter 来处理所有的 标签,所以我实现了 getDrawable 函数。我想在 AsyncTask 类中使用它。但是我不知道如何返回那个drawable... :(我的意思是,我在mItem.description + mItem.photo(项目的第一张图片(一些新闻)中有很多图片)。
    • 和 ImageGetter 下载图片 ;-)
    • 如何在你的方法中返回一个 Drawable?
    • 您可以将 doInBackgroubnd 返回的类型更改为您想要的任何类型。用 Drawable 替换字符串。
    【解决方案2】:

    这个回答的问题可以帮助你:AsyncTask Android example 这是一个很好的 AsyncTask 示例:http://samir-mangroliya.blogspot.in/p/android-asynctask-example.html

    【讨论】:

      【解决方案3】:

      您使用 TextView 而不是 WebView 有什么原因吗?

      根据你正在做的事情,我会说 webview 更合适,但如果你打算这样做,你应该看看this answer

      【讨论】:

      • WebView 很慢,而且占用大量内存。实际上在布局中使用 ImageView 会更好...
      【解决方案4】:

      使用Future

      Future 表示异步计算的结果。提供了检查计算是否完成、等待其完成以及检索计算结果的方法。

      更多信息可以在这里找到http://developer.android.com/reference/java/util/concurrent/Future.html

      【讨论】:

        猜你喜欢
        • 2011-11-17
        • 1970-01-01
        • 2011-12-13
        • 2011-12-06
        • 2013-08-12
        • 1970-01-01
        • 1970-01-01
        • 2016-02-22
        • 1970-01-01
        相关资源
        最近更新 更多