cowsay
cowsay

Reputation: 1332

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. 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.

enter image description here

Upvotes: 0

Views: 726

Answers (2)

Ananth
Ananth

Reputation: 1085

Add android:screenOrientation="portrait" under activity of your AndroidManifest.xml.

Upvotes: 0

Guillaume LEFEVRE
Guillaume LEFEVRE

Reputation: 86

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 :

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

Upvotes: 3

Related Questions