ImageButton using transitions on Android

I am trying to create a transparent (no background button) ImageButton that has a custom selector. I have a selector that works against the button, but now I want the selector dividers to intersect each other. I saw a TransitionDrawable object that can be represented in XML. Is there any way to associate this with my selector?

The following is the XML layout code for creating a screen image button in the lower left corner of the screen. Currently, it is moving from one image to the next, sharply ignoring the XML transition.

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
android imagebutton transitions
source share
3 answers

You need to drop the selector and use the transition directly, like ImageButtons drawable. Animation itself should be applied in code.

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

Where drawable.reverseTransition(500) will cancel the transition from the current state.

See Transition Drawable and TransitionDrawable.html#reverseTransition(int) more details.

+6
source share

Using transitions in selectors

 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); } 
+4
source share

the answers above are too complicated try this

 <?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> 
+2
source share

All Articles