如何使用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模板。
答案 0 :(得分:1)
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不是你需要的最佳选择,也不容易扩展。
答案 1 :(得分:0)
这个已回答的问题可以帮助您:AsyncTask Android example 这是一个很好的AsyncTask示例:http://samir-mangroliya.blogspot.in/p/android-asynctask-example.html
答案 2 :(得分:0)
您是否有理由使用TextView而不是WebView?
根据你正在做的事情,我会说webview更合适,但是如果你这样做,你应该看看this answer。
答案 3 :(得分:0)
使用Future
Future表示异步计算的结果。提供方法以检查计算是否完成,等待其完成,以及检索计算结果。
可在此处找到更多信息http://developer.android.com/reference/java/util/concurrent/Future.html