Android buttom textview 颜色平滑过渡的动画效果

1. TransitionDrawable。例如,在文件夹中绘制一个xml文件,你可以这样写:

<?xml version="1.0" encoding="UTF-8"?>
<transition xmlns:android=" CodeGo.net 
 <!-- The drawables used here can be solid colors, gradients, shapes, images, etc. -->
 <item android:drawable="@drawable/original_state" />
 <item android:drawable="@drawable/new_state" />
</transition>
然后,在你的xml的实际检视你都引用这个TransitionDrawable在android:background属性。 在这一点上,你可以通过执行启动代码中的过渡:
TransitionDrawable transition = (TransitionDrawable) viewObj.getBackground();
transition.startTransition(transitionTime);
或通过调用运行在反向过渡:
transition.reverseTransition(transitionTime);

我希望这可以帮助您解决您的问题! 


2. 属性动画的ValutAnimator动画:
Integer colorFrom = getResources().getColor(R.color.red);
Integer colorTo = getResources().getColor(R.color.blue);
 ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(),colorFrom,colorTo);
            colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animator) {
                    setBackgroundColor((Integer)animator.getAnimatedValue());
                }
            });
            colorAnimation.setDuration(200);
            colorAnimation.start();

兼容低版本至Android 2.x,可以使用nineoldAndroids的库。 


3. 根据您的观点得到它的背景颜色,以及如何让你的目标颜色有几种不同的方法来做到这一点。 优先个Android的属性动画 如果使用对象的动画: 你的观点有其背景色定义为argb在一个XML文件中的值。 你的观点有它的颜色由设置view.setBackgroundColor()您的观点已在绘制中定义它的背景色,不定义如中风或角落的任何额外的属性 在绘制你的观点有它的背景色定义和要删除像中风或角落的任何额外的属性牢记 CodeGo.net,去除多余的属性将不会动画。 对象动画作品通过调用view.setBackgroundColor它取代了定义绘制的,除非是它的一个实例ColorDrawable,它很少是。从像中风或角落的可绘制任何额外的背景属性将被删除。 如果使用一个值动画: 在绘制也设置类似的行程或转角的属性你的观点有它的背景色定义,你想改变它运行时决定一个新的颜色。 如果使用过渡绘制: 你认为应该是以前已经定义了两个可绘制之间切换 我曾与那当我打开,我一直没能解决DrawerLayout运行转换可绘制性能问题,因此,如果您遇到任何意外口吃你可能遇到的bug,因为我有。 你将不得不修改值动画的例子,如果你想有一个StateLists绘制或LayerLists绘制,否则会崩溃的。

finalGradientDrawable background = (GradientDrawable) view.getBackground();

GradientDrawable能设置button或者textview的边框等等属性


xml

<View
 android:background="#FFFF0000"
 android:layout_width="50dp"
 android:layout_height="50dp"/>
创建ObjectAnimator:
final ObjectAnimator backgroundColorAnimator = ObjectAnimator.ofObject(view,
                  "backgroundColor",
                  new ArgbEvaluator(),
                  0xFFFFFFFF,
                  0xff78c5f9);
backgroundColorAnimator.setDuration(300);
backgroundColorAnimator.start();

您还可以从AnimatorInflater加载动画定义像XMight确实在Android的objectAnimator动画布局的backgroundColor 值动画: xml
<View
 android:background="@drawable/example"
 android:layout_width="50dp"
 android:layout_height="50dp"/>
可绘制对象的xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android=" CodeGo.net 
 <solid android:color="#FFFFFF"/>
 <stroke
  android:color="#edf0f6"
  android:width="1dp"/>
 <corners android:radius="3dp"/>
</shape>

创建一个这样的ValueAnimator
final ValueAnimator valueAnimator = ValueAnimator.ofObject(new ArgbEvaluator(),
                    0xFFFFFFFF,
                    0xff78c5f9);
            final GradientDrawable background = (GradientDrawable) getBackground();
            valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(final ValueAnimator animator) {
                    background.setColor((Integer) animator.getAnimatedValue());
                }
            });
            valueAnimator.setDuration(300);
            valueAnimator.start();

过渡绘制: xml:
<View
 android:background="@drawable/example"
 android:layout_width="50dp"
 android:layout_height="50dp"/>
可绘制对象的xml:
<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android=" 
 <item>
  <shape>
   <solid android:color="#FFFFFF"/>
   <stroke
    android:color="#edf0f6"
    android:width="1dp"/>
   <corners android:radius="3dp"/>
  </shape>
 </item>
 <item>
  <shape>
   <solid android:color="#78c5f9"/>
   <stroke
    android:color="#68aff4"
    android:width="1dp"/>
   <corners android:radius="3dp"/>
  </shape>
 </item>
</<span style="font-family: Arial, Helvetica, sans-serif;">transition></span>
使用TransitionDrawable
final TransitionDrawable background = (TransitionDrawable) view.getBackground();
background.startTransition(300);

你可以通过调用扭转动画.reverse()在动画实例。 还有其他方法可以做到,但动画这三个大概是我一个ValueAnimator。 


4. 另一种简单的方法来实现这一目标是执行AlphaAnimation。 让你的视图的ViewGroup 添加一个子视图将其索引为0,与match_parent布局 给你的子的背景作为容器 改变到容器到目标色彩的背景 淡出AlphaAnimation。 取出子时的动画效果(使用AnimationListener)



最终我是用的代码如下


/**
         * Set whether this tag view is in the checked state.
         *
         * @param checked true is checked, false otherwise
         */
        public void setCheckedChangeColor(boolean checked) {
            isChecked = checked;
            changeColorTransition(checked);

        }

        private void changeColorTransition(final boolean checked){
            animUpdateDrawable = true;

            int fromColor,toColor;
            if (checked) {
                fromColor = backgroundColor;
                toColor = checkedBackgroundColor;
            } else {
                fromColor = checkedBackgroundColor;
                toColor = backgroundColor;
            }

            ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), fromColor, toColor);
            colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animator) {

                    GradientDrawable toDrawable = new GradientDrawable();
                    toDrawable.setCornerRadii(mRadius);
                    toDrawable.setColor((Integer)animator.getAnimatedValue());
                    toDrawable.setStroke(mStrokeWidth, !checked ? mStrokeColor.getDefaultColor() : mCheckedStrokeColor.getDefaultColor());

                    if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) {
                        setBackgroundDrawable(toDrawable);
                    } else {
                        setBackground(toDrawable);
                    }

                }

            });

            colorAnimation.addListener(new ValueAnimator.AnimatorListener(){
                @Override
                public void onAnimationEnd(Animator animator) {
                    animUpdateDrawable = false;

                    if (checked) {
                        setTextColor(checkedTextColor);
                    } else {
                        setTextColor(textColor);
                    }
                }

                @Override
                public void onAnimationCancel(Animator animator) {
                }

                @Override
                public void onAnimationRepeat(Animator animator) {
                }

                @Override
                public void onAnimationStart(Animator animator) {
                }
            });
            colorAnimation.setDuration(350);
            colorAnimation.start();
        }

        @Override
        protected boolean getDefaultEditable() {
            return true;
        }


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值