【问题标题】:How to set custom rows and columns according to image dimensions?如何根据图像尺寸设置自定义行和列?
【发布时间】:2017-10-27 12:53:45
【问题描述】:

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

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

例如。 1 => 如果获取小尺寸图片

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

因此,网格调整为:

  • 单元格尺寸
  • 行数
  • 列数

这是我当前的代码:

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;
    }
}

【问题讨论】:

    标签: android gridview android-recyclerview imageview


    【解决方案1】:

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

    Github 链接 - https://github.com/google/flexbox-layout

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

    【讨论】:

      猜你喜欢
      • 2015-08-28
      • 1970-01-01
      • 2011-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多