Android中的按钮动画状态更改

时间:2016-03-21 18:03:20

标签: android android-animation

嗨我有一个启用状态和禁用状态的按钮。最初它将处于禁用状态,如下所示

Disabled State

在特定时间我需要启用它,如下所示

Enabled State

我想为此状态更改设置动画

这是我的无效Drawable: -

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@android:color/transparent" />
    <corners android:radius="2dp" />
    <stroke
        android:width="1dp"
        android:color="#FFFFFF" />
</shape>

以下是我的启用状态: -

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#FFFFFF" />
    <corners android:radius="2dp" />
    <stroke
        android:width="1px"
        android:color="#FFFFFF" />
</shape>

如何在事件触发器上设置此状态更改的动画?

1 个答案:

答案 0 :(得分:1)

能够使用TransitionDrawable

实现结果
final Handler handler = new Handler();

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                TransitionDrawable transition = (TransitionDrawable) btnSignIn.getBackground();
                transition.startTransition(1000);
                btnSignIn.setTextColor(Color.BLACK);
            }
        }, 1000);

我的新绘画就像: -

<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- The drawables used here can be solid colors, gradients, shapes, images, etc. -->
    <item android:drawable="@drawable/invalid_button" />
    <item android:drawable="@drawable/valid_button" />
</transition>

其中invalid_button.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@android:color/transparent" />
    <corners android:radius="2dp" />
    <stroke
        android:width="1dp"
        android:color="#FFFFFF" />
</shape>

和valid_button.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#FFFFFF" />
    <corners android:radius="2dp" />
    <stroke
        android:width="1px"
        android:color="#FFFFFF" />
</shape>