Android中的布局动画

将相本无种,男儿当自强。这篇文章主要讲述Android中的布局动画相关的知识,希望能为你提供帮助。
简介
布局动画是给布局的动画,会影响到布局中子对象
使用方法
给布局添加动画效果:
先找到要设置的layout的id,然后创建布局动画,创建一个LayoutAnimationController,并把动画传给它,最后就可以设置这个布局的lac。
sa的duration是整个布局动画完成的时间,LAC的delay是指每一个子对象间的延迟时间。

LinearLayout layout = (LinearLayout) findViewById(R.id.activity_main); ScaleAnimation sa = new ScaleAnimation(0, 1, 0, 1); sa.setDuration(350); LayoutAnimationController lac = new LayoutAnimationController(sa, 0.5f); layout.setLayoutAnimation(lac);

布局内容改变动画:
通过设置layout的animateLayoutChange属性,可以达到布局内容改变的时候产生动画效果。
给列表添加布局动画:
【Android中的布局动画】与给布局添加动画效果类似,只需要创建lac,并设置给list即可。
public class MainActivity extends ListActivity {
private ArrayAdapter< String> adapter;
private LayoutAnimationController lac;
private ScaleAnimation sa;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
adapter = new ArrayAdapter< String> (this, android.R.layout.simple_list_item_1, new String[]{"Hello", "world", "!"});
setListAdapter(adapter);
sa = new ScaleAnimation(0, 1, 0, 1);
sa.setDuration(1000);
lac = new LayoutAnimationController(sa, 0.5f);
getListView().setLayoutAnimation(lac);
}
}

也可以通过xml给布局配置动画效果:
首先创建scale.xml:
< scale android:fromXScale="0" android:toXScale="1" android:toYScale="1" android:fromYScale="0" android:duration="3000" xmlns:android="http://schemas.android.com/apk/res/android"> < /scale>

然后创建layoutAnimation:
< layoutAnimation android:animation="@anim/scale" android:delay="0.5" xmlns:android="http://schemas.android.com/apk/res/android"> < /layoutAnimation>

最后可以在布局的组件中指定layoutAnimation:
< ListView android:id="@android:id/list" android:layoutAnimation="@anim/listview_anim" android:layout_width="match_parent" android:layout_height="match_parent"> < /ListView>

 

    推荐阅读