Android UI

动画第十步->LayoutAnimation、gridLa

2016-12-23  本文已影响171人  crossroads
极客学院Animation教程讲解的很详细,点击进入哦

这里为学习的整理和补充O(∩_∩)O

前言

LayoutAnimation :普通viewGroup 添加统一的进入动画
gridLayoutAnimation: 针对 gridView 添加进入动画

LayoutAnimation 和 gridLayoutAnimation 在 API 1 中就有的函数,因为是在 API 1 中就引入了,所以也只能使用 animtion 来做动画,而不能使用 animator。

一、LayoutAnimation

1. xml实现

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:duration="1000"
    >
    <translate
        android:fromXDelta="-30%"
        android:toXDelta="0"
/>
    <alpha
        android:fromAlpha="0.0"
        android:toAlpha="1.0"
/>
</set>
<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:animation="@anim/slide_in_left"
    android:animationOrder="normal"
    android:delay="1"
/>

LayoutAnimation详解:

  • delay:指每个 Item 的动画开始延时,取值是 android:animation 所指定动画时长的倍数,取值类型可以是 float 类型,也可以是百分数,默认是 0.5;
    在 slide_in_left.xml 中android:duration="1000",即单次动画的时长是 1000 毫秒,而我们在这里的指定 android:delay=”1”,即一个 Item 的动画会在上一个 item 动画完成后延时单次动画时长的一倍时间开始,即延时 1000 毫秒后开始。
  • animationOrder:指 viewGroup 中的控件动画开始顺序,取值有 normal(正序)、reverse(倒序)、random(随机)
<ListView
    android:id="@+id/listview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
  android:layoutAnimation="@anim/layout_animation"
/>
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ListView listView = (ListView) findViewById(R.id.listview);
    arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_expandable_list_item_1, getData());
    listView.setAdapter(arrayAdapter);
}
public List<String> getData() {
    ArrayList<String> strings = new ArrayList<>();
    for (int i = 0; i < 20; i++) {
        strings.add("测试"+i);
    }
    return strings;
}

执行效果如下:


效果

会发现:
1.第二屏以及之后的数据并不会有动画
2.listview 中各个 item 从左至右滑入位置
3.动画仅在第一次创建时有用,后期加入的数据,将不会再有动画(这个大家可以自己测一下,这里仅标注一下),换句话说,就是

** android:layoutAnimation 只在 viewGroup 创建的时候,才会对其中的 item 添加动画。在创建成功以后,再向其中添加 item 将不会再有动画。**

2. LayoutAnimation 的代码实现—LayoutAnimationController

public LayoutAnimationController(Animation animation)
public LayoutAnimationController(Animation animation, float delay)
/**
 * 设置 animation 动画
 */
public void setAnimation(Animation animation)
/**
 * 设置单个 item 开始动画延时
 */
public void setDelay(float delay)
/**
 * 设置 viewGroup 中控件开始动画顺序,取值为 ORDER_NORMAL、ORDER_REVERSE、ORDER_RANDOM
 */
public void setOrder(int order)
Animation loadAnimation = AnimationUtils.loadAnimation(this, R.anim.slide_in_left);
LayoutAnimationController layoutAnimationController=new LayoutAnimationController(loadAnimation);
layoutAnimationController.setOrder(LayoutAnimationController.ORDER_NORMAL);
layoutAnimationController.setDelay(1);
listView.setLayoutAnimation(layoutAnimationController);
listView.startLayoutAnimation();

二、GridLayoutAnimation

1. xml实现

<?xml version="1.0" encoding="utf-8"?>
<gridLayoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
                     android:rowDelay="75%"
                     android:columnDelay="60%"
                     android:directionPriority="row"
                     android:direction="top_to_bottom|right_to_left"
                     android:animation="@android:anim/slide_in_left"
/>

rowDelay:每一行动画开始的延迟。与 LayoutAnimation 一样
columnDelay:每一列动画开始的延迟。取值类型及意义与 rowDelay 相同。
directionPriority:方向优先级。取值为 row,collumn,none,意义分别为:行优先,列优先,和无优先级(同时进行)
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;即从上往下,从左往右。
animation: gridview 内部元素所使用的动画。

<GridView
    android:id="@+id/grid"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnWidth="90dp"
    android:gravity="center"
    android:horizontalSpacing="10dp"
    android:layoutAnimation="@anim/gridlayout_animation"
    android:numColumns="auto_fit"
    android:stretchMode="columnWidth"
    android:verticalSpacing="10dp"
/>
arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_expandable_list_item_1, getData());
gridView.setAdapter(arrayAdapter);

效果:

2. 代码实现-GridLayoutAnimationController

/**
 * 设置列动画开始延迟
 */
public void setColumnDelay(float columnDelay)
/**
 * 设置行动画开始延迟
 */
 public void setRowDelay(float rowDelay)
 /**
 * 设置 gridview 动画的入场方向。取值有:DIRECTION_BOTTOM_TO_TOP、DIRECTION_TOP_TO_BOTTOM、DIRECTION_LEFT_TO_RIGHT、DIRECTION_RIGHT_TO_LEFT
 */
 public void setDirection(int direction)
 /**
 * 动画开始优先级,取值有 PRIORITY_COLUMN、PRIORITY_NONE、PRIORITY_ROW
 */
 public void setDirectionPriority(int directionPriority)

好啦,就到这里啦在此,祝大家圣诞节快乐O(∩_∩)O

上一篇下一篇

猜你喜欢

热点阅读