2010-08-25 10 views
6

Estoy tratando de crear un ImageButton transparente (sin botón de fondo) que tenga un selector personalizado. Tengo el selector trabajando contra el botón, pero ahora quiero que el selector arrastrable se difumine entre sí. Vi el objeto TransitionDrawable que se puede representar en XML. ¿Hay alguna forma de conectar esto a mi selector?ImageButton usando Transiciones en Android

A continuación se muestra el código de diseño XML para crear el botón de imagen en la pantalla en la esquina inferior izquierda de la pantalla. Actualmente va de una imagen a la siguiente ignorando abruptamente el XML de transición.

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> 

Respuesta

6

Debe soltar el selector y usar la transición directamente como drawable de ImageButtons. La animación en sí mismo debe ser aplicado en código

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

Dónde drawable.reverseTransition(500) invertirá la transición de su estado actual.

Ver Transition Drawable y también TransitionDrawable.html#reverseTransition(int) para una explicación más detallada.

4

Uso de transiciones en selectores

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

respuestas arriba es demasiado complejo, probar este

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

Atributos "exitFadeDuration" & "enterFadeDuration" sólo se utilizan en el nivel API 11 y superior –

Cuestiones relacionadas