如何根据图像尺寸设置自定义行和列?

时间:2017-10-27 12:53:46

标签: android gridview android-recyclerview imageview

我从API获取5-6张图片。根据用户的选择,图像有不同的尺寸(例如30x40或300x400等)。

根据图像的大小/尺寸 - 我想更改行数以及网格的单元格大小。

例如。 1 =>如果提取小尺寸图像

enter image description here

例如。 2 =>如果获取更大尺寸的图像

enter image description here

因此,网格调整了它:

  • cell dimension
  • 行数
  • 列数

这是我目前的代码:

content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/image_sizes"
        android:layout_marginTop="80dp" />

    <GridView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/grid_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:horizontalSpacing="10dp"
        android:verticalSpacing="10dp"
        android:gravity="center">

    </GridView>

</LinearLayout>

ImageAdapter.java

public class ImageAdapter extends BaseAdapter {
    private Context mContext;
    private Integer dim;

    // Keep all Images in array
    public ArrayList mThumbIds;

    // Constructor
    public ImageAdapter(Context c, ArrayList imageList, Integer dimension){
        this.mThumbIds = imageList;
        mContext = c;
        this.dim = dimension;
    }

    @Override
    public int getCount() {
        return this.mThumbIds.size();
    }

    @Override
    public Object getItem(int position) {
        return this.mThumbIds.get(position);
    }

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

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

        ImageView imageView = new ImageView(mContext);
        Transformation t = new Transformation();

        if (this.dim == 1) {
            t.width(30);
            t.height(50);
            imageView.setLayoutParams(new GridView.LayoutParams(30, 50));
        }
        else if (this.dim == 2) {
            t.width(300);
            t.height(400);
            imageView.setLayoutParams(new GridView.LayoutParams(300, 400));
        }
        else if (this.dim == 3) {
            t.width(1000);
            t.height(800);
            imageView.setLayoutParams(new GridView.LayoutParams(1000, 800));
        }

        //imageView.setLayoutParams(new GridView.LayoutParams(70, 70));

        final Cloudinary cloud = new Cloudinary(MyConfig.getMyConfigs());
        String str_url = cloud.url().transformation(t).generate(this.mThumbIds.get(position).toString());

        URL url = null;
        try {
            url = new URL(str_url);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        Bitmap bmp = null;
        try {
            bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }

        imageView.setImageBitmap(bmp);

        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

        return imageView;
    }
}

1 个答案:

答案 0 :(得分:0)

使用Google的FlexboxLayoutManger作为RecyclerView中的LayoutManger而不是GridView

Github Link - https://github.com/google/flexbox-layout

从上面链接查看README,其中解释了其实现