Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

Android 動畫開發:四種 Animation 效果

Animation 是 Android 提供動畫效果的元件,能做出絢麗的翻頁、以及 ListView 與 GridView 的進場展示。

本文簡單介紹四種動畫方式:

1. AlphaAnimation——控制漸變透明的動畫效果。

2. ScaleAnimation——控制尺寸伸縮的動畫效果。

3. TranslateAnimation——控制畫面平移的動畫效果。

4. RotateAnimation——控制畫面角度變化的動畫效果。

具體用法直接看程式碼。以下範例寫在 Activity 的 onCreate() 中,直接為 ListView 載入動畫效果:

AnimationSet set = new AnimationSet(false);

Animation animation = new AlphaAnimation(0, 1);   // 控制漸變透明
animation.setDuration(500);                        // 動畫時間(毫秒)
set.addAnimation(animation);                       // 加入動畫集合

animation = new TranslateAnimation(1, 13, 10, 50); // 控制畫面平移
animation.setDuration(300);
set.addAnimation(animation);

animation = new RotateAnimation(30, 10);           // 控制畫面角度變化
animation.setDuration(300);
set.addAnimation(animation);

animation = new ScaleAnimation(5, 0, 2, 0);        // 控制尺寸伸縮
animation.setDuration(300);
set.addAnimation(animation);

LayoutAnimationController controller = new LayoutAnimationController(set, 1);

GridView gridView = (GridView) this.findViewById(R.id.gridview);
gridView.setLayoutAnimation(controller);           // 為 GridView 設置動畫

ListView listview = (ListView) this.findViewById(R.id.listview);
listview.setLayoutAnimation(controller);           // 為 ListView 設置動畫

把四種動畫放進同一個 AnimationSet,再用 LayoutAnimationController 套到列表控制項上,就能看到多段效果依序串接的進場表現。實作時可依需求增減動畫、調整 setDuration 的毫秒數,或替控制項加上延遲參數以製造錯落的效果。

Copyright © Linux教程網 All Rights Reserved