3

I have a layout with a ImageView that I want to keep similar in landscape and portrait orientations, such as this figure:

Portrait and landscape layouts

I'm using two similar layouts (layout and layout-land). In the second layout, I add the android:rotation parameter such in code below, but it resulted in this figure:

Landscape

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

Is it possible to rotate the image view using only xml layout file? How could I do it?

msampaio
  • 3,394
  • 6
  • 33
  • 53
  • The image itself will have orientation info embedded, so you don't need the android:rotation. Images should come in rotated properly. The layout should control the aspect ratio relative to the screen – Martin Aug 11 '15 at 21:11

2 Answers2

2

Try adding android:configChanges="orientation|screenSize" to your activities xml in your AndroidManifest.xml

Then in your Fragment/Activity, whatever contains the ImageView, override public void onConfigurationChanged(Configuration newConfig) and in there you can check if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) and then you could rotate your ImageView programmatically image.setRotation(90) Note that setRotation requires API 11 or higher. Otherwise refer to this answer for how to rotate programmatically: https://stackoverflow.com/a/10104318/4898635

For more information on handling runtime changes like orientation, check this out: http://developer.android.com/guide/topics/resources/runtime-changes.html

Community
  • 1
  • 1
Dom
  • 8,222
  • 2
  • 15
  • 19
0

Instead of using

android:src = "@drawable/background_agogo"

use

android:background="@drawable/background_agogo"

The image should stretch to fit the imageview when using the background attribute.

ginsengtang
  • 751
  • 1
  • 7
  • 17