msampaio
msampaio

Reputation: 3443

How to rotate a imageView in portrait / landscape changing

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?

Upvotes: 3

Views: 5418

Answers (2)

Dom
Dom

Reputation: 8372

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

Upvotes: 2

ginsengtang
ginsengtang

Reputation: 751

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.

Upvotes: 0

Related Questions