Java Android多线程预加载的imageview(uri)
我已经编写了一个简单的程序来在listview中显示图像,但是当我滚动列表时,速度非常慢。 我想创建一个新线程来处理imageview的加载。 我在论坛和文档中阅读过,但我不明白,您可以在单独的线程上编写简单的代码,也许可以使用asyntask来管理什么内容(在单独的线程中预加载图像,并仅在加载时显示)? (我计划不使用毕加索或其他作品) 现在在我的代码中,当我按下菜单项时,我向linearlayout添加了一个imageview(uri)。 这是我的代码: main活动: activity\u main.xml:Java Android多线程预加载的imageview(uri),java,android,multithreading,android-layout,Java,Android,Multithreading,Android Layout,我已经编写了一个简单的程序来在listview中显示图像,但是当我滚动列表时,速度非常慢。 我想创建一个新线程来处理imageview的加载。 我在论坛和文档中阅读过,但我不明白,您可以在单独的线程上编写简单的代码,也许可以使用asyntask来管理什么内容(在单独的线程中预加载图像,并仅在加载时显示)? (我计划不使用毕加索或其他作品) 现在在我的代码中,当我按下菜单项时,我向linearlayout添加了一个imageview(uri)。 这是我的代码: main活动: activity\u
谢谢
其思想是UI线程(应用程序的主线程)将
不要每次想访问文件和文件时都在等待
搜索特定图像以加载它
在这种情况下,您可以使用(例如)
AsyncTask允许您(在后台)执行后台操作
线程)不会导致UI线程冻结
正如您在文档中看到的,您应该创建一个扩展AsyncTask的类:
// In this example first param (URL) indicates the parameter you want to pass.
// The second param is the progress unit you want to publish while it is computing
// The third is the result you wish yo send to the UI thread
public class BackgroundTask extends AsyncTask<URL, Integer, Long> {
}
为了完成此操作,您可能需要扩展result函数
// In this function you should call your UI thread to set the image
protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}
对于您的示例,我建议您将onActivityResult代码移动到新的doInBackground方法中
编辑:
在您的主要活动中:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
BackgroundTask task = new BackgroundTask(mImage);
task.execute(data.getData());
在BackgroundTask类中:
protected Long doInBackground(Uri... uris) {
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(uris[0],
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
mImage.setImageURI(uris[0]);
}
其思想是UI线程(应用程序的主线程)将
不要每次想访问文件和文件时都在等待
搜索特定图像以加载它
在这种情况下,您可以使用(例如)
AsyncTask允许您(在后台)执行后台操作
线程)不会导致UI线程冻结
正如您在文档中看到的,您应该创建一个扩展AsyncTask的类:
// In this example first param (URL) indicates the parameter you want to pass.
// The second param is the progress unit you want to publish while it is computing
// The third is the result you wish yo send to the UI thread
public class BackgroundTask extends AsyncTask<URL, Integer, Long> {
}
为了完成此操作,您可能需要扩展result函数
// In this function you should call your UI thread to set the image
protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}
对于您的示例,我建议您将onActivityResult代码移动到新的doInBackground方法中
编辑:
在您的主要活动中:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
BackgroundTask task = new BackgroundTask(mImage);
task.execute(data.getData());
在BackgroundTask类中:
protected Long doInBackground(Uri... uris) {
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(uris[0],
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
mImage.setImageURI(uris[0]);
}
谢谢你的回答,我知道asyntask是如何工作的,但是我无法实现线程加载图像,你能写一些代码吗?非常感谢你,但是线性布局滚动也会滞后,为什么?四..但是我可以在没有滞后的情况下添加任意多的图像可能图像大小很大这可能是个问题(特别是在调整大图像大小时)但是当你完成加载所有图像时,它仍然滞后吗?谢谢你的回答,我知道asyntask是如何工作的,但是我无法实现线程加载图像,你能写一些代码吗?非常感谢,但是线性布局滚动也滞后,为什么?四..但是我可以在没有滞后的情况下添加尽可能多的图像大小非常大这可能是个问题(特别是当你调整大图像的大小时),但是当你加载完所有的图像后,它仍然滞后吗?