如何在纵向/横向更改时旋转imageView

3

我有一个布局,其中包含一个 ImageView,我希望在横屏和竖屏方向上保持类似的布局,如下图所示:

Portrait and landscape layouts

我正在使用两个类似的布局(layout和layout-land)。在第二个布局中,我添加了android:rotation参数,如下面的代码所示,但结果如下图所示:

Landscape

<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:rotation="90"
    android:scaleType="fitXY"
    android:src="@drawable/background_agogo" />

是否可能仅使用xml布局文件旋转图像视图?我该怎么做?


图像本身将嵌入方向信息,因此您不需要使用android:rotation。 图像应正确旋转。 布局应控制相对于屏幕的纵横比。 - Martin
2个回答

2
尝试在您的AndroidManifest.xml中的活动xml中添加android:configChanges="orientation|screenSize"。然后在包含ImageView的Fragment/Activity中,重写public void onConfigurationChanged(Configuration newConfig),并在其中检查if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE),然后您可以编程方式旋转您的ImageView image.setRotation(90)。请注意,setRotation需要API 11或更高版本。否则,请参考此答案以了解如何以编程方式旋转:https://dev59.com/gWox5IYBdhLWcg3ww3GV#10104318。有关处理运行时更改(如方向)的更多信息,请查看:http://developer.android.com/guide/topics/resources/runtime-changes.html

0

不要使用

android:src = "@drawable/background_agogo"

使用

android:background="@drawable/background_agogo"

使用背景属性时,图像应该拉伸以适应imageview。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接