ListView是日常开发中经常用到的一个空间,我们可以自定义它的item,也可以为他的item添加动画。这里呢,我就说说如何为ListView添加动画。
为ListView添加动画一般采用LayoutAnimationController,他是一个布局动画控制器。
ListView有一个方法setLayoutAnimation(LayoutAnimationController layoutAnimationController),这个方法接受的是LayoutAnimationController这个布局动画控制器。LayoutAnimationController的构造参数接受一个动画和延迟的时间。所以我们可以这样用:
Animation mAnimation =new AlphaAnimation(0f,1f);//透明度的动画
mAnimation.setDuration(500);//设置动画时长
//实例化LayoutAnimationController
LayoutAnimationController mLayoutAnimationController =new LayoutAnimationController(mAnimation,1f);
//为LayoutAnimationController设置他的执行顺序,有3个常量
// LayoutAnimationController.ORDER_NORMAL;顺序显示
// LayoutAnimationController.ORDER_REVERSE;反显示
// LayoutAnimationController.ORDER_RANDOM;随机显示
mLayoutAnimationController.setOrder(LayoutAnimationController.ORDER_NORMAL);//我们用顺序显示
mListView.setLayoutAnimation(mLayoutAnimationController);//给ListView设置LayoutAnimationController
adapter.notifyDataSetInvalidated();//刷新适配器,让数据界面重绘
经过上述步骤,我们就给ListView设置了一个透明度变化的动画效果,下面给出我的一个实例:
package com.xigua.listview;
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.LayoutAnimationController;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class MainActivityextends AppCompatActivityimplements View.OnClickListener{
//控件
private ListViewmListView;
private ButtonmButton1,mButton2,mButton3,mButton4;
//动画控制器
private LayoutAnimationControllermLayoutAnimationController;
//动画类
private AnimationmAnimation;
//数据源
private String[]arry = {"一","二","三","四","五","六"};
//适配器
private ArrayAdapteradapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
adapter =new ArrayAdapter(this, android.R.layout.simple_list_item_1,arry);
mListView.setAdapter(adapter);
}
private void initViews() {
//初始化listview
mListView = findViewById(R.id.listView);
//初始化按钮,并添加点击事件
mButton1 = findViewById(R.id.button1);
mButton1.setOnClickListener(this);
mButton2 = findViewById(R.id.button2);
mButton2.setOnClickListener(this);
mButton3 = findViewById(R.id.button3);
mButton3.setOnClickListener(this);
mButton4 = findViewById(R.id.button4);
mButton4.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
mAnimation =new TranslateAnimation(-50f,0f,0f,0f);
mAnimation.setDuration(500);
mLayoutAnimationController =new LayoutAnimationController(mAnimation,1f);
mLayoutAnimationController.setOrder(LayoutAnimationController.ORDER_NORMAL);
mListView.setLayoutAnimation(mLayoutAnimationController);
adapter.notifyDataSetInvalidated();
break;
case R.id.button2:
mAnimation =new AlphaAnimation(0f,1f);
mAnimation.setDuration(500);
mLayoutAnimationController =new LayoutAnimationController(mAnimation,1f);
mLayoutAnimationController.setOrder(LayoutAnimationController.ORDER_NORMAL);
mListView.setLayoutAnimation(mLayoutAnimationController);
adapter.notifyDataSetInvalidated();
break;
case R.id.button3:
mAnimation =new RotateAnimation(0f,360f);
mAnimation.setDuration(500);
mLayoutAnimationController =new LayoutAnimationController(mAnimation,1f);
mLayoutAnimationController.setOrder(LayoutAnimationController.ORDER_NORMAL);
mListView.setLayoutAnimation(mLayoutAnimationController);
adapter.notifyDataSetInvalidated();
break;
case R.id.button4:
mAnimation =new ScaleAnimation(0.1f,1.0f,0.1f,1.0f);
mAnimation.setDuration(500);
mLayoutAnimationController =new LayoutAnimationController(mAnimation,1f);
mLayoutAnimationController.setOrder(LayoutAnimationController.ORDER_NORMAL);
mListView.setLayoutAnimation(mLayoutAnimationController);
adapter.notifyDataSetInvalidated();
break;
default:
break;
}
}
}
xml文件布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:orientation="horizontal"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/button1"
android:text="位移"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button2"
android:text="透明"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button3"
android:text="旋转"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button4"
android:text="缩放"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
以上就是为ListViewt添加动画的全部过程。
网友评论