Android buttom textview 颜色平滑过渡的动画效果
16lz
2021-01-23
1. TransitionDrawable。例如,在文件夹中绘制一个xml文件,你可以这样写:
<?xml version="1.0" encoding="UTF-8"?>
然后,在你的xml的实际检视你都引用这个TransitionDrawable在android:background
属性。 在这一点上,你可以通过执行启动代码中的过渡: TransitionDrawable transition = (TransitionDrawable) viewObj.getBackground();transition.startTransition(transitionTime);
或通过调用运行在反向过渡: transition.reverseTransition(transitionTime);
我希望这可以帮助您解决您的问题!
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的库。
argb
在一个XML文件中的值。 你的观点有它的颜色由设置view.setBackgroundColor()
您的观点已在绘制中定义它的背景色,不定义如中风或角落的任何额外的属性 在绘制你的观点有它的背景色定义和要删除像中风或角落的任何额外的属性牢记 CodeGo.net,去除多余的属性将不会动画。 对象动画作品通过调用view.setBackgroundColor
它取代了定义绘制的,除非是它的一个实例ColorDrawable
,它很少是。从像中风或角落的可绘制任何额外的背景属性将被删除。 如果使用一个值动画: 在绘制也设置类似的行程或转角的属性你的观点有它的背景色定义,你想改变它运行时决定一个新的颜色。 如果使用过渡绘制: 你认为应该是以前已经定义了两个可绘制之间切换 我曾与那当我打开,我一直没能解决DrawerLayout运行转换可绘制性能问题,因此,如果您遇到任何意外口吃你可能遇到的bug,因为我有。 你将不得不修改值动画的例子,如果你想有一个StateLists绘制或LayerLists绘制,否则会崩溃的。 final
GradientDrawable
background = (GradientDrawable) view.getBackground();
GradientDrawable能设置button或者textview的边框等等属性。
xml:
创建ObjectAnimator:
final ObjectAnimator backgroundColorAnimator = ObjectAnimator.ofObject(view, "backgroundColor", new ArgbEvaluator(), 0xFFFFFFFF, 0xff78c5f9);backgroundColorAnimator.setDuration(300);backgroundColorAnimator.start();
您还可以从AnimatorInflater加载动画定义像XMight确实在Android的objectAnimator动画布局的backgroundColor 值动画: xml:
可绘制对象的xml:
<?xml version="1.0" encoding="utf-8"?>
创建一个这样的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:
可绘制对象的xml:
<?xml version="1.0" encoding="utf-8"?> -
transition>
使用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; }
更多相关文章
- Activity 属性设置大全
- android 常见布局及控件的属性详解
- Android布局动画之animateLayoutChanges与LayoutTransition
- 转:善用Android预定义样式