Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android中使用转换的ImageButton_Android_Imagebutton_Transitions - Fatal编程技术网

Android中使用转换的ImageButton

Android中使用转换的ImageButton,android,imagebutton,transitions,Android,Imagebutton,Transitions,我正在尝试创建一个透明(无按钮背景)的ImageButton,它有一个自定义选择器。我让选择器对着按钮工作,但我现在想让选择器的可抽片相互交叉淡入。 我看到了可以用XML表示的TransitionDrawable对象。有没有办法将其连接到我的选择器 下面是在屏幕左下角的屏幕上创建图像按钮的XML布局代码。它当前从一个图像转到下一个图像,突然忽略了转换XML 选择器按钮.xml <?xml version="1.0" encoding="utf-8"?> <selector xm

我正在尝试创建一个透明(无按钮背景)的ImageButton,它有一个自定义选择器。我让选择器对着按钮工作,但我现在想让选择器的可抽片相互交叉淡入。 我看到了可以用XML表示的TransitionDrawable对象。有没有办法将其连接到我的选择器

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

选择器按钮.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>
<?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>
<?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>

将\u normal\u转换为\u pressed.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>
<?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>
<?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>

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

您需要放下选择器,直接使用转换按钮作为可绘制的图像按钮。动画本身必须在代码中应用

ImageButton button = (ImageButton) findViewById(R.id.button);
TransitionDrawable drawable = (TransitionDrawable) button.getDrawable();
drawable.startTransition(500);
其中
可绘制。反转转换(500)
将从当前状态反转转换


有关进一步的说明,请参见和
TransitionDrawable.html#reverseTransition(int)

在选择器中使用转换

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

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

<?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级及更高级别中使用