animateLayoutChanges和layoutTransition的使用


在布局文件中, 我们难免会进行一些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)}.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 本笔记的原文本链接 Property Animation Overview 属性动画总览 The property...
    Jaesoon阅读 1,248评论 2 3
  • 基础篇 分类: View Animation: 固定方式的view动画,扩展性不强。 Drawable anima...
    JeremyDai阅读 322评论 0 0
  • Activity切换动画 当前可以通过activity.overridePendingTransition()设置...
    proud2008阅读 1,116评论 0 0
  • 查看并关闭所有客户系统 vim-cmd vmsvc/getallvms 取得所有客户系统的编号 vim-cmd v...
    dongu阅读 3,555评论 0 0
  • 姐姐怀孕小孩生病让我请假去杭州检查 我这个月做绩效,谁差谁走人 然后我要准备失业了 选择题很难,不是考试,可以知道...
    南南的鱼阅读 193评论 0 1

友情链接更多精彩内容