gpt4 book ai didi

java - AsyncTask 图像 getter

转载 作者:行者123 更新时间:2023-11-30 11:29:38 25 4
gpt4 key购买 nike

如何使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com