Android将图像加载到GridView中

时间:2015-12-10 12:58:14

标签: android android-imageview android-gridview

我想将本地图片加载到GridView中。

我的适配器中的代码是

public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {
        // if it's not recycled, initialize some attributes
        imageView = new ImageView(context);
        //imageView.setLayoutParams(new GridView.LayoutParams(85, 85));


        imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }


    Bitmap btmp = BitmapFactory.decodeFile(images.get(position).getAbsolutePath());

    BitmapImage img = new BitmapImage(btmp);
    btmp = ThumbnailUtils.extractThumbnail(btmp, 128,128);

    imageView.setImageBitmap(btmp);
    return imageView;
}

现在我的问题是加载我的应用需要很长时间(几秒钟)。滚动也非常麻烦。

有没有快速的方法来创建我的照片概览的缩略图?

或者是否有网格单元并行加载的方式? 或者只显示一张空白图片并在加载缩略图后替换它?

1 个答案:

答案 0 :(得分:0)

使用毕加索图书馆 http://square.github.io/picasso/

Picasso.with(上下文)
    .load(R.drawable.your_image)
    .placeholder(R.drawable.icon_waitong)
    .error(R.drawable.icon_error)
    .into(ImageView的);