我正在后台下载图像以获取列表。我注意到即使AsycTask
滚动也不流畅。经过几个小时的调试,我得出结论,这InputStream
是有罪的。
doInBackground
应该在非 ui 线程上工作,那么为什么它会影响 UI?如果我注释掉InputStream
代码,滚动是平滑的。
我连续有 3 张图片,所以通常会同时加载 3 张图片。
class BitmapWorkerTask extends AsyncTask<String, Void, Bitmap> {
final WeakReference<ImageView> imageViewReference;
public BitmapWorkerTask(ImageView imageView) {
// Use a WeakReference to ensure the ImageView can be garbage collected
imageViewReference = new WeakReference<ImageView>(imageView);
}
// Decode image in background.
@Override
protected Bitmap doInBackground(String... params) {
InputStream is = null;
try {
is = (InputStream) new URL(params[0]).getContent();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
// Once complete, see if ImageView is still around and set bitmap.
@Override
protected void onPostExecute(Bitmap bitmap) {
// REMOVED ON PURPOSE TO CONFIRM THAT INPUTSTREAM IS GUILTY
}
}