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

本文详细介绍了如何在Android开发中使用LayoutAnimation为ViewGroup添加动画,包括创建布局动画、定义动画效果、应用动画到ViewGroup以及使用动画监听器等步骤。通过实例展示了如何在XML布局文件和代码中实现布局动画,以及如何通过设置动画参数来控制动画的顺序和开始时间。

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

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);







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值