在布局文件中, 我们难免会进行一些addView, removeView,以及visiable,gone等操作, 如果不添加动画的,操作看起来会有些生硬,不是很好,今天我们通过两种方式来给ViewGroup中添加动画事件
方法1: 在布局文件中添加属性android:animateLayoutChanges="true"
设置默认的布局动画, 以下为默认布局动画的代码
public LayoutTransition() {
if (defaultChangeIn == null) {
// "left" is just a placeholder; we'll put real properties/values in when needed
PropertyValuesHolder pvhLeft = PropertyValuesHolder.ofInt("left", 0, 1);
PropertyValuesHolder pvhTop = PropertyValuesHolder.ofInt("top", 0, 1);
PropertyValuesHolder pvhRight = PropertyValuesHolder.ofInt("right", 0, 1);
PropertyValuesHolder pvhBottom = PropertyValuesHolder.ofInt("bottom", 0, 1);
PropertyValuesHolder pvhScrollX = PropertyValuesHolder.ofInt("scrollX", 0, 1);
PropertyValuesHolder pvhScrollY = PropertyValuesHolder.ofInt("scrollY", 0, 1);
defaultChangeIn = ObjectAnimator.ofPropertyValuesHolder((Object)null,
pvhLeft, pvhTop, pvhRight, pvhBottom, pvhScrollX, pvhScrollY);
defaultChangeIn.setDuration(DEFAULT_DURATION);
defaultChangeIn.setStartDelay(mChangingAppearingDelay);
defaultChangeIn.setInterpolator(mChangingAppearingInterpolator);
defaultChangeOut = defaultChangeIn.clone();
defaultChangeOut.setStartDelay(mChangingDisappearingDelay);
defaultChangeOut.setInterpolator(mChangingDisappearingInterpolator);
defaultChange = defaultChangeIn.clone();
defaultChange.setStartDelay(mChangingDelay);
defaultChange.setInterpolator(mChangingInterpolator);
defaultFadeIn = ObjectAnimator.ofFloat(null, "alpha", 0f, 1f);
defaultFadeIn.setDuration(DEFAULT_DURATION);
defaultFadeIn.setStartDelay(mAppearingDelay);
defaultFadeIn.setInterpolator(mAppearingInterpolator);
defaultFadeOut = ObjectAnimator.ofFloat(null, "alpha", 1f, 0f);
defaultFadeOut.setDuration(DEFAULT_DURATION);
defaultFadeOut.setStartDelay(mDisappearingDelay);
defaultFadeOut.setInterpolator(mDisappearingInterpolator);
}
mChangingAppearingAnim = defaultChangeIn;
mChangingDisappearingAnim = defaultChangeOut;
mChangingAnim = defaultChange;
mAppearingAnim = defaultFadeIn;
mDisappearingAnim = defaultFadeOut;
}
方法2: 通过代码设置layoutTransition
属性, 定制你需要的动画
layoutTransition
通过5种方式设置动画的类型:
CHANGE_APPEARING:
A flag indicating the animation that runs on those items that are changing
due to an item disappearing from the container.
CHANGE_DISAPPEARING:
A flag indicating the animation that runs on those items that are changing due to an item disappearing from the container.
APPEARING:
A flag indicating the animation that runs on those items that are appearing
in the container.
DISAPPEARING:
A flag indicating the animation that runs on those items that are disappearing from the container.
CHANGING:
A flag indicating the animation that runs on those items that are changing due to a layout change not caused by items being added to or removed from the container. This transition type is not enabled by default; it can be enabled via {@link #enableTransitionType(int)}.
网友评论