Android动画副作用:按钮在设置颜色动画时更改其形状

Android动画副作用:按钮在设置颜色动画时更改其形状,android,Android,我在下面发布的所有代码都像我想要的那样工作,只是作为参考,也许还有人想实现类似的目标。它很简单,评论也很好 因此,我正在开发一个应用程序,在该应用程序中,我可以根据如下定义的xml可绘制资源动态创建几个圆形按钮: <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >

我在下面发布的所有代码都像我想要的那样工作,只是作为参考,也许还有人想实现类似的目标。它很简单,评论也很好

因此,我正在开发一个应用程序,在该应用程序中,我可以根据如下定义的xml可绘制资源动态创建几个圆形按钮:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners
    android:radius="25dp"
    />
<solid
    android:color="#0000FF"
    />
<padding
    android:left="0dp"
    android:top="0dp"
    android:right="0dp"
    android:bottom="0dp"
    />
<size
    android:width="50dp"
    android:height="50dp"
    />
</shape>
然后我想对这些按钮应用动画。动画只是通过几次更改按钮的颜色(蓝色->黄色->蓝色->蓝色…)使按钮闪烁 这个动画效果也很好(符合我的要求)

按钮会像我想要的那样闪烁,除了当我调用
changeButtonColor()
时,我的按钮会将其形状更改为正方形

上面的动画似乎覆盖了
setBackgroundResource()
所做的,但是,我试图通过在动画中放置
setBackgroundResource()
来防止这种情况,但没有结果


有没有关于如何消除这种副作用的建议?

实际上,我不知道这种行为的原因,但我已经实施了一种解决方法。我正在更改背景资源,而不是设置颜色更改动画

因此,我定义了另一个资源,如蓝色按钮,但颜色为黄色:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners
    android:radius="25dp"
    />
<solid
    android:color="#FFFF00"
    />
<padding
    android:left="0dp"
    android:top="0dp"
    android:right="0dp"
    android:bottom="0dp"
    />
<size
    android:width="50dp"
    android:height="50dp"
    />
</shape>

无论如何谢谢你的帮助

事实上,我不知道这种行为的原因,但我已经实施了一种解决方法。我正在更改背景资源,而不是设置颜色更改动画

因此,我定义了另一个资源,如蓝色按钮,但颜色为黄色:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners
    android:radius="25dp"
    />
<solid
    android:color="#FFFF00"
    />
<padding
    android:left="0dp"
    android:top="0dp"
    android:right="0dp"
    android:bottom="0dp"
    />
<size
    android:width="50dp"
    android:height="50dp"
    />
</shape>
无论如何谢谢你的帮助

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners
    android:radius="25dp"
    />
<solid
    android:color="#FFFF00"
    />
<padding
    android:left="0dp"
    android:top="0dp"
    android:right="0dp"
    android:bottom="0dp"
    />
<size
    android:width="50dp"
    android:height="50dp"
    />
</shape>
private void changeButtonColor(final Button button){

    int animationTime = 800;

    int yellowResource = R.drawable.buttonshape_yellow;
    int blueResource = R.drawable.buttonshape;

    ValueAnimator anim = new ValueAnimator();
    anim.setIntValues(blueResource, yellowResource, blueResource, yellowResource, blueResource);
    anim.setEvaluator(new ArgbEvaluator());

    anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator valueAnimator) {
      button.setBackgroundResource((Integer)valueAnimator.getAnimatedValue());
        }
    });
    anim.setInterpolator(new LinearInterpolator());
    anim.setDuration(animationTime);
    anim.start();
}