2014-10-05 24 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); 
    }