Android objectAnimator动画背景颜色布局

前端之家收集整理的这篇文章主要介绍了Android objectAnimator动画背景颜色布局前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有个问题.我想使用objectanimator为LinearLayout的背景颜色设置动画.问题是它动画,但它既不关心持续时间,也不关心valueFrom和valueTo.

这是我的xml文件

  1. <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:duration="2000"
  3. android:propertyName="backgroundColor"
  4. android:repeatCount="infinite"
  5. android:repeatMode="reverse"
  6. android:valueFrom="#FF0000"
  7. android:valueTo="#000000" />

在Java中,我称之为:

  1. ObjectAnimator objAnim = (ObjectAnimator)AnimatorInflater.loadAnimator(getActivity(),R.animator.animator_bkg);
  2. objAnim.setTarget(view);
  3. objAnim.start();

请注意,当我对布局的alpha进行动画处理时,它的工作原理如预期.这是Android的错误(4.0.3在华硕变压器),还是我错过了什么?

解决方法

我google了一下有答案尝试使用TransitionDrawable. http://developer.android.com/guide/topics/resources/drawable-resource.html#Transition

而且,stackoverflow.com上还有一个专门针对相同问题的主题.

ADDED代码示例:

  1. Button btn = (Button)this.findViewById(R.id.btn1);
  2. //Let's change background's color from blue to red.
  3. ColorDrawable[] color = {new ColorDrawable(Color.BLUE),new ColorDrawable(Color.RED)};
  4. TransitionDrawable trans = new TransitionDrawable(color);
  5. //This will work also on old devices. The latest API says you have to use setBackground instead.
  6. btn.setBackgroundDrawable(trans);
  7. trans.startTransition(5000);

猜你在找的Android相关文章