2017-10-27 3 views
0

API에서 5-6 개의 이미지를 가져옵니다. 사용자 선택에 따라 이미지의 크기가 다릅니다 (예 : 30x40 또는 300x400 등).이미지 크기에 따라 맞춤 행 및 열을 설정하는 방법은 무엇입니까?

이미지의 크기/크기를 기준으로 - 행 수와 그리드의 셀 크기를 변경하고 싶습니다.

예 : 1 => 작은 크기의 이미지를 페치하는 경우

enter image description here

예. 2 => 큰 크기 이미지

enter image description here

따라서, 그리드가 그것의 조정 페치 경우

  • 행의 셀 사이즈
  • 숫자 열
  • 번호

여기에 현재 코드가 있습니다 :

,210

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

답변

0
RecyclerView 대신의 GridView에서 LayoutManger로 구글에 의해

사용 FlexboxLayoutManger는

Github에서 링크 - https://github.com/google/flexbox-layout

체크 아웃은 README에서 해당 구현을 설명하는 위의 링크