mcdev
mcdev

Reputation: 31

Image cropping to fit in a GridView


I need to display three columns of ImageView in a GridView and I want to square the image to avoid this effect https://www.dropbox.com/s/hjc55ijbf8cdkao/SC20130430-194230.png

This is my xml code:

<?xml version="1.0" encoding="utf-8"?>

<GridView 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/grid_view"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:numColumns="3"
  android:columnWidth="90dp"
  android:horizontalSpacing="10dp"
  android:verticalSpacing="10dp"
  android:gravity="center"
  android:stretchMode="columnWidth" >  

</GridView>

and the adapter:

public class PhotoListAdapter extends ArrayAdapter<Photo> {

Context context;

// Constructor
public PhotoListAdapter(Context context, ArrayList<Photo> mPhotoList) {
    super(context, R.layout.item_album_detail, mPhotoList);
    this.context = context;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    // Inflating
    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = inflater.inflate(R.layout.item_album_detail, parent, false);

    // Linking view by id
    ImageView pic = (ImageView) convertView.findViewById(R.id.current_img);

    // Get current item
    Photo item = getItem(position);

    // Set widget
    pic.setScaleType(ImageView.ScaleType.CENTER_CROP);
    ImageDownloader imgd = new ImageDownloader(pic);
    imgd.execute(item.getPhoto_m());

    return convertView;
}

}

in item_album_detail.xml there's just a ImageView

<ImageView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:contentDescription="@string/desc"
  android:id="@+id/current_img"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:adjustViewBounds="true" />

I hope to obtain this https://www.dropbox.com/s/i5gimy1lcwyavui/SC20130430-195710.png

Upvotes: 1

Views: 3085

Answers (2)

droidlooms
droidlooms

Reputation: 35

can you try changing the sizes image views in relative layout programmatically. I had to do this to get tiles(same sizes)

    DisplayMetrics displaymetrics = new DisplayMetrics();
    mActivity.getWindowManager().getDefaultDisplay()
            .getMetrics(displaymetrics);
    int height = displaymetrics.heightPixels / 2
            - (int) convertDpToPixel(1);
    int width = displaymetrics.widthPixels / 2 - (int) convertDpToPixel(1);

    iv1.setLayoutParams(new RelativeLayout.LayoutParams(width,
            height));
    iv2.setLayoutParams(new RelativeLayout.LayoutParams(width,
            height));
    iv3.setLayoutParams(new RelativeLayout.LayoutParams(width,
            height));
    iv4.setLayoutParams(new RelativeLayout.LayoutParams(width,
            height));

Upvotes: 0

Passionate Androiden
Passionate Androiden

Reputation: 732

Changing the scale type will solve your issue. but images might look stretched.

pic.setScaleType(ImageView.ScaleType.FIT_XY);

Upvotes: 1

Related Questions