u2fan
u2fan

Reputation: 121

Fixing tiny images in GridView?

I'm trying to implement a basic GridView gallery as per the Android Developer Guide/Tutorial.

The ImageViews inside my grid are Bitmaps taken from a user's camera.

This works fine except for the fact that my images are incredibly small.

My xml:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

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

</LinearLayout>

My ImageAdapter

public class ImageAdapter extends BaseAdapter {
    private Context mContext;
    private ArrayList<Bitmap> imgs;

    public ImageAdapter(Context c, ArrayList<Bitmap> arrayList) {
        mContext = c;
        imgs = arrayList;
    }

    public int getCount() {
        return imgs.size();
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    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(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageBitmap(imgs.get(position));
        return imageView;
    }

}

I've tried various things to fix this issue, but the only one that seems to work is adjusting the ImageView's layout params to enormous numbers (e.g. GridView.LayoutParams(300, 300)). However, even if I do this, my images are still relatively small (i.e. nowhere near 300dp x 300dp).

Any idea what I'm doing wrong?

Upvotes: 1

Views: 1481

Answers (5)

Anton  Malmygin
Anton Malmygin

Reputation: 3496

The problem is that new GridView.LayoutParams() expect args in pixels.

So, your need to convert 85 from dp to pixels first.

Upvotes: 0

u2fan
u2fan

Reputation: 121

Setting layout params in my adapter to GridView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)) managed to fix the issue.

Found this solution at GridView items are too small - Android

Upvotes: 2

Vladimir Arevshatyan
Vladimir Arevshatyan

Reputation: 673

you should accept the answer of

android:adjustViewBounds="true"

What it means?it stretches your image to fit the height and width of your image view so they won't be small,but why you don't want to use

android:ScaleType="fitXY"?

Because first option adjusted the proportion of your image so it won't lose the quality and second one anyway stretches it and it loses quality! IMPORTANT:be aware that if image will be too small,adjust option will not work as it must save the image proportion,so its all up to you what you want to use!

Upvotes: 0

Chaudhary Amar
Chaudhary Amar

Reputation: 856

Put this param in your ImageView.

android:adjustViewBounds="true"

You can see this too.

How to find cell width from StaggeredGridLayoutManager?

Upvotes: 2

Android Android
Android Android

Reputation: 724

Try to change for bigger size:

android:columnWidth="90dp"

Upvotes: 0

Related Questions