我有个问题.我想使用objectanimator为LinearLayout的背景颜色设置动画.问题是它动画,但它既不关心持续时间,也不关心valueFrom和valueTo.
这是我的xml文件:
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:propertyName="backgroundColor" android:repeatCount="infinite" android:repeatMode="reverse" android:valueFrom="#FF0000" android:valueTo="#000000" />
在Java中,我称之为:
ObjectAnimator objAnim = (ObjectAnimator)AnimatorInflater.loadAnimator(getActivity(),R.animator.animator_bkg); objAnim.setTarget(view); objAnim.start();
请注意,当我对布局的alpha进行动画处理时,它的工作原理如预期.这是Android的错误(4.0.3在华硕变压器),还是我错过了什么?
解决方法
我google了一下有答案尝试使用TransitionDrawable.
http://developer.android.com/guide/topics/resources/drawable-resource.html#Transition
而且,stackoverflow.com上还有一个专门针对相同问题的主题.
ADDED代码示例:
Button btn = (Button)this.findViewById(R.id.btn1); //Let's change background's color from blue to red. ColorDrawable[] color = {new ColorDrawable(Color.BLUE),new ColorDrawable(Color.RED)}; TransitionDrawable trans = new TransitionDrawable(color); //This will work also on old devices. The latest API says you have to use setBackground instead. btn.setBackgroundDrawable(trans); trans.startTransition(5000);