2016-05-11 21 views
5

Tôi đang cố gắng vừa với hình ảnh vào chế độ xem lưới. Nhưng tôi không hiểu từ đâu GridView của tôi là nhận được chiều cao của xem.Kết hợp hình ảnh vào chế độ xem lưới

Ô của chế độ xem lưới nhỏ hơn hình ảnh. Tôi muốn hiển thị hình ảnh đầy trong một ô. Nhưng tôi không muốn mã hóa cứng kích thước của ImageView.

mã My

GridView trong xml

<GridView 
    android:id="@+id/gridview_movie_list" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_gravity="center" 
    android:numColumns="auto_fit" 
    android:columnWidth="185dp" 
    android:stretchMode="columnWidth"/> 

ImageView tôi mà tôi đang lạm phát.

<ImageView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:scaleType="centerCrop"/> 

Và phương pháp getView của tôi trong lớp adapter của tôi

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

    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
      mContext.LAYOUT_INFLATER_SERVICE); 
    ImageView imageView; 

    if (convertView == null) { 
     imageView = (ImageView) inflater.inflate(R.layout.grid_view_item, null); 
    } else { 
     imageView = (ImageView) convertView; 
    } 

    Picasso.with(mContext).setLoggingEnabled(false); 
    Picasso.with(mContext) 
      .load(imageResource[position]) 
      .into(imageView); 

    return imageView; 
} 

Đây là những gì tôi nhận được

This is what I get

Và đây là những gì được yêu cầu

enter image description here

.210

Trả lời

5

Điều gì đã giúp tôi thêm dòng này vào chế độ xem hình ảnh của tôi xml.

android:adjustViewBounds="true" 

Bây giờ đầu ra của tôi giống như những gì tôi muốn. Thanks for the help mỗi một

0

Bạn đã cố gắng, trong phương pháp getView override để thêm các thông số:

imageView.setScaleType(ImageView.ScaleType.CENTER); // Replace default CENTER_CROP 
// Try ScaleType.CENTER or ScaleType.CENTER_INSIDE 
imageView.setPadding(8, 8, 8, 8); // why not be explicit about the padding while we're at it 

docs Tốt ol' sẽ giúp quá:

+0

Không đoạn mã này không giúp. Điều này có nghĩa là nó rescales hình ảnh để phù hợp với những tế bào. Những gì tôi muốn thực sự là hình ảnh nên điền vào các tế bào. –

+0

Có thể thử 'ScaleType.FIT_CENTER'? – Nevermore

0

Thử fitXy trên ImageView nó sẽ cố gắng để phù hợp với các cạnh mà không làm giảm pixel

<ImageView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:scaleType="fitXY"/> 
+0

Không có đoạn mã nào không hữu ích. Điều này có nghĩa là nó rescales hình ảnh để điền vào các tế bào. Nhưng kích thước tế bào giống như những gì tôi đã nhận được cho ra đặt trước đó. –

+0

Tôi thấy, xin lỗi về câu trả lời sai lầm. Tôi nghĩ rằng bạn có thể sử dụng adapter để điền tất cả các hình ảnh vào GridView của bạn. Vì vậy, bạn sẽ kết thúc với 2 xml một xml là chế độ xem lưới của bạn và chế độ xem khác cho chế độ xem của bạn trên lưới. vì vậy bạn chỉ cần tạo bộ điều hợp cho chế độ xem lưới của mình. Tôi sẽ gửi thêm 1 câu trả lời cho bạn. –

0

Đây là GridView của bạn thêm vào gridview.xml

<GridView 
    android:id="@+id/clueGrid" 
    android:layout_width="280dp" 
    android:layout_height="match_parent" 
    android:layout_below="@id/gameInstructions" 
    android:layout_centerHorizontal="true" 
    android:horizontalSpacing="20dp" 
    android:verticalSpacing="20dp" 
    android:paddingTop="10dp" 
    android:numColumns="2" 
    /> 

thêm video này vào xml khác -> grid_view_image.xml

<com.XXXX.view.SquareRelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:background="@drawable/grid_item_background" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

<ImageView 
    android:layout_width="40dp" 
    android:layout_height="40dp" 
    android:src="@drawable/clue_found" 
    android:layout_alignParentRight="true" 
    android:layout_marginRight="10dp" 
    android:layout_marginBottom="10dp" 
    android:layout_alignParentBottom="true" 
    android:id="@+id/itemView"/> 

</com.XXXX.view.SquareRelativeLayout> 

và rằng việc sử dụng xml SquareRelativeLayout thêm Java này

public class SquareRelativeLayout extends RelativeLayout { 

    public SquareRelativeLayout(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){ 
     super.onMeasure(widthMeasureSpec, widthMeasureSpec); 
    } 
} 

Sau đó, bây giờ bạn cần t o tạo Adaptor của bạn

public class ItemGridAdapter kéo dài BaseAdapter {

List<Item> itemList; 

public ItemGridAdapter(List<Item> itemList){ 
    this.itemList = itemList; 
} 

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

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

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

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    if (convertView == null){ 
     LayoutInflater inflater = LayoutInflater.from(parent.getContext()); 
     convertView = inflater.inflate(R.layout.grid_view_image, parent, false); 
    } 
    final ImageView itemView = (ImageView) convertView.findViewById(R.id.itemView); 
    itemView.setImageDrawable("YOUR DRAWABLE"); 
    return convertView; 
} 
} 

Sau đó, trên lớp học chính của bạn có sử dụng GridView.xml

ItemGridAdapter itemGridAdapter = new ItemGridAdapter(itemList); 
itemGrid.setAdapter(itemGridAdapter); 

Sau đó, bạn có thể chỉ phát xung quanh ImageView và bố cục của mình. HopeFully điều này sẽ giúp !!

tôi thấy bạn sử dụng Picasso đây là câu trả lời của tôi cho adapter thiết lập hình ảnh:

Target target = new Target() { 
      @Override 
      public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { 
       itemView.setImageBitmap(bitmap); 
      } 

      @Override 
      public void onBitmapFailed(Drawable errorDrawable) { 
       // Meh 
      } 

      @Override 
      public void onPrepareLoad(Drawable placeHolderDrawable) { 
       // Meh 
      } 
     }; 

    Picasso.with(convertView.getContext()) 
       .load(imageUrl) 
       .into(target); 
Các vấn đề liên quan