Android中的布局動畫

來源:互聯網
上載者:User

標籤: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中的布局動畫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.