显示图像时滞后网格视图

时间:2017-02-26 15:11:43

标签: android gridview

我正在从我的设备加载图像并将它们填充到网格视图中。 我的网格视图滚动是滞后的,即使我为此目的使用异步任务。 任何帮助表示赞赏

public class Images extends Fragment {
int imageCount = 0;
GridView gv;
ArrayList<File> list;
static ImageAdapter imageAdapter;
static File root;
static ArrayList<File> a = new ArrayList<File>();

public Images() {
    // Required empty public constructor
}

ArrayList<File> imageReader(File root) {
    ArrayList<File> a = new ArrayList<>();
    File[] files = root.listFiles();
    for (int i = 0; i < files.length; i++) {
        if (files[i].isDirectory()) {
            a.addAll(imageReader(files[i]));
        } else {
            if (files[i].getName().endsWith(".jpg") || files[i].getName().endsWith(".png")) {
                a.add(files[i]);
                imageCount = imageCount + 1;
            }
        }
    }
    return a;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_images, null);
    gv = (GridView) view.findViewById(R.id.gridView);

    new MyImagesAsync().execute();
    return view;
}

public class MyImagesAsync extends AsyncTask {

    @Override
    protected void onPostExecute(Object o) {
        super.onPostExecute(o);
        imageAdapter = new ImageAdapter(getContext(), gv, Images.a);
        gv.setAdapter(imageAdapter);
    }

    @Override
    protected Object doInBackground(Object[] params) {
        root = new File("/sdcard");
        File[] files = new File("/sdcard").listFiles();
        for (int i = 0; i < files.length; i++) {
            if (files[i].isDirectory()) {
                Images.a.addAll(imageReader(files[i]));
            } else {
                if (files[i].getName().endsWith(".jpg") || files[i].getName().endsWith(".png")) {
                    Images.a.add(files[i]);
                }
            }
        }
        return null;
    }
}

}

这是我的适配器

public class ImageAdapter extends BaseAdapter {
Context c;
LayoutInflater inflater;
GridView gv;
ArrayList<File> list;

ImageAdapter(Context c, GridView gv, ArrayList<File> list) {
    this.gv = gv;
    this.list = list;
    this.c = c;
}

@Override
public int getCount() {
    return list.size();
}

@Override
public Object getItem(int i) {
    return list.get(i);
}

@Override
public long getItemId(int i) {
    return list.size();
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v;
    if (convertView == null) {
        inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.single_grid, parent, false);
    } else {
        v = convertView;
    }

    String path = list.get(position).getPath();
    Bitmap bitmap = BitmapFactory.decodeFile(path);
    Bitmap bitmap2 = scaleBitmap(bitmap, 100, 100);
    ImageView iv = (ImageView) v.findViewById(R.id.imageView);
    iv.setImageBitmap(bitmap2);
    return v;
}

public static Bitmap scaleBitmap(Bitmap bitmapToScale, float newWidth, float newHeight) {
    if (bitmapToScale == null)
        return null;
    //get the original width and height
    int width = bitmapToScale.getWidth();
    int height = bitmapToScale.getHeight();
    // create a matrix for the manipulation
    Matrix matrix = new Matrix();

    // resize the bit map
    matrix.postScale(newWidth / width, newHeight / height);

    // recreate the new Bitmap and set it back
    return Bitmap.createBitmap(bitmapToScale, 0, 0, bitmapToScale.getWidth(), bitmapToScale.getHeight(), matrix, true);
}

}

2 个答案:

答案 0 :(得分:0)

您正在主应用程序线程上加载图像。这将表现不佳。使用众多image-loading libraries available for Android中的一个,用于在后台线程上加载图像,然后在图像准备好时填充ImageView

此外:

  • 查询MediaStore图片的效率要高得多,而不是自己扫描所有可能不存在的/sdcard目录

  • 使用c.getSystemService(Context.LAYOUT_INFLATER_SERVICE)可能会为您提供一个对您的活动主题一无所知的LayoutInflater - 请getLayoutInflater()致电Activity

答案 1 :(得分:0)

使用AsyncTask进行图像加载