2012-10-15 31 views
0

我有一個用戶可以通過意圖選擇照片的活動。預期的行爲如下:使用ImageView旋轉Android活動將ImageView縮小爲1px

  • 圖像的大小設置爲填充父寬度(減去邊距)。
  • 這個大小不應該增加圖像,只是在需要時縮小。
  • 寬高比應始終準確。
  • 高度應按照寬度精確縮放以保持正確的高寬比。
  • 縱向模式下,出現滾動之前可以獲得的最大高度(只填滿屏幕)。
  • 這意味着如果限制高度,寬度可能不會完全伸展到父寬度。
  • 在橫向模式下,由於允許滾動,所以最大高度不相關,寬度應始終爲父寬度。

下面是我使用的畫像XML佈局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:orientation="vertical" 
       android:background="@drawable/gray_background" 
       android:weightSum="1" > 

     <!-- Other controls here --> 

     <ImageView android:id="@+id/imgView" 
       android:layout_gravity="center" 
       android:layout_margin="5dip" 
       android:layout_width="fill_parent" 
       android:layout_height="0dip"     
       android:contentDescription="@string/ImageToSend" 
       android:adjustViewBounds="true" 
       android:layout_weight="1" 
       android:scaleType="fitCenter" /> 

     <!-- Other controls here --> 
    </LinearLayout> 

這是我現在用的是景觀XML佈局:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:fillViewport="true"> 
    <!-- Other controls are here -->  
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" 
       android:background="@drawable/gray_background" > 

     <ImageView android:id="@+id/imgView" 
        android:layout_gravity="center" 
        android:layout_margin="5dip" 
        android:layout_width="fill_parent" 
        android:layout_height="0dip"     
        android:contentDescription="@string/ImageToSend" 
        android:adjustViewBounds="true" 
        android:layout_weight="1" 
        android:scaleType="fitCenter" /> 

    <!-- Other controls are here -->  
    </LinearLayout> 
</ScrollView> 

這工作完全正常,無論我在什麼看法。我遇到的問題是當用戶旋轉設備時,ImageView不再具有正確的大小。如果我從橫向模式選擇圖像開始,旋轉看起來很好。如果我以縱向模式選擇圖像開始,然後切換到橫向模式,圖像現在顯示爲1px×1px。

任何想法的人?

回答

1

經過一段時間失敗後,我發現調用setContentView()並重新佈局解決了這個問題。不知道這是我應該做的,但它已經修復了它。