Android 中使用 Transitions 的 ImageButton
我正在尝试创建一个具有自定义选择器的透明(无按钮背景)ImageButton。我让选择器针对按钮工作,但现在我希望选择器可绘制对象彼此交叉淡入淡出。 我看到了可以用 XML 表示的 TransitionDrawable 对象。有没有办法将其连接到我的选择器?
下面是在屏幕左下角创建图像按钮的 XML 布局代码。目前,它从一个图像转到下一个图像,突然忽略转换 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>
I'm trying to create a transparent (no button background) ImageButton that has a custom selector. I have the selector working against the button but I now want the selector drawables to cross-fade into each other.
I saw the TransitionDrawable object that can be represented in XML. Is there a way to connect this into my selector?
Below is the XML layout code to create the image button on the screen in the lower left corner of the screen. It currently goes from one image to the next abruptly ignoring the transition 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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要删除选择器并直接使用过渡作为 ImageButtons 可绘制对象。动画本身必须在代码中应用
,其中
drawable.reverseTransition(500)
将从当前状态反转转换。请参阅 Transition Drawable 以及
TransitionDrawable.html #reverseTransition(int)
以获得进一步的解释。You need to drop the selector and use the transition directly as the ImageButtons drawable. The animation itself must be applied in code
Where
drawable.reverseTransition(500)
will reverse the transition from its current state.See Transition Drawable and also
TransitionDrawable.html#reverseTransition(int)
for a further explanation.在选择器中使用转换
Using transitions in selectors
上面的答案太复杂了,试试这个
answers above is too complex, try this