Picasso的fit()。centreCrop()在GridView中插入空格

时间:2015-09-09 12:01:08

标签: android android-gridview picasso

使用Picasso填充我的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(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(585, 585));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }


        Picasso.with(mContext)
                .load(photoUrls.get(position))                    
                .into(imageView);


    return imageView;
}

我的GridView看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/galleryGridView"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:numColumns="2"
   android:columnWidth="90dp"
   android:horizontalSpacing="10dp"
   android:verticalSpacing="10dp"
   android:gravity="center"
   android:stretchMode="columnWidth" >

</GridView>

但正如您所看到的,一些图像重叠。

enter image description here

所以我添加了fit().centerCrop()

Picasso.with(mContext)
            .load(photoUrls.get(position))
            .fit().centerCrop()
            .into(imageView);

现在图像已经很好地对齐,但是已经打开了一个巨大的空间:

enter image description here

不确定我现在需要做什么。我已经尝试过改变布局宽度/高度而没有运气。

1 个答案:

答案 0 :(得分:0)

最后通过删除填充来解决这个问题:

imageView.setPadding(8, 8, 8, 8);

fit()方法

.fit().centerCrop()

所以我的getView()方法现在看起来像这样:

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(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(585, 585));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

    } else {
        imageView = (ImageView) convertView;
    }


        Picasso.with(mContext)
                .load(photoUrls.get(position))

                .into(imageView);


    return imageView;
}