【问题标题】:Android Gallery App Loading Very SlowAndroid Gallery 应用程序加载非常缓慢
【发布时间】:2017-01-12 06:44:11
【问题描述】:

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

我降低了图像的分辨率,以便更快地加载图库,但仍然需要很长时间才能加载。

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

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

如何让它加载更快?

【问题讨论】:

    标签: android image gallery thumbnails image-gallery


    【解决方案1】:

    1) 您应该使用带有 RecyclerView 的 GridLayoutManager。 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)
           }
       }
    };
    

    它将提高您的画廊的性能。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多