动画绘制alpha属性 [英] Animating drawable alpha property
问题描述
我想一个动画的ViewGroup的背景绘制对象的alpha属性。
I want to animate the alpha property of a ViewGroup's background Drawable.
我得到使用view.getBackground()为背景的绘制参考
I get a reference to the background's drawable using view.getBackground().
然后我用下面的code(从这个线程):
Then I use the following code (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)。
But the animation always starts from the alpha value 0. (meaning, when I want to animate to 0, it disappears immediately, because it animates from 0 to 0).
有谁知道我怎样才能使这项工作?
Does anyone know how I can make this work?
推荐答案
我相信ü想要的是你的动画设置的初始值和最终值,而不仅仅是最终值,如:
I believe what u want is to set initial and final values for your animations, and not just the final value, like this:
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 = /
alternatively, starting from the current value using drawable.getAlpha()
, but that method is only available starting on API 19 =/
这篇关于动画绘制alpha属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!