0

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

cowsay
  • 1,282
  • 1
  • 15
  • 36
  • If that is actually all you want, then you can just lock the screen orientation (but I am guessing you want to do more than that). – Suragch Apr 05 '15 at 23:57

2 Answers2

3

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">
  • As said above. To rotate your image use [this answer.](http://stackoverflow.com/a/10104318/3455196) – Siri0S Apr 06 '15 at 04:45
0

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

Ananth
  • 1,065
  • 1
  • 12
  • 20