Android Gallery 应用加载速度很慢

标签 android image gallery thumbnails image-gallery

我想构建一个图库应用,从我的文件夹中获取照片列表并将其显示在一个页面中。

我降低了图像的分辨率,这样图库加载速度会更快,但加载仍然需要很长时间。

我认为问题是每次打开应用程序时都会加载每个文件。我该如何解决这个问题?

我在应用中使用了 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)
       }
   }
};

它将提高您的图库的性能。

关于Android Gallery 应用加载速度很慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39321381/

相关文章:

android - 使用 setSelection(position) 翻转图库 View

javascript - 更改链接悬停时的另一张图像

javascript - Shadowbox 未加载/工作

javascript - 使用 jQuery 插件或 Javascript 为文件夹中的每个图像创建一个 <li>

android - context.registerReceiver 在尝试检查电源是否已连接时返回 null Intent

android - 在android中读/写文件

java - 如何在JRadioButton上设置图像和文本?

javascript - 垂直对齐图像 hover img

java - appium如何从图库中选择照片

android - 如何在没有上下文的情况下在 Android 中创建数据库?