Android 中使用 Transitions 的 ImageButton

发布于 2024-09-15 13:02:58 字数 1777 浏览 7 评论 0原文

我正在尝试创建一个具有自定义选择器的透明(无按钮背景)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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月竹挽风 2024-09-22 13:02:58

您需要删除选择器并直接使用过渡作为 ImageButtons 可绘制对象。动画本身必须在代码中应用

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

,其中 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

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

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.

自由范儿 2024-09-22 13:02:58

在选择器中使用转换

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);
        }

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);
        }
扮仙女 2024-09-22 13:02:58

上面的答案太复杂了,试试这个

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

answers above is too complex, 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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文