Android - flip image in xml

Android

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

Use the scale attributes in ImageView

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

Solution 2 - Android

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

Solution 3 - Android

I resolve my problem by using layer-list:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item >
        <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
            <item>
                <shape android:shape="rectangle">
                    <gradient android:startColor="#9f9" android:centerColor="#000"
                        android:endColor="#0f0" android:angle="-90" />
                    <stroke android:width="1.0px" android:color="#444" />
                    <corners android:bottomRightRadius="7dip"
                        android:bottomLeftRadius="0.1dp"
                        android:topLeftRadius="0.1dp"
                        android:topRightRadius="7dip"/>
                </shape>
            </item>
            <item>
                <rotate android:fromDegrees="180.0" android:toDegrees="180.0"
                    android:pivotX="50%" android:pivotY="50%" android:drawable="@drawable/arrow_right" />
            </item>
        </layer-list>
    </item>
</selector>

Solution 4 - Android

Answer by @lewis-mcgeary link - original answer link :https://stackoverflow.com/a/43783080/4075178 is the easiest and self-explanatory.

This code is for fliping horizontally.

 <vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportHeight="24"
android:viewportWidth="24">

<group
    android:name="rotationGroup"
    android:pivotX="12"
    android:scaleX="-1" >

    <path
        android:fillColor="#FFFFFF"
        android:pathData="M15,14C17.67,14 23,15.33 23,18V20H7V18C7,15.33 12.33,14 15,14M15,12A4,4 0 0,1 11,8A4,4 0 0,1 15,4A4,4 0 0,1 19,8A4,4 0 0,1 15,12M5,9.59L7.12,7.46L8.54,8.88L6.41,11L8.54,13.12L7.12,14.54L5,12.41L2.88,14.54L1.46,13.12L3.59,11L1.46,8.88L2.88,7.46L5,9.59Z" />
</group>

If you need to flip vertically, use

<group android:pivotY ="half of viewPortHeight"
android:ScaleY =" -1">

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNoleshView Question on Stackoverflow
Solution 1 - AndroidTommaso RestiView Answer on Stackoverflow
Solution 2 - Androidandroid developerView Answer on Stackoverflow
Solution 3 - AndroidNoleshView Answer on Stackoverflow
Solution 4 - AndroidVikas PandeyView Answer on Stackoverflow