【问题标题】:ImageButton using Transitions in AndroidAndroid中使用Transitions的ImageButton
【发布时间】:2010-08-25 06:13:53
【问题描述】:

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

【问题讨论】:

    标签: android imagebutton transitions


    【解决方案1】:

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

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

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

    请参阅Transition DrawableTransitionDrawable.html#reverseTransition(int) 以获得进一步的解释。

    【讨论】:

      【解决方案2】:

      在选择器中使用过渡

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

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

        • 属性 "exitFadeDuration" 和 "enterFadeDuration" 仅在 API 级别 11 及更高级别中使用
        猜你喜欢
        • 2017-09-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-13
        • 1970-01-01
        • 2016-06-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多