Android - flip image in xml
I want to flip image for button's background in xml. I've seen example how to do it, but it was programmatically way: http://xjaphx.wordpress.com/2011/06/26/image-processing-image-flipping-mirroring. Anyway, I have a xml file (button_left_state.xml) like below :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<rotate android:fromDegrees="180.0" android:toDegrees="180.0"
android:pivotX="50%" android:pivotY="50%" android:drawable="@drawable/buttonrightpressed" />
</item>
<item>
<rotate android:fromDegrees="180.0" android:toDegrees="0.0"
android:pivotX="50%" android:pivotY="50%" android:drawable="@drawable/buttonright"/>
</item>
</selector>
But this code just rotate image to 180 degrees. Is it possible to 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:rotationX="180"
This will flip the imageView horizontally (left<->right).
For vertically, put this:
android:rotationY="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) :