2012-02-18 8 views
38

में छवि फ्लिप करें मैं xml में बटन की पृष्ठभूमि के लिए छवि को फ़्लिप करना चाहता हूं। मैंने उदाहरण देखा है कि यह कैसे करें, लेकिन यह प्रोग्रामेटिक तरीके से था: http://xjaphx.wordpress.com/2011/06/26/image-processing-image-flipping-mirroring। वैसे भी, मैं नीचे की तरह एक xml फ़ाइल (button_left_state.xml) है:एंड्रॉइड - xml

<?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> 

लेकिन इस कोड सिर्फ 180 डिग्री के चित्र को घुमाने के। क्या xml में छवि को फ़्लिप करना संभव है?

+0

क्या आपने मेरे लिंक का प्रयास किया है? – Raman

+0

इस पर कोई भाग्य? मैं एक ही समस्या में भाग रहा हूँ। मेरे पास एक बटन है जो ग्रेडियेंट के साथ एक तीर है। मैंने इसे फ़्लिप किया ताकि तीर दूसरी दिशा का सामना कर रहा हो, जो कि कोड का उपयोग कर काफी अधिक है। हालांकि, अब मेरा ढाल उल्टा है। –

+0

नीचे देखें। मैंने जवाब पोस्ट किया। – Nolesh

उत्तर

8

मैं 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> 

172

उपयोग पैमाने imageView

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

यह अब तक का सबसे सरल, कामकाजी समाधान है। – Louth

+0

मिनट एपीआई 11 :( –

+16

पूर्वावलोकन में काम करने के रूप में नहीं दिख रहा है लेकिन डिवाइस पर काम करता है, धन्यवाद – John

13

में जिम्मेदार बताते हैं यहाँ एक बहुत ही कम और समझने में आसान समाधान है।

imageView को यह करें:

android:rotationX="180" 

यह क्षैतिज imageView फ्लिप जाएगा (बाएं < -> दाएं)।

खड़ी के लिए, यह डाल:

android:rotationY="180" 

उदाहरण:

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

और परिणाम (एक JNI library that I've made से लिया छवि, कि JNI के माध्यम से यह कर सकते हैं):

enter image description here

+2

पर दिखाई देगी, यह केवल सममित छवियों के लिए काम करेगी। घूर्णन छवि को फ़्लिपिंग/मिररिंग के बराबर नहीं है !! – muetzenflo

+2

@muetzenflo स्क्रीनशॉट समेत अद्यतन उत्तर। ध्यान दें कि यह काम करता है –

+0

ठंडा, मुझे एहसास नहीं हुआ कि कोई रोटा सकता है 3 डी अक्ष पर ते। आपके परिश्रम के लिए धन्यवाद – muetzenflo