动画可绘制的alpha属性

时间:2015-03-02 09:57:19

标签: android android-animation

我想为ViewGroup的背景Drawable的alpha属性设置动画。

我使用view.getBackground()获得了对background的drawable的引用。

然后我使用以下代码(from this thread):

    if (backgroundDrawable.getAlpha() == 0) {
            ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(backgroundDrawable, PropertyValuesHolder.ofInt("alpha", 255));
            animator.setTarget(backgroundDrawable);
            animator.setDuration(2000);
            animator.start();
        } else {
            ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(backgroundDrawable, PropertyValuesHolder.ofInt("alpha", 0));
            animator.setTarget(backgroundDrawable);
            animator.setDuration(2000);
            animator.start();
        }

但是动画总是从alpha值0开始。(意思是,当我想要动画为0时,它会立即消失,因为它会从0动画到0)。

有谁知道如何才能使这项工作?

1 个答案:

答案 0 :(得分:3)

我相信您想要的是为您的动画设置初始值和最终值,而不仅仅是最终值,如下所示:

if (backgroundDrawable.getAlpha() == 0) {
        ObjectAnimator animator = ObjectAnimator
            .ofPropertyValuesHolder(backgroundDrawable, 
                      PropertyValuesHolder.ofInt("alpha", 0, 255));
        animator.setTarget(backgroundDrawable);
        animator.setDuration(2000);
        animator.start();
    } else {
        ObjectAnimator animator = ObjectAnimator
             .ofPropertyValuesHolder(backgroundDrawable, 
                       PropertyValuesHolder.ofInt("alpha", 255, 0));
        animator.setTarget(backgroundDrawable);
        animator.setDuration(2000);
        animator.start();
    }

或者,从使用drawable.getAlpha()的当前值开始,但该方法仅在API 19 = /

开始时可用
相关问题