Tom11 Tom11 - 15 days ago 7
Android Question

How to forbid BACKGROUND image rotation in Android

I have an activity and I want to keep rotation, however I do not want to rotate background image. Could somebody help me with this one? Here is my code:

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background_test"
tools:context=".Activities.MainActivity">

...

</LinearLayout>


Thanks in advance for your help.

Answer

You can not do this without coding. The best way would be checking the orientation in your activity and rotating the bitmap accordingly. The "quick win" solution would be this:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipChildren="false">

    <ImageView android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:rotation="@integer/rotation"
               android:scaleType="centerCrop"
                android:src="@drawable/background"/>
</FrameLayout>

And override rotation in the ressource folders: <integer name="rotation">90</integer> in values-land and <integer name="rotation">0</integer> in values.