ImageButton在Android中使用Transitions

前端之家收集整理的这篇文章主要介绍了ImageButton在Android中使用Transitions前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试创建一个具有自定义选择器的透明(无按钮背景) ImageButton.我让选择器对着按钮工作,但我现在希望选择器drawables相互交叉淡入淡出.
我看到了可以用 XML表示的TransitionDrawable对象.有没有办法将它连接到我的选择器?

下面是在屏幕左下角的屏幕上创建图像按钮的XML布局代码.它目前从一个图像到另一个图像突然忽略了转换XML.

selector_button.xml

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

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

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

解决方法

您需要删除选择器并直接使用过渡作为ImageButtons drawable.动画本身必须在代码中应用 @H_404_7@ImageButton button = (ImageButton) findViewById(R.id.button); TransitionDrawable drawable = (TransitionDrawable) button.getDrawable(); drawable.startTransition(500);

drawable.reverseTransition(500)将从当前状态转换为转换.

有关进一步说明,请参阅Transition Drawable以及TransitionDrawable.html#reverseTransition(int).

猜你在找的Android相关文章