在上一篇的博客中,主要介绍了如何使用GridLayoutAnimationController来实现GridView的item初始加载的动画,但是不论是LayoutAnimationController还是GridLayoutAnimationController都是只在item第一次加载显示去起作用,之后就不会再有动画效果了,当然你可以在适配器里手动添加动画,这个就不介绍了!!
本篇博客将主要介绍如何使用LayoutTransition实现View的添加和移除动画,当然了,这里面有一些坑需要我们自己注意一下!!
首先看一下不设置CHANGE_DISAPPEARING和CHANGE_APPEARING动画的效果图:
这里我说的没有设置只是没有设置自定义的动画效果,在系统源码中,如果开发者没有设置动画,系统会使用默认的动画,一会看一下源码就清楚了!!!
再看一下所有动画都设置的效果图:
跟上面的一对比还是可以明显感觉出来差别的,这在里就不多说了,先看一下代码吧!!
主布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="addViewToParent"
android:text="@string/add_view" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"