简体   繁体   中英

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 (eg GridView.LayoutParams(300, 300) ). However, even if I do this, my images are still relatively small (ie nowhere near 300dp x 300dp).

Any idea what I'm doing wrong?

Put this param in your ImageView.

android:adjustViewBounds="true"

You can see this too.

How to find cell width from StaggeredGridLayoutManager?

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

尝试更改为更大的尺寸:

android:columnWidth="90dp"

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!

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM