2012-05-31 150 views
1

我有這個列表視圖,其中圖像視圖不適合屏幕寬度,即使位圖的大小大於屏幕大小。下面是結果我得到:Android列表視圖圖像不適合屏幕寬度

(由於垃圾郵件的政策,我不能在這裏後我的截圖,請點擊下面的鏈接查看截圖。) http://www.sundancepost.com/ivue/screen/screen1.jpg

如圖中紅色框指示屏幕上方,該列表不適合屏幕的寬度。

以下是我想要的結果:

http://www.sundancepost.com/ivue/screen/screen2.jpg

這裏是我的佈局代碼。

featured_listrow.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical">  

<ImageView 
    android:id="@+id/banner"   
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content" 
    android:adjustViewBounds="true" 
    android:src="@drawable/ampersand_banner"/> 

</RelativeLayout> 

featured_list:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<ListView 
    android:id="@+id/android:list" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:smoothScrollbar="true" 
    > 

</ListView> 

</LinearLayout> 

我相信我已經正確設置layout_width和layout_height兩者的佈局文件。我認爲這與運行時設置的android:adjustViewBounds有關。有人能幫幫我嗎?

+0

'android:scaleType =「fitXY」' –

+0

不幸的是,結果仍然是一樣的。即使當我調整圖像大小以適合屏幕寬度時,也會發生這種情況。 –

+0

嘗試玩scaleType的值。另外,請顯示listVIew的適配器的代碼。我不認爲xml有什麼問題。 –

回答

2

我終於明白了問題所在。圖像的縮放發生在這ImageLoader.classhttp://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/

所以,當我註釋掉了代碼的縮放部分,這一切都恢復正常。

這是代碼:

private Bitmap decodeFile(File f){ 
    try { 
     //decode image size 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 
     BitmapFactory.decodeStream(new FileInputStream(f),null,o); 

     //Find the correct scale value. It should be the power of 2. 
     // final int REQUIRED_SIZE=100; 
     // int width_tmp=o.outWidth, height_tmp=o.outHeight; 
     int scale=1; 
     //while(true){ 
     //if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE) 
     //break; 
     //width_tmp/=2; 
     //height_tmp/=2; 
     //scale*=2; 
     //} 

     //decode with inSampleSize 
     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize=scale; 
     return BitmapFactory.decodeStream(new FileInputStream(f), null, o2); 
    } catch (FileNotFoundException e) {} 
    return null; 
} 
0

我認爲唯一可以幫助的是使用其他程序調整圖像大小。問題在於圖像不能被裁剪,只能在xml或代碼中設置圖像大小時進行水平或垂直兩側的編程。

+0

我沒有通過photoshop調整圖像大小。我的圖片的當前寬度是480px,這應該很適合我的android設備。但是,正如它發生的那樣,即使使用正確的圖像尺寸,圖像仍然不適合屏幕。 –

+0

有趣的是,當我將featured_listrow.xml切換到圖形佈局時,圖像看起來非常適合屏幕的寬度。但是,它不會在運行時發生。 –

1

您可以設置圖像在圖像中任一視圖中使用

1)android:scaleType="fitXY"

2)android:scaleType="centerCrop"

在你的圖像視圖中使用任何一個。它讓我完美的結果,因爲我想要的。