Android Animation(动画)---基础二(LayoutAnimationController)
程序员文章站
2022-03-17 11:12:26
...
LayoutAnimationController动画效果,一次出现
一、布局文件使用
- 动画list_item_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:animation="@anim/list_item_alpha"
android:animationOrder="normal"
android:delay="0.8"
/>
- 每个列表项动画list_item_alpha.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="2000"
/>
</set>
- 主界面布局文件activity_main.xml
特别注意要在ListView中添加android:layoutAnimation="@anim/list_item_layout"
<?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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.mazaiting.layoutanimationcontroller.MainActivity"
>
<ListView
android:id="@+id/listView"
android:layoutAnimation="@anim/list_item_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:text="测试"
android:onClick="test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:text="清空"
android:onClick="clear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
- 列表布局文件item_list.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/app_name"
android:textSize="20sp"
/>
- 主界面代码
public class MainActivity extends AppCompatActivity {
private ListView mListView;
private static final String[] STRINGS = {
"BruceZhang", "Alpha", "Translate", "Blanklin", "Rotate", "GreenFrank"
};
private ArrayAdapter<String> mAdapter;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) this.findViewById(R.id.listView);
mAdapter = new ArrayAdapter<>(this, R.layout.item_list, STRINGS);
}
public void test(View view) {
mListView.setAdapter(mAdapter);
}
public void clear(View view) {
mListView.setAdapter(null);
}
}
二、代码配置
- 动画文件
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="2000"
/>
</set>
- 主界面布局文件
<?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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.mazaiting.layoutanimationcontroller.MainActivity"
>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:text="测试"
android:onClick="test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:text="清空"
android:onClick="clear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
- 主界面动画设置
public class MainActivity extends AppCompatActivity {
private ListView mListView;
private static final String[] STRINGS = {
"BruceZhang", "Alpha", "Translate", "Blanklin", "Rotate", "GreenFrank"
};
private ArrayAdapter<String> mAdapter;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) this.findViewById(R.id.listView);
mAdapter = new ArrayAdapter<>(this, R.layout.item_list, STRINGS);
// 1. 创建一个Animation对象
Animation animation = AnimationUtils.loadAnimation(this, R.anim.list_item_alpha);
// 2. 使用如下代码创建LayoutAnimationController对象
LayoutAnimationController lac = new LayoutAnimationController(animation);
// 3. 设置控件显示的顺序
lac.setOrder(LayoutAnimationController.ORDER_RANDOM);
// 4. 为ListView设置LayoutAnimationController尚需经
mListView.setLayoutAnimation(lac);
}
public void test(View view) {
mListView.setAdapter(mAdapter);
}
public void clear(View view) {
mListView.setAdapter(null);
}
}
上一篇: 数据类型(类)转换
推荐阅读
-
黑马Android76期学习笔记01基础--day07--广播,有、无序广播、特殊广播接受者、样式和主题,this与context的区别、普通对话框,进度条对话框、帧动画
-
Android基础之Handler机制(二)之Message源码分析
-
Android开发--图形图像与动画(五)--详解LayoutAnimationController
-
android 飘心动画(直播点赞)效果(二)---贝塞尔曲线的实现
-
Android 属性动画(Property Animation) ObjectAnimator的介绍
-
Android基础夯实--重温动画(二)之Frame Animation
-
android 动画之基础动画 alpha(渐变) scale(缩放) translate(移动) rotate(旋转)
-
Android仿网易游戏的精美开场动画+动画基础详解
-
《Android移动应用基础教程》(Android Studio)(第二版)黑马教程
-
Android 基础-动画设计和实现