Android开发之LayoutAnimation的基本使用

1、先看一张图

2、功能说明
2.1.1、Animation的基本使用参照:Android开发之Animation的基本使用
2.1.2、ListView的基本使用参照:Android开发基础控件ListView的使用
2.2、ListView中各个单元格按顺序执行动画,使用xml实现;
2.3、基本实现步骤:先创建一个animation–>创建一个animation_layout–>绑定到listview上;
2.4、参考代码
list_anim.xml

1
2
3
4
5
6
7
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="2000">
    <alpha
        android:fromAlpha="0"
        android:toAlpha="1" />
</set>

list_anim_layout.xml

1
2
3
4
5
<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
    android:delay="0.5"
    android:animationOrder="normal"
    android:animation="@anim/list_anim" />

activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?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"
    tools:context="com.yusian.layoutanimation.MainActivity">
 
    <ListView
        android:id="@+id/lv_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layoutAnimation="@anim/list_anim_layout">
 
    </ListView>
    <Button
        android:id="@+id/btn_start_animation"
        android:layout_width="match_parent"
        android:layout_height="55dp"
        android:text="开始动画"/>
 
</LinearLayout>

One thought on “Android开发之LayoutAnimation的基本使用

  1. Sian Post author

    使用代码实现动画即创建一个LayoutAnimationController对象,然后调用listView的setLayoutAnimation()方法及startLayoutAnimation()方法即可:
    Activity

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    
    package com.yusian.layoutanimation;
     
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.animation.AlphaAnimation;
    import android.view.animation.Animation;
    import android.view.animation.AnimationUtils;
    import android.view.animation.LayoutAnimationController;
    import android.widget.ListView;
    import android.widget.SimpleAdapter;
     
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
     
    public class MainActivity extends AppCompatActivity implements View.OnClickListener{
        private ListView listView;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            findViewById(R.id.btn_start_animation).setOnClickListener(this);
            listView = (ListView) findViewById(R.id.lv_main);
            List<HashMap<String, String>> array = new ArrayList<>();
            HashMap map1 = new HashMap();
            map1.put("name", "张三");
            map1.put("sex", "男");
            HashMap map2 = new HashMap();
            map2.put("name", "李四");
            map2.put("sex", "女");
            HashMap map3 = new HashMap();
            map3.put("name", "王五");
            map3.put("sex", "男");
            array.add(map1);
            array.add(map2);
            array.add(map3);
     
            SimpleAdapter adapter = new SimpleAdapter(this,
                    array, R.layout.listview_cell,
                    new String[]{"name", "sex"},
                    new int[]{R.id.tv_name, R.id.tv_sex});
            listView.setAdapter(adapter);
        }
     
        @Override
        public void onClick(View v) {
            Animation animation = AnimationUtils.loadAnimation(this,R.anim.list_anim);
            LayoutAnimationController lac = new LayoutAnimationController(animation);
            lac.setDelay(0.5f);
            lac.setOrder(LayoutAnimationController.ORDER_RANDOM);
            listView.setLayoutAnimation(lac);
            listView.startLayoutAnimation();
        }
    }

Leave a Reply