2010-08-25 13 views
6

में संक्रमण का उपयोग कर छविबटन मैं एक पारदर्शी (कोई बटन पृष्ठभूमि) छवि बटन बनाने की कोशिश कर रहा हूं जिसमें एक कस्टम चयनकर्ता है। मेरे पास चयनकर्ता बटन के खिलाफ काम कर रहा है लेकिन अब मैं चयनकर्ता ड्रॉबल्स को एक दूसरे में पार करने के लिए चाहता हूं। मैंने ट्रांजिशन ड्रॉबल ऑब्जेक्ट देखा जिसे एक्सएमएल में प्रदर्शित किया जा सकता है। क्या मेरे चयनकर्ता में इसे जोड़ने का कोई तरीका है?एंड्रॉइड

नीचे स्क्रीन के निचले बाएं कोने में स्क्रीन पर छवि बटन बनाने के लिए XML लेआउट कोड है। यह वर्तमान में संक्रमण छवि एक्सएमएल को अनदेखा कर एक छवि से अगले में जाता है।

selector_button.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" android:drawable="@drawable/transition_normal_to_pressed" /> <!-- pressed --> 
    <item android:state_focused="true" android:drawable="@drawable/transition_pressed_to_normal" /> <!-- focused --> 
    <item android:drawable="@drawable/menu_normal" /> <!-- default --> 
</selector> 

transition_normal_to_pressed.xml

<?xml version="1.0" encoding="utf-8"?> 
<transition xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@drawable/menu_pressed" /> 
    <item android:drawable="@drawable/menu_normal" /> 
</transition> 

activity.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <ImageButton 
     android:id="@+id/btnMenu" 
     android:background="@android:color/transparent" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/selector_button" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentBottom="true" /> 
</RelativeLayout> 

उत्तर

6

आपको चयनकर्ता को छोड़ने और छविबटन को खींचने योग्य रूप से सीधे संक्रमण का उपयोग करने की आवश्यकता है। कहाँ drawable.reverseTransition(500) अपनी वर्तमान स्थिति से संक्रमण रिवर्स जाएगा एनीमेशन ही कोड

ImageButton button = (ImageButton) findViewById(R.id.button); 
TransitionDrawable drawable = (TransitionDrawable) button.getDrawable(); 
drawable.startTransition(500); 

में लागू किया जाना चाहिए।

एक और स्पष्टीकरण के लिए Transition Drawable और TransitionDrawable.html#reverseTransition(int) देखें। चयनकर्ताओं में

4

का प्रयोग संक्रमण

ImageButton button = (ImageButton) findViewById(R.id.button); 
Drawable d = button.getDrawable.getCurrent(); //or AnyView.getBackground().getCurrent(); for custom background 
     if (d instanceof TransitionDrawable) { 
      TransitionDrawable t = (TransitionDrawable) d; 
      t.startTransition(500); 
     } 
2

जवाब ऊपर बहुत जटिल है, इस

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android" 
android:exitFadeDuration="240" 
android:enterFadeDuration="120" > 
<item android:state_pressed="true" 
    android:drawable="@color/pressed_bg" /> 
<item android:state_pressed="false" 
    android:drawable="@android:color/transparent" /> 
</selector> 
+0

विशेषताएं "exitFadeDuration" और कोशिश "enterFadeDuration" केवल API स्तर में किया जाता है 11 और उच्च –