佈局動畫
佈局動畫是針對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();
}
}