Fixing tiny images in GridView?

倾然丶 夕夏残阳落幕 提交于 2019-12-23 02:39:32

问题


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?


回答1:


Put this param in your ImageView.

android:adjustViewBounds="true"

You can see this too.

How to find cell width from StaggeredGridLayoutManager?




回答2:


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




回答3:


Try to change for bigger size:

android:columnWidth="90dp"



回答4:


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!




回答5:


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

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



来源:https://stackoverflow.com/questions/31311355/fixing-tiny-images-in-gridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!