6

カスタムセレクターを持つ透明な (ボタンの背景なし) 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>

活動.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>
4

3 に答える 3

6

セレクターをドロップし、トランジションをImageButtonsドローアブルとして直接使用する必要があります。アニメーション自体はコードで適用する必要があります

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

どこでdrawable.reverseTransition(500)現在の状態からの遷移を逆にします。

トランジションドローアブルおよび詳細な説明も参照してくださいTransitionDrawable.html#reverseTransition(int)

于 2010-09-13T10:43:52.693 に答える
4

セレクターでトランジションを使用する

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);
        }
于 2011-02-19T19:36:57.540 に答える