Android 动画 LayoutAnimationsController
2012-01-18 14:24
435 查看
1、什么是LayoutAnimationsController
LayoutAnimationsController可以用于实现使多个控件按顺序一个一个的显示。
1)LayoutAnimationsController用于为一个layout里面的控件,或者是一个ViewGroup里面的控件设置统一的动画效果。
2)每一个控件都有相同的动画效果。
3)控件的动画效果可以在不同的时间显示出来。
4)LayoutAnimationsController可以在xml文件当中设置,以可以在代码当中进行设置。
2、在xml当中使用LayoutAnimationController
1)在res/anim文件夹下创建一个名为list_anim_layout.xml文件:
android:dylay -
动画间隔时间;
android:animationOrder -
动画执行的循序(normal:顺序,random:随机,reverse:反向显示)
android:animation –
引用动画效果文件
<layoutAnimation
xmlns:android="http://schemas.android.com/apk/res/android"
android:delay="0.5"
android:animationOrder="normal"
android:animation="@anim/list_anim"/>
2)创建list_anim.xml文件,设置动画效果
<?xml
version="1.0"
encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:shareInterpolator="true">
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="1000"/>
</set>
3)在布局文件main.xml当中为ListVIew添加如下配置
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:layoutAnimation="@anim/list_anim_layout"/>
4)程序结构
5)list_anim_layout.xml
<layoutAnimation
xmlns:android="http://schemas.android.com/apk/res/android"
android:delay="0.5"
android:animationOrder="normal"
android:animation="@anim/list_anim"/>
6)list_anim.xml
<?xml
version="1.0"
encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:shareInterpolator="true">
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="1000"/>
</set>
7)main.xml
<?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"
>
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:layoutAnimation="@anim/list_anim_layout"/>
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="测试"/>
</LinearLayout>
8)item.xml
<?xml
version="1.0"
encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:paddingTop="1dip"
android:paddingBottom="1dip">
<TextView
android:id="@+id/name"
android:layout_width="180dip"
android:layout_height="30dip"
android:textSize="5pt"
android:singleLine="true"
/>
<TextView
android:id="@+id/sex"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="5pt"
android:singleLine="true"/>
</LinearLayout>
9)java文件
public
class Animation2Activity
extendsListActivity {
private
Button
button =
null;
private
ListView
listView =
null;
@Override
public
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView
= getListView();
button
= (Button)findViewById(R.id.button);
button.setOnClickListener(newButtonListener());
}
private
ListAdapter createListAdapter() {
List<HashMap<String,String>> list =
new
ArrayList<HashMap<String,String>>();
HashMap<String,String> m1 =
new
HashMap<String,String>();
m1.put("name",
"bauble");
m1.put("sex",
"male");
HashMap<String,String> m2 =
new
HashMap<String,String>();
m2.put("name",
"Allorry");
m2.put("sex",
"male");
HashMap<String,String> m3 =
new
HashMap<String,String>();
m3.put("name",
"Allotory");
m3.put("sex",
"male");
HashMap<String,String> m4 =
new
HashMap<String,String>();
m4.put("name",
"boolbe");
m4.put("sex",
"male");
list.add(m1);
list.add(m2);
list.add(m3);
list.add(m4);
SimpleAdapter simpleAdapter =
new
SimpleAdapter(
this,list,R.layout.item,new
String[]{"name","sex"},
new
int[]{R.id.name,R.id.sex});
return
simpleAdapter;
}
private
class ButtonListener
implementsOnClickListener{
public
void onClick(View v) {
listView.setAdapter(createListAdapter());
}
}
}
备注:要将整个动画效果设置到LinerLayout中,可以这样设置:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layoutAnimation="@anim/list_anim_layout"
>
3、在代码当中使用LayoutAnimationController
1)去掉main.xml中的android:layoutAnimation="@anim/list_anim_layout"/>
2)创建一个Animation对象:可以通过装载xml文件,或者是直接使用Animation的构造方法创建Animation对象;
Animation animation = (Animation) AnimationUtils.loadAnimation(
Animation2Activity.this,
R.anim.list_anim);
3)创建LayoutAnimationController对象:
LayoutAnimationController
controller =
new
LayoutAnimationController(animation);
4)设置控件的显示顺序以及延迟时间
controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
controller.setDelay(0.5f);
5)为ListView设置LayoutAnimationController属性:
listView.setLayoutAnimation(controller);
完整代码:
private
class ButtonListener
implementsOnClickListener {
public
void onClick(View v) {
listView.setAdapter(createListAdapter());
Animation animation = (Animation) AnimationUtils.loadAnimation(
Animation2Activity.this,
R.anim.list_anim);
LayoutAnimationController controller =
new
LayoutAnimationController(animation);
controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
controller.setDelay(0.5f);
listView.setLayoutAnimation(controller);
}
}
二、
AnimationListener
1、什么是AnimationListener
1).AnimationListener是一个监听器,该监听器在动画执行的各个阶段会得到通知,从而调用相应的方法;
2).AnimationListener主要包括如下三个方法:
n
·onAnimationEnd(Animation animation) -
当动画结束时调用
n
·onAnimationRepeat(Animation animation) -
当动画重复时调用
n
·onAniamtionStart(Animation animation) -
当动画启动时调用
2、具体实现
1)main.xml
<?xml
version="1.0"
encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/addButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="添加图片"
/>
<Button
android:id="@+id/deleteButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@id/addButton"
android:text="删除图片"
/>
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginTop="100dip"
android:src="@drawable/an"
/>
</RelativeLayout>
2).java文件
public
class Animation2Activity
extends
Activity {
private
Button
addButton =
null;
private
Button
deleteButton =
null;
private
ImageView
imageView =
null;
private
ViewGroup
viewGroup =
null;
@Override
public
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addButton
= (Button)findViewById(R.id.addButton);
deleteButton
= (Button)findViewById(R.id.deleteButton);
imageView
= (ImageView)findViewById(R.id.image);
//LinearLayout下的一组控件
viewGroup
= (ViewGroup)findViewById(R.id.layout);
addButton.setOnClickListener(newAddButtonListener());
deleteButton.setOnClickListener(newDeleteButtonListener());
}
private
class AddButtonListener
implements
OnClickListener{
public
void onClick(View v) {
//淡入
AlphaAnimation animation =
new
AlphaAnimation(0.0f, 1.0f);
animation.setDuration(1000);
animation.setStartOffset(500);
//创建一个新的ImageView
ImageView newImageView =
new
ImageView(
Animation2Activity.this);
newImageView.setImageResource(R.drawable.an);
viewGroup.addView(newImageView,
new
LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
newImageView.startAnimation(animation);
}
}
private
class DeleteButtonListener
implements
OnClickListener{
public
void onClick(View v) {
//淡出
AlphaAnimation animation =
new
AlphaAnimation(1.0f, 0.0f);
animation.setDuration(1000);
animation.setStartOffset(500);
//为Aniamtion对象设置监听器
animation.setAnimationListener(
new
RemoveAnimationListener());
imageView.startAnimation(animation);
}
}
private
class RemoveAnimationListener
implements
AnimationListener{
//动画效果执行完时remove
public
void onAnimationEnd(Animation animation) {
System.out.println("onAnimationEnd");
viewGroup.removeView(imageView);
}
public
void onAnimationRepeat(Animation animation) {
System.out.println("onAnimationRepeat");
}
public
void onAnimationStart(Animation animation) {
System.out.println("onAnimationStart");
}
}
}
3、总结一下
可以在Activity中动态添加和删除控件,方法是:
1)取到那个Layout
viewGroup
= (ViewGroup)findViewById(R.id.layout);
2)添加时,先创建对象,然后添加
ImageView newImageView =
new
ImageView(
Animation2Activity.this);
newImageView.setImageResource(R.drawable.an);
viewGroup.addView(newImageView,
new
LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
3)删除时,直接删除。
viewGroup.removeView(imageView);
LayoutAnimationsController可以用于实现使多个控件按顺序一个一个的显示。
1)LayoutAnimationsController用于为一个layout里面的控件,或者是一个ViewGroup里面的控件设置统一的动画效果。
2)每一个控件都有相同的动画效果。
3)控件的动画效果可以在不同的时间显示出来。
4)LayoutAnimationsController可以在xml文件当中设置,以可以在代码当中进行设置。
2、在xml当中使用LayoutAnimationController
1)在res/anim文件夹下创建一个名为list_anim_layout.xml文件:
android:dylay -
动画间隔时间;
android:animationOrder -
动画执行的循序(normal:顺序,random:随机,reverse:反向显示)
android:animation –
引用动画效果文件
<layoutAnimation
xmlns:android="http://schemas.android.com/apk/res/android"
android:delay="0.5"
android:animationOrder="normal"
android:animation="@anim/list_anim"/>
2)创建list_anim.xml文件,设置动画效果
<?xml
version="1.0"
encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:shareInterpolator="true">
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="1000"/>
</set>
3)在布局文件main.xml当中为ListVIew添加如下配置
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:layoutAnimation="@anim/list_anim_layout"/>
4)程序结构
5)list_anim_layout.xml
<layoutAnimation
xmlns:android="http://schemas.android.com/apk/res/android"
android:delay="0.5"
android:animationOrder="normal"
android:animation="@anim/list_anim"/>
6)list_anim.xml
<?xml
version="1.0"
encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:shareInterpolator="true">
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="1000"/>
</set>
7)main.xml
<?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"
>
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:layoutAnimation="@anim/list_anim_layout"/>
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="测试"/>
</LinearLayout>
8)item.xml
<?xml
version="1.0"
encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:paddingTop="1dip"
android:paddingBottom="1dip">
<TextView
android:id="@+id/name"
android:layout_width="180dip"
android:layout_height="30dip"
android:textSize="5pt"
android:singleLine="true"
/>
<TextView
android:id="@+id/sex"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="5pt"
android:singleLine="true"/>
</LinearLayout>
9)java文件
public
class Animation2Activity
extendsListActivity {
private
Button
button =
null;
private
ListView
listView =
null;
@Override
public
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView
= getListView();
button
= (Button)findViewById(R.id.button);
button.setOnClickListener(newButtonListener());
}
private
ListAdapter createListAdapter() {
List<HashMap<String,String>> list =
new
ArrayList<HashMap<String,String>>();
HashMap<String,String> m1 =
new
HashMap<String,String>();
m1.put("name",
"bauble");
m1.put("sex",
"male");
HashMap<String,String> m2 =
new
HashMap<String,String>();
m2.put("name",
"Allorry");
m2.put("sex",
"male");
HashMap<String,String> m3 =
new
HashMap<String,String>();
m3.put("name",
"Allotory");
m3.put("sex",
"male");
HashMap<String,String> m4 =
new
HashMap<String,String>();
m4.put("name",
"boolbe");
m4.put("sex",
"male");
list.add(m1);
list.add(m2);
list.add(m3);
list.add(m4);
SimpleAdapter simpleAdapter =
new
SimpleAdapter(
this,list,R.layout.item,new
String[]{"name","sex"},
new
int[]{R.id.name,R.id.sex});
return
simpleAdapter;
}
private
class ButtonListener
implementsOnClickListener{
public
void onClick(View v) {
listView.setAdapter(createListAdapter());
}
}
}
备注:要将整个动画效果设置到LinerLayout中,可以这样设置:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layoutAnimation="@anim/list_anim_layout"
>
3、在代码当中使用LayoutAnimationController
1)去掉main.xml中的android:layoutAnimation="@anim/list_anim_layout"/>
2)创建一个Animation对象:可以通过装载xml文件,或者是直接使用Animation的构造方法创建Animation对象;
Animation animation = (Animation) AnimationUtils.loadAnimation(
Animation2Activity.this,
R.anim.list_anim);
3)创建LayoutAnimationController对象:
LayoutAnimationController
controller =
new
LayoutAnimationController(animation);
4)设置控件的显示顺序以及延迟时间
controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
controller.setDelay(0.5f);
5)为ListView设置LayoutAnimationController属性:
listView.setLayoutAnimation(controller);
完整代码:
private
class ButtonListener
implementsOnClickListener {
public
void onClick(View v) {
listView.setAdapter(createListAdapter());
Animation animation = (Animation) AnimationUtils.loadAnimation(
Animation2Activity.this,
R.anim.list_anim);
LayoutAnimationController controller =
new
LayoutAnimationController(animation);
controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
controller.setDelay(0.5f);
listView.setLayoutAnimation(controller);
}
}
二、
AnimationListener
1、什么是AnimationListener
1).AnimationListener是一个监听器,该监听器在动画执行的各个阶段会得到通知,从而调用相应的方法;
2).AnimationListener主要包括如下三个方法:
n
·onAnimationEnd(Animation animation) -
当动画结束时调用
n
·onAnimationRepeat(Animation animation) -
当动画重复时调用
n
·onAniamtionStart(Animation animation) -
当动画启动时调用
2、具体实现
1)main.xml
<?xml
version="1.0"
encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/addButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="添加图片"
/>
<Button
android:id="@+id/deleteButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@id/addButton"
android:text="删除图片"
/>
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginTop="100dip"
android:src="@drawable/an"
/>
</RelativeLayout>
2).java文件
public
class Animation2Activity
extends
Activity {
private
Button
addButton =
null;
private
Button
deleteButton =
null;
private
ImageView
imageView =
null;
private
ViewGroup
viewGroup =
null;
@Override
public
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addButton
= (Button)findViewById(R.id.addButton);
deleteButton
= (Button)findViewById(R.id.deleteButton);
imageView
= (ImageView)findViewById(R.id.image);
//LinearLayout下的一组控件
viewGroup
= (ViewGroup)findViewById(R.id.layout);
addButton.setOnClickListener(newAddButtonListener());
deleteButton.setOnClickListener(newDeleteButtonListener());
}
private
class AddButtonListener
implements
OnClickListener{
public
void onClick(View v) {
//淡入
AlphaAnimation animation =
new
AlphaAnimation(0.0f, 1.0f);
animation.setDuration(1000);
animation.setStartOffset(500);
//创建一个新的ImageView
ImageView newImageView =
new
ImageView(
Animation2Activity.this);
newImageView.setImageResource(R.drawable.an);
viewGroup.addView(newImageView,
new
LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
newImageView.startAnimation(animation);
}
}
private
class DeleteButtonListener
implements
OnClickListener{
public
void onClick(View v) {
//淡出
AlphaAnimation animation =
new
AlphaAnimation(1.0f, 0.0f);
animation.setDuration(1000);
animation.setStartOffset(500);
//为Aniamtion对象设置监听器
animation.setAnimationListener(
new
RemoveAnimationListener());
imageView.startAnimation(animation);
}
}
private
class RemoveAnimationListener
implements
AnimationListener{
//动画效果执行完时remove
public
void onAnimationEnd(Animation animation) {
System.out.println("onAnimationEnd");
viewGroup.removeView(imageView);
}
public
void onAnimationRepeat(Animation animation) {
System.out.println("onAnimationRepeat");
}
public
void onAnimationStart(Animation animation) {
System.out.println("onAnimationStart");
}
}
}
3、总结一下
可以在Activity中动态添加和删除控件,方法是:
1)取到那个Layout
viewGroup
= (ViewGroup)findViewById(R.id.layout);
2)添加时,先创建对象,然后添加
ImageView newImageView =
new
ImageView(
Animation2Activity.this);
newImageView.setImageResource(R.drawable.an);
viewGroup.addView(newImageView,
new
LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
3)删除时,直接删除。
viewGroup.removeView(imageView);
相关文章推荐
- Android动画之LayoutAnimationController(五)
- Android动画之LayoutAnimationController(五)
- Android开发--图形图像与动画(五)--详解LayoutAnimationController
- Android开发--图形图像与动画(五)--详解LayoutAnimationController
- 【Android动画九章】-LayoutAnimationController
- Android动画之LayoutAnimationController(五)
- Android动画-LayoutAnimationController
- Android Animation(动画)---基础二(LayoutAnimationController)
- Android属性动画PropertyAnimation LayoutTransition(布局容器动画)
- Android属性动画PropertyAnimation系列三之LayoutTransition(布局容器动画)
- 用LayoutAnimationController给ViewGroup的item添加动画,以GridView为例
- Layout动画:在android布局发生变化时添加动画效果
- android布局中子View添加动态隐藏和显示的动画效果。(LayoutTransition)
- Android_LayoutAnimationController
- Android Study Material Design 十三 之CoordinatorLayout交互动画
- Android Animations动画使用详解
- Android SwiperRefreshLayout 首次加载显示刷新动画
- Android Animations动画使用详解
- Android 布局动画之LayoutAnimation与LayoutTransition
- android.view.animation(3) - LayoutAnimationController 和 GridLayoutAnimationController