Android布局动画和逐帧动画

来源:互联网 发布:vb读取excel单元格 编辑:程序博客网 时间:2024/05/29 13:50

布局动画

布局动画是针对ViewGroup的动画,首先是效果:

通过Xml方式实现:
首先为ViewGroup添加layoutAnimation属性,
这里写图片描述

<ListView        android:id="@+id/listview"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:listSelector="#00000000"        android:layoutAnimation="@anim/layout_animation"        />

layoutAnimation.xml:

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

delay:延时
animationOrder:动画顺序,normal(正序),reverse(反序),random(随机)
animation:加载的动画效果
animation.xml:

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"     >    <scale        android:fromXScale="0%"        android:fromYScale="0%"        android:toXScale="100%"        android:toYScale="100%"        android:pivotX="50%"        android:pivotY="50%"        android:duration="444"/>    <alpha         android:fromAlpha="0"        android:toAlpha="1"        android:duration="444"        /></set>

通过代码实现:
我们只需要上面的@anim/in这个动画效果即可,其余通过代码实现。

Animation animation = AnimationUtils.loadAnimation(this, R.anim.in);        LayoutAnimationController lac = new LayoutAnimationController(animation,0.1f);        lac.setOrder(LayoutAnimationController.ORDER_RANDOM);        listView.setLayoutAnimation(lac);        listView.startLayoutAnimation();

逐帧动画

逐帧动画就是通过控制,一帧一帧的播放图片形成动画。
先上效果图:
这里写图片描述
官方实例中xml文件放在了drawable文件夹下,那我们就在drawable文件夹下创建frame.xml:

<?xml version="1.0" encoding="utf-8"?><animation-list xmlns:android="http://schemas.android.com/apk/res/android"    android:oneshot="false" >    <item android:duration="150" android:drawable="@drawable/loading_01" />    <item android:duration="150" android:drawable="@drawable/loading_02" />    <item android:duration="150" android:drawable="@drawable/loading_03" />    <item android:duration="150" android:drawable="@drawable/loading_04" />    <item android:duration="150" android:drawable="@drawable/loading_05" />    <item android:duration="150" android:drawable="@drawable/loading_06" />    <item android:duration="150" android:drawable="@drawable/loading_07" /></animation-list>

当然也可以在代码中实现:

@Override// 在onCreata方法中只加载一帧,因为onCreate执行的时候,界面还没加载完成,onWindowFocusChanged会在onResume之后被调用,但是此时界面还是没有加载出来(黑乎乎的),等待onDraw,但是这时view都已经计算完成。    public void onWindowFocusChanged(boolean hasFocus)    {        super.onWindowFocusChanged(hasFocus);        if(hasFocus)        {            image.setBackgroundResource(R.drawable.frame);            ((AnimationDrawable)image.getBackground()).start();        }    }
0 0
原创粉丝点击