【问题标题】:Button Animation in Android on State changeAndroid中状态变化的按钮动画
【发布时间】:2016-07-08 09:36:40
【问题描述】:

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

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

我想为这个状态变化制作动画

这是我的无效可绘制对象:-

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

如何在事件触发器上为这种状态变化设置动画??

【问题讨论】:

  • @0X0nosugar 我需要在文本更改或某些验证时使用它...
  • 我认为第一个答案可能是您正在寻找的:TransitionDrawables。它们可以通过编程方式启动:transition.startTransition(transitionTime);但也许第一个答案中的链接到另一个答案更有帮助。
  • @0X0nosugar 我能够使用 TransitionDrawable 实现结果...已粘贴下面的代码..thanx

标签: android android-animation


【解决方案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);

我的新drawable就像:-

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-22
    • 1970-01-01
    • 2017-12-09
    • 2017-11-24
    • 1970-01-01
    • 2018-01-22
    • 2022-11-08
    相关资源
    最近更新 更多