Animation动画效果四

本文详细介绍如何使用layoutAnimation为Android应用中的ListView添加动画效果。通过创建动画配置文件,并定义动画顺序和参数,实现在加载ListView时的动画展示。文章提供了一个完整的示例,包括XML动画配置文件、布局文件和Java代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[b]如何使用layoutAnimation[/b]
首先: 需要创建一个layoutAnimation的配置文件, 定义动画的步骤和引用动画
然后: 在需要动画效果的控件中加入[color=red]android:layoutAnimation="@anim/list_anim_layout"[/color].

[b]一个完成的例子:[/b]

[b]首先/res/anim/alpha.xml[/b]
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_decelerate_interpolator">
<alpha android:fromAlpha="0" android:toAlpha="1" android:duration="2000"/>
</set>


[b]新建一个/res/anim/list_anim_layout.xml[/b]
<?xml version="1.0" encoding="utf-8"?><!-- animationOrder: random, normal -->
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android" android:delay="2" android:animationOrder="normal" android:animation="@anim/alpha"></layoutAnimation>


[b]这个/res/layout/activity.xml是ListView用的[/b]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="50px">
<TextView android:id="@+id/view1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:layout_gravity="center_vertical"/>
<TextView android:id="@+id/view2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical"/>
</LinearLayout>


[b]/res/layout/main.xml主文件[/b]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:layoutAnimation="@anim/list_anim_layout"></ListView>
<Button android:text="Button" android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="setAlphaClick"></Button>
</LinearLayout>


[b]Animation3Activity.java文件[/b]
package com.cn;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class Animation3Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);


}
private void createListView(){
List<Map<String,String>> list = new ArrayList<Map<String,String>>();
Map<String,String> map = new HashMap<String,String>();
map.put("姓名", "小王");
map.put("年龄", "12");
list.add(map);
map = new HashMap<String,String>();
map.put("姓名", "小王");
map.put("年龄", "12");
list.add(map);
map = new HashMap<String,String>();
map.put("姓名", "小王");
map.put("年龄", "12");
list.add(map);
ListView view = (ListView)findViewById(R.id.listView1);
view.setAdapter(new SimpleAdapter(this, list, R.layout.activity, new String[]{"姓名","年龄"}, new int[]{R.id.view1,R.id.view2}));
// setContentView(view);
}

public void setAlphaClick(View v){
createListView();
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值