Java 异步任务图像获取器
如何使用AsyncTask完成此任务? 这将在文本视图中显示一些html内容。我想使用AsyncTask来显示“正在加载”消息,因为这种方式花费了太多时间…:Java 异步任务图像获取器,java,android,asynchronous,Java,Android,Asynchronous,如何使用AsyncTask完成此任务? 这将在文本视图中显示一些html内容。我想使用AsyncTask来显示“正在加载”消息,因为这种方式花费了太多时间…: @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragme
@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;
}
}
}
@覆盖
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图根视图=充气机。充气(R.layout.fragment\u stire\u detail,
货柜(虚假);;
//在文本视图中将虚拟内容显示为文本。
如果(mItem!=null){
TextView noteView=((TextView)rootView
.findviewbyd(R.id.stire_细节);
字符串EntireStire=“”+mItem.title+”
“+”
“+mItem.description
+“
”+mItem.content+”;
noteView.setText(Html.fromHtml(EntireStire,新的MyImageGetter(),
空);
setMovementMethod(LinkMovementMethod.getInstance());
}
返回rootView;
}
私有类MyImageGetter实现Html.ImageGetter{
@凌驾
公共可提取getDrawable(字符串arg0){
位图;
试一试{
BitmapFactory.Options o=新的BitmapFactory.Options();
o、 inJustDecodeBounds=true;
BitmapFactory.decodeStream(
(InputStream)新URL(arg0.getContent(),null,o);
//我们要扩展到的新尺寸
所需的最终int_尺寸=200;
//找到正确的刻度值。它应该是2的幂。
int标度=1;
而(o.outWidth/scale/2>=所需尺寸
&&o.外高度/刻度/2>=所需尺寸)
比例*=2;
//用inSampleSize解码
BitmapFactory.Options o2=新的BitmapFactory.Options();
o2.inSampleSize=刻度;
//HKEY\ U当前\用户\软件\谷歌\浏览器\地铁
位图=BitmapFactory.decodeStream(
(InputStream)新URL(arg0.getContent(),null,o2);
@抑制警告(“弃用”)
Drawable Drawable=新的位图Drawable(位图);
drawable.setBounds(0,0,drawable.getIntrinsicWidth(),
getIntrinsicHeight());
回拉;
}捕获(例外e){
Drawable d=getResources().getDrawable(R.Drawable.ic_启动器);
d、 setBounds(0,0,d.getIntrinsicWidth(),d.getIntrinsicHeight());
返回d;
}
}
}
我使用了安卓系统的主/详细流程模板。这个回答的问题可以帮助您:
这是一个很好的异步任务示例:使用文本视图而不是文本视图有什么原因吗
根据您正在做的事情,我想说webview更合适,但是如果您打算这样做,您应该签出。在onCreate中,您需要将设置设置为“加载…”或其他,然后启动任务:
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(....)
}
}
}
私有类下载WebPagetTask扩展异步任务{
@凌驾
受保护的字符串doInBackground(字符串…URL){
//下载图片并准备文本
返回响应;
}
@凌驾
受保护的void onPostExecute(字符串结果){
setText(结果);
//更好:
ImageView.setbackground(..)
}
}
}
额外注意事项
我个人会使用图片视图!textView并不是您所需的最佳选择,而且无法轻松扩展。使用
Future
未来表示异步计算的结果。提供了检查计算是否完成、等待其完成以及检索计算结果的方法
更多信息可在此处找到WebView速度慢,占用大量内存。事实上,在布局中使用ImageView会更好……我建议将来使用
但您的方法与android更相关。看起来不错!我不想下载这些图片。基本上,我使用Html.ImageGetter来处理所有标记,因此实现了getDrawable函数。我希望在AsyncTask类中使用它。但我不知道如何归还那个可提取的…:(我的意思是,我有很多图片在mItem.description+mItem.photo(项目的第一张图片(一些新闻))。ImageGetter下载图片;-)如何在你的方法中返回一个Drawable?你可以将DoinBackground返回的类型更改为你想要的任何类型。将该字符串替换为可拉伸字符串。