安卓动画学习-补间动画进阶
- 这篇来说说为布局做一下动画展示,这么说吧,我们平常看到的页面都是直接展示的,我们似乎没有见到过布局中的每一个控件是怎么加入到整个页面当中的,也没有见过他是什么时候加入的,这一篇我们就来看看这个事情
- 我们去看看ViewGroup的官方文档就会发现,他有一个
android:layoutAnimation
- 我们可以通过这个属性为我们的ViewGroup设置第一次加载动画
- 我在xml文件中试了一下,发现他只能引用补间动画的xml文件
- 好吧,再去看看补间动画的东西
- 我们发现了两个貌似有用的类
LayoutAnimationController 布局动画控制器用于为布局或视图组的子项设置动画。每个孩子使用相同的动画
GridLayoutAnimationController 布局动画控制器用于为网格布局的子节点生成动画。
LayoutAnimationController
看一下他的xml属性
android:animation 用于每个子项的动画。
android:animationOrder 动画的开始顺序。
android:delay 用于延迟每个子项动画开始。
android:interpolator 插值器用于内插每个动画开始之间的延迟。
- 其中,第一个是对一个补间动画的引用
- 第二个表示子项进入的顺序,有三个常量可以设置
int ORDER_NORMAL 按照视图添加到视图组的顺序分配动画延迟。
int ORDER_RANDOM 随机分发动画延迟。
int ORDER_REVERSE 按照将视图添加到视图组的相反顺序分配动画延迟。
- 也就是说子项要么是从最上面到最下面依次出现,要么是从下到上依次出现,要么是随机出现
- 第三个就是对每次动画开始之前的延迟
- 第四个是插值器
看一下构造方法
LayoutAnimationController(Context context, AttributeSet attrs)
从外部资源创建一个新的布局动画控制器。
LayoutAnimationController(Animation animation)
以50%的延迟和指定的动画创建新的布局动画控制器。
LayoutAnimationController(Animation animation, float delay)
用指定的延迟和指定的动画创建一个新的布局动画控制器。
- 第一个我也没弄清,第二个和第三个容易理解
- 构建好之后我们就可以使用我们的派生自ViewGroup的控件set进去就ok
下面给大家看一下具体用法吧
- 先上图
- 代码
- 主布局
<LinearLayout
android:layoutAnimation="@anim/my_linear_animation"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<Button
android:background="@color/colorAccent"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:background="@color/colorPrimaryDark"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:background="@color/colorPrimary"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
- GIF图中那四个就是这四个按钮,我给这个LinearLayout设置了动画,看这个动画xml文件
<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation
xmlns:android="http://schemas.android.com/apk/res/android"
android:animation="@anim/my_linear_anim"
android:delay="1"
android:animationOrder="normal"
/>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
>
<translate
android:fromXDelta="-50%p"
android:toYDelta="0"
/>
<alpha
android:fromAlpha="0"
android:toAlpha="1"/>
</set>
- 代码很简单,也没啥说的,要注意的就是这里的delay属性
- 官方这么说,子动画延迟=子项索引延迟动画持续时间
- 再看一下代码实现
Animation animation = AnimationUtils.loadAnimation(this,R.anim.my_linear_anim);
LayoutAnimationController controller = new LayoutAnimationController(animation,1);
controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
LinearLayout linearLayout = findViewById(R.id.main_Layout);
linearLayout.setLayoutAnimation(controller);
- 不用解释
- 不过这里要说明一下,这个只对第一次有用,比方说你把这个运用到ListView上面去,当手动添加Item的时候是没有进场动画的,在VIewGroup的官方文档中有说到
-android:layoutAnimation Defines the layout animation to use the first time the ViewGroup is laid out. Layout animations can also be started manually after the first layout.
- 大概意思就是动画只会在布局时生效,但是我们可以手动开启动画
- 先来看看这个类的一些基本常用方法吧
Animation getAnimation()
final Animation getAnimationForView(View view)
boolean isDone()
void setAnimation(Animation animation)
void setAnimation(Context context, int resourceID)
void setDelay(float delay)
void start()
趁热打铁看一下他的子类GridLayoutAnimation
xml属性
- 继承自父类LayoutAnimationController所有的属性之外,他还有自己的属性
android:columnDelay 每一列动画开始的延迟。取值类型及意义与rowDelay相同。
android:direction gridview动画方向。
取值有四个:left_to_right:列,从左向右开始动画
right_to_left :列,从右向左开始动画
top_to_bottom:行,从上向下开始动画
bottom_to_top:行,从下向上开始动画
这四个值之间可以通过|连接,从而可以取多个值。很显然left_to_right和right_to_left是互斥的,top_to_bottom和bottom_to_top是互斥的。如果不指定 direction字段,默认值为left_to_right | top_to_bottom;即从上往下,从左往右。
android:directionPriority 方向优先级。取值为row,collumn,none,意义分别为:行优先,列优先,和无优先级(同时进行)
android:rowDelay 每一行动画开始的延迟。与LayoutAnimation一样,可以取百分数,也可以取浮点数。取值意义为,当前android:animation所指动画时长的倍数。
- 其实跟LayoutAnimationController基本上是一样的
- 看下代码
<gridLayoutAnimation
xmlns:android="http://schemas.android.com/apk/res/android"
android:rowDelay="75%"
android:columnDelay="60%"
android:directionPriority="none"
android:animation="@anim/my_linear_anim"
/>
<GridLayout
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="3"
android:layoutAnimation="@anim/my_gridlayoutanimation"
android:verticalSpacing="10dp">
----
</GridLayout>
- 嗯,剩下的都一样的,也就不再说明了,具体的根据具体情况再说吧