Android - Flip Image In Xml


Answer :

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) :

enter image description here


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> 

Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android SDK Location Should Not Contain Whitespace, As This Cause Problems With NDK Tools