天天看點

在scrollview裡面顯示imageview (一張超大圖檔), scrollview的上下顯示多餘的空白

這是項目中遇到的問題: 在scrollview裡面顯示imageview (一張超大圖檔), scrollview的上下會顯示空白(或者說,顯示多餘的空白,你需要向下拖動一會兒才能看到imageView的内容),這個問題,僅出現在density為120(resolution: 320X240, 400X240,432X240),160( resolution: 480X320)的機器上,而240(resolution: 800X480, 854X480)的卻能正常顯示,上下并沒有多餘的空白。

好奇怪的問題!!!

這樣的問題,同樣存在于當你在xml中設定imageView的背景時,

<ScrollView

style="@style/ScrollViewStyle"

android:layout_marginTop="85dp"

android:layout_marginBottom="110dp">

<ImageView

android:id="@+id/detailView"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:src="@drawable/hayhouse"

/>

</ScrollView>

這樣activity在顯示的時候,也同樣會出現上述留白的問題!

這個問題的解決方法是:

不要在布局檔案中設定src,改在程式中設定就沒有問題了,detailView.setBackgroundResource(R.drawable.hayhouse);

但是,請注意,用這種方式來設定一張很大的圖檔(大約500kb)時,會出現另一個令人頭疼的問題---OOM ,前面有一篇部落格,就是講的這個問題,解決方法如下:

Bitmap pdfImage = ReadBitmapUtil.readBitMap(this, R.drawable.more_info);

detailView.setImageBitmap(pdfImage);

後來,發現自己對一些東西還是沒有了解.....

新的解決方法1,在ImageView配置中加 android:adjustViewBounds="true"就OK了!

<nobr><em>Attribute Name</em></nobr> <nobr><em>Related Method</em></nobr> <nobr><em>Description</em></nobr>
android:adjustViewBounds setAdjustViewBounds(boolean) Set this to true if you want the ImageView to adjust its bounds to preserve the aspect ratio of its drawable.

或者可以使用方法2: 為三種密度,分别設定圖檔資源,分别放在drawable-ldpi, drawable-hdpi,drawable-mdpi檔案夾下,這樣就可以了!

注: BitmapFactory.decodeStream(is,null,opt);方法是直接讀取位元組碼,它并不會根據機器的各種分辨率來自動适應。

120像素/英寸 ( ldpi) 《===》 47.24 像素/厘米

160像素/英寸 ( ldpi) 《===》 62.99像素/厘米

240像素/英寸 (hdpi) 《===》 94.49像素/厘米