简体   繁体   English

从纵向/横向旋转设备时,如何使ImageView旋转图像?

[英]How can I make an ImageView rotate the image when the device is rotated from portrait/landscape?

Let's say you have an image of a skyscraper in an ImageView. 假设您在ImageView中有摩天大楼的图像。 In portrait mode, the skyscraper is pointing upwards like normal. 在纵向模式下,摩天大楼像往常一样指向上方。

By default, if you rotate the device landscape, the skyscraper will still be pointing upwards. 默认情况下,如果旋转设备景观,摩天大楼将仍然指向上方。

My desired result is for the skyscraper to follow the orientation of the device, so it would be pointing sideways. 我希望得到的结果是摩天大楼遵循设备的方向,因此它将指向侧面。

I cannot for the life of me find a solution to this. 我一生无法找到解决方案。

Basically, I have a full screen background image that looks good no matter how it's rotated. 基本上,我有一个全屏背景图像,无论旋转方式如何,它看起来都不错。 I want to use scaleType centerCrop so it fills the screen, and because the image doesn't rotate, it "zooms in" so to speak in order to fill the landscape screen with a portrait image. 我想使用scaleType centerCrop使其充满整个屏幕,并且由于图像不旋转,因此可以“放大”以便用纵向图像填充横向屏幕。

在此处输入图片说明

Try to rotate your image on configuration changed : 尝试在更改的配置上旋转图像:

 @Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
    }
}

You might have to add these lines to your AndroidManifest.xml to tell the system you are handling configuration changes yourself : 您可能必须将这些行添加到AndroidManifest.xml中,以告诉您自己正在处理配置更改的系统:

<activity
android:name="MainActivity"
android:configChanges="keyboardHidden|orientation|screenSize">

在AndroidManifest.xml的活动下添加android:screenOrientation =“ portrait”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM