您的位置:首页 > 移动开发 > Android开发

Android 动画(一)---布局动画

2015-06-07 14:54 609 查看
LayoutAnimation 可以用来为ViewGroup添加动画,并按照预定的顺序把一个动画(或者动画集合)应用到ViewGroup的第一个子View 中。

可以使用LayoutAnimationController 来指定一个应用到View组中的每一个动画(或动画集合)。ViewGroup中包含的每一个View都将应用到这个相同的动画,但可以使用LayoutAnimationController来指定每一个View的顺序和起始时间。



1、创建布局动画

要创建一个新的布局动画,首先要定义一个将应用于每个子View的动画,然后,在代码中或者作为外部动画资源,创建一个新的LayoutAnimation,它引用了要应用的动画并定义了应用它的顺序和时刻安排。

1.1、定义一个将应用于每个子View的动画 popin.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<scale
android:duration="2000"
android:fromXScale="0.0"
android:fromYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.0"
android:toYScale="1.0" />

</set>


1.2、定义一个LayoutAnimation popinlayout.xml

<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:delay="0.5"
android:animationOrder="reverse"
android:animation="@anim/popin">

</layoutAnimation>


其中:

android:animationOrder="reverse"指定ViewGroup中的每一个子View应用动画的顺序,这里指定为“逆序”;

android:animation="@anim/popin"指定要在每一个View中的应用的动画,这里为1.1中的定义的动画资源;

2、使用布局动画

一旦定义了一个布局动画,就可以使用代码或者布局XML 资源将其应用到一个ViewGroup中。

2.1、在XML中使用,通过在布局定义中使用android:layoutAnimation="@anim/popinlayout"来完成

如下为 LinearLayout应用上述布局动画

<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:layoutAnimation="@anim/popinlayout">

<Button
android:id="@+id/start_anim_bt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/start_anim_bt_txt"/>
<TextView
android:id="@+id/info_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<ImageView
android:id="@+id/img_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/image2"/>

</LinearLayout>


2.2、在代码中设置一个布局动画,可以调用ViewGroup的setLayoutAnimation,并给它传递所希望应用到的LayoutAnimation对象的引用。

通常情况下,布局动画会在ViewGroup第一次进行布局的时候执行一次。可以通过对ViewGroup调用scheduleLayoutAnimation来强制它再次执行,然后当ViewGroup下次被布局的时候,这个动画就会再次执行。

布局动画也支持动画监听器。

<span style="white-space:pre">	</span>//得到要应用动画的ViewGroup
linearlayout_vg=(LinearLayout)findViewById(R.id.linearlayout_vg);

//得到在res/anim/popin.xml文件中定义的动画资源
Animation myAnimation= AnimationUtils.loadAnimation(MainActivity.this, R.anim.popin);

LayoutAnimationController layoutAnimationController=new LayoutAnimationController(myAnimation);
layoutAnimationController.setDelay(0.5f);
//设置ViewGroup中每一个子View应用动画的顺序
layoutAnimationController.setOrder(LayoutAnimationController.ORDER_REVERSE);

//布局动画也支持动画监听器
linearlayout_vg.setLayoutAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {

Toast.makeText(MainActivity.this,"动画开始了。。。",Toast.LENGTH_SHORT).show();

}

@Override
public void onAnimationEnd(Animation animation) {

Toast.makeText(MainActivity.this,"动画结束了。。。",Toast.LENGTH_SHORT).show();
}

@Override
public void onAnimationRepeat(Animation animation) {
Toast.makeText(MainActivity.this,"动画重复执行了。。。",Toast.LENGTH_SHORT).show();

}
});

//当ViewGroup被布局的时候,强制该动画再次执行
linearlayout_vg.scheduleLayoutAnimation();

//在ViewGroup中应用布局动画
linearlayout_vg.setLayoutAnimation(layoutAnimationController);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: