標籤:blog view hang 內容 lis match public this code
簡介
布局動畫是給布局的動畫,會影響到布局中子物件
使用方法
給布局添加動畫效果:
先找到要設定的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屬性,可以達到布局內容改變的時候產生動畫效果。
給列表添加布局動畫:
與給布局添加動畫效果類似,只需要建立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>
Android中的布局動畫