2012-06-05 31 views
0

我有以下GridView定義w/stretchMode設置爲「columnWidth」。Android GridView拉伸模式與異步ImageView加載

<GridView android:id="@+id/grid" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:columnWidth="160dp" 
    android:numColumns="auto_fit" 
    android:verticalSpacing="10dp" 
    android:horizontalSpacing="10dp" 
    android:stretchMode="columnWidth" 
    android:listSelector="@drawable/list_selector" 
/> 

GridView中的每個項目結構如下。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:padding="2dp" 
> 
    <FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
    > 
     <ImageView android:id="@+id/videoGridItemImage" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:adjustViewBounds="true" 
     /> 
     <TextView android:id="@+id/videoGridItemDuration" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"  
      android:layout_gravity="bottom"  
      android:background="#88000000" 
      android:textColor="#F1F1F1" 
      android:textAppearance="@android:style/TextAppearance.Small" 
      android:padding="2dp" 
     /> 
    </FrameLayout> 
    <TextView android:id="@+id/videoGridItemTitle" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="left" 
     android:textAppearance="@android:style/TextAppearance.Medium" 
     android:textColor="#F1F1F1" 
     android:maxLines="1" 
     android:background="#88000000" 
     android:paddingLeft="2dp" 
     android:paddingRight="2dp" 
    /> 
    <TextView android:id="@+id/videoGridItemSubtitle" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="left" 
     android:textStyle="italic" 
     android:textColor="#666" 
     android:textAppearance="@android:style/TextAppearance.Small" 
     android:maxLines="1" 
     android:background="#88000000" 
     android:paddingLeft="2dp" 
     android:paddingRight="2dp" 
    /> 
</LinearLayout> 

在GridView的適配器中,我通過將ImageView的URL和引用傳遞給專用類來異步加載並顯示圖像。該類將圖像下載到位圖中,然後使用setImageBitmap()更新ImageView。此工作流程按預期運行;但是,我的圖像並未填充ImageView的整個寬度。在左側/右側有少量的paddding。我意識到我可以使用scaleType =「fitXY」,但這並不理想,因爲它會歪斜圖像。使用stretchMode =「columnWidth」將ImageView的src屬性設置爲本地圖像可以很好地縮放。我的猜測是,ImageView縮放到原始columnWidth而不是拉伸/動態寬度。有沒有人有任何見解,爲什麼這是發生和/或如何解決?在此先感謝您的幫助。

回答

0

你有兩個選擇:

  1. 設置你的ImageView layout_width爲wrap_content。 ImageView將根據圖像的大小調整大小,並且您的列將相應進行調整。

  2. 將您的ImageView的scaleType設置爲centerCrop。這將嘗試將圖像放入圖像視圖中,而不會使圖像傾斜,並且只會剪掉任何不適合的多餘圖像。這不會留下空白。

+0

1。將ImageView的layout_width設置爲wrap_content只需將所有填充放在右側 - 左側一半,右側一半。 ImageView仍然不會根據需要填充整列的寬度。 – ajporterfield

+0

2.將ImageView的scaleType設置爲centerCrop可以修復由fitXY引起的歪斜問題;然而,它也會造成不理想的形象。 – ajporterfield

+0

我很確定問題出在stretchMode =「columnWidth」。如果我爲stetchMode使用任何其他值(包括「none」),圖像將按預期顯示 - 不裁剪/傾斜並填充整個列寬。 – ajporterfield

0

我有同樣的問題。我的解決方案是重寫ImageView(在我的情況下)FrameLayout以在圖像下載之前設置它的高度。我想在我的GridView強制廣場磚,並完成像這樣:

public class SquareFrameLayout extends FrameLayout { 

    public SquareFrameLayout(Context context) { 
    super(context); 
    } 
    public SquareFrameLayout(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    } 
    public SquareFrameLayout(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    } 

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

每個GridView的項目是這樣的:

<com.my.client.ui.layout.SquareFrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/discovery_tile" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <ImageView 
    android:id="@+id/my_image" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:scaleType="centerCrop" 
    android:cropToPadding="true" 
    android:contentDescription="@string/my_desc" /> 

</com.my.client.ui.layout.SquareFrameLayout> 

然後在GridView我用這個:

<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:name="com.my.client.ui.MyFragment" 
    android:id="@+id/my_gridview" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:numColumns="3" 
    android:verticalSpacing="0px" 
    android:horizontalSpacing="0px" 
    android:stretchMode="columnWidth" 
    android:gravity="fill" />