Android 图库应用加载速度非常慢
Android Gallery App Loading Very Slow
我想构建一个图库应用程序,从我的文件夹中获取照片列表并将其显示在一页中。
我降低了图像的分辨率,这样图库加载速度会更快,但加载仍然需要很长时间。
我认为问题是每次打开应用程序时都会加载每个文件。我该如何解决?
我在应用程序中使用了 Gridview。
我的代码:
final GridView[] grid = new GridView[1];
final File[] files = fileOps.getGoodFiles();
//this will return the array of files (images) to be displayed
final CustomGrid adapter = new CustomGrid(GeoGallery.this,files);
grid[0] =(GridView)findViewById(R.id.grid);
grid[0].setAdapter(adapter);
在我的 CustomGridView 中:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View grid;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
grid = inflater.inflate(R.layout.grid_single, null);
ImageView imageView = (ImageView)grid.findViewById(R.id.grid_image);
Bitmap bitmap = BitmapFactory.decodeFile(files[position].getPath());
bitmap = Bitmap.createScaledBitmap(bitmap,120,120*bitmap.getHeight()/bitmap.getWidth(),false);
imageView.setImageBitmap(bitmap);
return grid;
}
如何让它加载得更快?
1) 您应该将 GridLayoutManager 与 RecyclerView 一起使用。 Here你可以得到一些帮助。
2) 如果我没看错,你正在加载完整图像然后压缩它。您应该使用 BitmapFactory.Options 加载压缩图像。
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap b = BitmapFactory.decodeFile(filepath[position], options);
3) 加载和操作图像等操作非常昂贵。因此,将它们放入 onBindViewHolder 方法中的一个线程中
final int pos = position;
Thread thread = new Thread() {
@Override
public void run() {
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
Bitmap b = BitmapFactory.decodeFile(filepath[pos], options);
holder.imageView.setImageBitmap(b)
} catch (Exception e)
}
}
};
它将提高您的图库的性能。
我想构建一个图库应用程序,从我的文件夹中获取照片列表并将其显示在一页中。
我降低了图像的分辨率,这样图库加载速度会更快,但加载仍然需要很长时间。
我认为问题是每次打开应用程序时都会加载每个文件。我该如何解决?
我在应用程序中使用了 Gridview。
我的代码:
final GridView[] grid = new GridView[1];
final File[] files = fileOps.getGoodFiles();
//this will return the array of files (images) to be displayed
final CustomGrid adapter = new CustomGrid(GeoGallery.this,files);
grid[0] =(GridView)findViewById(R.id.grid);
grid[0].setAdapter(adapter);
在我的 CustomGridView 中:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View grid;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
grid = inflater.inflate(R.layout.grid_single, null);
ImageView imageView = (ImageView)grid.findViewById(R.id.grid_image);
Bitmap bitmap = BitmapFactory.decodeFile(files[position].getPath());
bitmap = Bitmap.createScaledBitmap(bitmap,120,120*bitmap.getHeight()/bitmap.getWidth(),false);
imageView.setImageBitmap(bitmap);
return grid;
}
如何让它加载得更快?
1) 您应该将 GridLayoutManager 与 RecyclerView 一起使用。 Here你可以得到一些帮助。
2) 如果我没看错,你正在加载完整图像然后压缩它。您应该使用 BitmapFactory.Options 加载压缩图像。
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap b = BitmapFactory.decodeFile(filepath[position], options);
3) 加载和操作图像等操作非常昂贵。因此,将它们放入 onBindViewHolder 方法中的一个线程中
final int pos = position;
Thread thread = new Thread() {
@Override
public void run() {
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
Bitmap b = BitmapFactory.decodeFile(filepath[pos], options);
holder.imageView.setImageBitmap(b)
} catch (Exception e)
}
}
};
它将提高您的图库的性能。