Android - flip image in xml

Use the scale attributes in ImageView

android:scaleX="-1" //To flip horizontally or
android:scaleY="-1" //To flip vertically

Here's a very short and easy to understand solution.

Add this to the imageView:

 android:rotationY="180"

This will flip the imageView horizontally (left<->right).

For vertically, put this:

android:rotationX="180"

Example:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="original image:"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/test"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="flip horizontally :"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:rotationY="180"
        android:src="@drawable/test"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="flip vertically:"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:rotationX="180"
        android:src="@drawable/test"/>

</LinearLayout>

And the result (image taken from a JNI library that I've made, that can do it via JNI) :

enter image description here

Tags:

Android