动画[6]PropertyAnimator LayoutTransition

来源:互联网 发布:淘宝招聘信息 编辑:程序博客网 时间:2024/06/02 01:20

【参考链接】

LayoutTransition(布局容器动画)http://www.bubuko.com/infodetail-826585.html

 

API 11开始,AndroidViewGroup提供了setLayoutTranslation()方法

通过为ViewGroup指定LayoutTranslation,当ViewGroup调用addView()removeView(),或者子View调用setVisiblity()等方法时,子View能够产生动画效果。

/**
 * This class enables automaticanimations on layout changes in ViewGroup objects. To enable
 * transitions for a layoutcontainer, create a LayoutTransition object and set it on any
 * ViewGroup by calling {@link ViewGroup#setLayoutTransition(LayoutTransition)}.This will cause
 * default animations to runwhenever items are added to or removed from that container. To specify
 * custom animations, use the {@linkLayoutTransition#setAnimator(int, Animator)
 * setAnimator()} method.
 *
 *
<p>One of the core concepts of thesetransition animations is that there are two types of
 * changes that cause the transitionand four different animations that run because of
 * those changes. The changes thattrigger the transition are items being added to a container
 * (referred to as an"appearing" transition) or removed from a container (also known as
 * "disappearing").Setting the visibility of views (between GONE and VISIBLE) will trigger
 * the same add/remove logic. Theanimations that run due to those events are one that animates
 * items being added, one thatanimates items being removed, and two that animate the other
 * items in the container thatchange due to the add/remove occurrence. Users of
 * the transition may want differentanimations for the changing items depending on whether
 * they are changing due to anappearing or disappearing event, so there is one animation for
 * each of these variations of thechanging event. Most of the API of this class is concerned
 * with setting up the basicproperties of the animations used in these four situations,
 * or with setting up customanimations for any or all of the four.
</p>
 
*
 *
<p>By default, the DISAPPEARING animationbegins immediately, as does the CHANGE_APPEARING
 * animation. The other animationsbegin after a delay that is set to the default duration
 * of the animations. This behaviorfacilitates a sequence of animations in transitions as
 * follows: when an item is beingadded to a layout, the other children of that container will
 * move first (thus creating spacefor the new item), then the appearing animation will run to
 * animate the item being added.Conversely, when an item is removed from a container, the
 * animation to remove it will runfirst, then the animations of the other children in the
 * layout will run (closing the gapcreated in the layout when the item was removed). If this
 * default choreography behavior isnot desired, the {@link#setDuration(int, long)} and
 * {@link #setStartDelay(int,long)} of any or all of the animations can be changed as
 * appropriate.
</p>

 

可以设置如下4种情况的动画效果

LayoutTransition.APPEARING

当添加一个View时,此View的动画效果

LayoutTransition.DISAPPEARING

当移除一个View时,此View的动画效果

LayoutTransition.CHANGE_APPEARING

当添加一个View时,会发生位置移动的其他View的动画效果

LayoutTransition.CHANGE_DISAPPEARING

当移除一个View时,会发生位置移动的其他View的动画效果

并且,会等CHANGE_APPEARING执行完以后才执行APPEARING,等DISAPPEARING执行完以后才执行CHANGE_DISAPPEARING

 

我们可以直接使用系统默认的动画效果,也可以设置自己的。

以如下代码为例,设置LinearLayout的添加删除子View时的动画效果

//默认的LayoutTransition
layoutTransition=newLayoutTransition();
ll.setLayoutTransition(layoutTransition);

defaultAppearingAnim=layoutTransition
       
.getAnimator(LayoutTransition.APPEARING);
defaultDisappearingAnim=layoutTransition
       
.getAnimator(LayoutTransition.DISAPPEARING);
defaultChangingAppearingAnim=layoutTransition
       
.getAnimator(LayoutTransition.CHANGE_APPEARING);
defaultChangingDisappearingAnim=layoutTransition
       
.getAnimator(LayoutTransition.CHANGE_DISAPPEARING);

//自定义的Appearing动画:以Y为轴翻转
customAppearingAnim= ObjectAnimator.ofFloat(null,"rotationY",90f,0f)
       .setDuration(
layoutTransition.getDuration(LayoutTransition.APPEARING));

//自定义的Disappearing动画:以X为轴翻转
customDisappearingAnim= ObjectAnimator.ofFloat(null,"rotationX",0f,90f)
       .setDuration(
layoutTransition.getDuration(LayoutTransition.DISAPPEARING));

//后面两个动画必须得叠加这4个属性//并且后面两个动画的of()中的values不能为2个值,不然没有效果
//
不知道为什么!!!
PropertyValuesHolderpvhLeft = PropertyValuesHolder.ofInt("left",0,1);
PropertyValuesHolderpvhTop = PropertyValuesHolder.ofInt("top",0,1);
PropertyValuesHolderpvhRight = PropertyValuesHolder.ofInt("right",0,1);
PropertyValuesHolderpvhBottom = PropertyValuesHolder.ofInt("bottom",0,1);
//PropertyValuesHolderpvhScrollX = PropertyValuesHolder.ofInt("scrollX", 0, 1);
//PropertyValuesHolder pvhScrollY = PropertyValuesHolder.ofInt("scrollY",0, 1);

//
自定义的ChangeAppearing动画
PropertyValuesHolderpvhScaleX = PropertyValuesHolder.ofFloat("scaleX",1f,0f,1f);
PropertyValuesHolderpvhScaleY = PropertyValuesHolder.ofFloat("scaleY",1f,0f,1f);

customChangingAppearingAnim= ObjectAnimator.ofPropertyValuesHolder(newObject(),pvhLeft,pvhTop,pvhRight,
       
pvhBottom,pvhScaleX,pvhScaleY)
       .setDuration(
layoutTransition.getDuration(LayoutTransition.CHANGE_APPEARING));

//自定义的ChangeDisappearing动画
PropertyValuesHolderpvhRotation = PropertyValuesHolder.ofFloat("rotation",0f,360f,0f);
////或者通过这样来定义
//Keyframe kf0 = Keyframe.ofFloat(0f, 0f);
//Keyframe kf1 = Keyframe.ofFloat(.9999f, 360f);
//Keyframe kf2 = Keyframe.ofFloat(1f, 0f);
//PropertyValuesHolder pvhRotation =PropertyValuesHolder.ofKeyframe("rotation", kf0, kf1, kf2);
customChangingDisappearingAnim= ObjectAnimator.ofPropertyValuesHolder(newObject(),pvhLeft,pvhTop,pvhRight,
       
pvhBottom,pvhRotation)
       .setDuration(
layoutTransition.getDuration(LayoutTransition.CHANGE_DISAPPEARING));

 

 

 

原创粉丝点击