2014-10-05 22 views
0

在不同的Android版本中旋轉內部具有圖像視圖的佈局(例如FrameLayout)時出現奇怪的行爲。Android 4.3之前的佈局旋轉和裁剪行爲

在下面這段XML佈局看看:

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#000000" > 

    <FrameLayout 
     android:layout_width="250dp" 
     android:layout_height="250dp" 
     android:background="#ffffff" 
     android:layout_gravity="center" 
     android:rotation="15" > 

     <ImageView 
      android:id="@+id/imageView1" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:scaleType="centerCrop" 
      android:src="@drawable/bg" /> 

    </FrameLayout> 

</FrameLayout> 

下面將在Android 4.3和4.4,但奇怪的結果顯示在Android 4.2,4.1,4.0.3良好的效果。

在Android 4.2(壞的結果)

enter image description here

在Android 4.3(好成績)

enter image description here

任何人都知道爲什麼出現這種情況到Android 4.3之前,如何解決它?

謝謝!

+0

嘗試切換到從視圖動畫(基於屬性的動畫(http://developer.android.com/guide/topics/graphics/prop-animation.html)HTTP: //developer.android.com/guide/topics/graphics/view-animation.html)。我遇到了一些類似的問題,並通過此開關解決了問題。 – aga 2014-10-05 19:36:30

+0

謝謝,但這不是動畫相關的。它只是一個靜態的佈局,我想旋轉一切。 – 2014-10-06 06:11:52

+0

@TheGreatDescartes你在真實的設備上試過了嗎?可能是一個模擬器問題。你是否還試圖通過代碼設置它,或者實際上拋棄了內部FrameLayout,而只是旋轉im​​ageView(或其內容)?這裏有一些方法來旋轉的東西:http://stackoverflow.com/questions/1930963/rotating-a-view-in-android。順便說一句,關於動畫,從Honeycomb開始,這實際上是動畫的工作原理:旋轉字段的值隨着動畫運行而改變,並且它們都被同步。 – 2014-10-08 11:43:59

回答

0

似乎我無法重現問題,因爲我沒有啓用「使用GPU主機」。

總之,這裏的克服這種方式,即使該功能啓用它:

設置機器人:layerType =「軟件」爲ImageView的。

我希望你不需要動畫,因爲這個屬性會影響性能。

爲了使它工作得更好(至少對於較新的Android版本),我建議製作一個資源,該資源將成爲問題版本(包括)的「軟件」,以及來自其上方的「硬件」。

我試過其他方法來處理它,但它們不能很好地工作。

您可能可以在運行時重置layerType,因此畢竟它可以很好地工作。 對我來說,它的工作。

只要致電:

@Override 
    protected void onCreate(final Bundle savedInstanceState) 
    { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    findViewById(R.id.imageView1).setLayerType(View.LAYER_TYPE_HARDWARE,null); 
    }